Skip to content

Commit 664833d

Browse files
felipmiguelbillwertVinay Gera
authored
Parse expire_in attribute as seconds from now (Azure#32306)
* Parse expire_in attribute as seconds from now * Update sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/MSIToken.java Co-authored-by: Bill Wert <billwert@microsoft.com> * fixing expires_in tests * keep expires_on implementation and change only behavior for expires_in * refactor code and add tests. Co-authored-by: Bill Wert <billwert@microsoft.com> Co-authored-by: Vinay Gera <vinay_work@hotmail.com>
1 parent fcf8237 commit 664833d

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/MSIToken.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ public MSIToken(
5555
@JsonProperty(value = "access_token") String token,
5656
@JsonProperty(value = "expires_on") String expiresOn,
5757
@JsonProperty(value = "expires_in") String expiresIn) {
58-
super(token, EPOCH.plusSeconds(parseDateToEpochSeconds(CoreUtils.isNullOrEmpty(expiresOn) ? expiresIn
59-
: expiresOn)));
58+
super(token, EPOCH.plusSeconds(parseToEpochSeconds(expiresOn, expiresIn)));
6059
this.accessToken = token;
61-
this.expiresOn = expiresOn;
60+
this.expiresOn = expiresOn;
6261
this.expiresIn = expiresIn;
6362
}
6463

@@ -67,26 +66,32 @@ public String getToken() {
6766
return accessToken;
6867
}
6968

70-
private static Long parseDateToEpochSeconds(String dateTime) {
69+
private static Long parseToEpochSeconds(String expiresOn, String expiresIn) {
70+
String dateToParse = CoreUtils.isNullOrEmpty(expiresOn) ? expiresIn : expiresOn;
71+
7172
try {
72-
return Long.parseLong(dateTime);
73+
Long seconds = Long.parseLong(dateToParse);
74+
if (!CoreUtils.isNullOrEmpty(expiresOn)) {
75+
return seconds;
76+
} else {
77+
return OffsetDateTime.now(ZoneOffset.UTC).plusSeconds(seconds).toEpochSecond();
78+
}
7379
} catch (NumberFormatException e) {
7480
LOGGER.verbose(e.getMessage());
7581
}
7682

7783
try {
78-
return Instant.from(DTF.parse(dateTime)).getEpochSecond();
84+
return Instant.from(DTF.parse(dateToParse)).getEpochSecond();
7985
} catch (DateTimeParseException e) {
8086
LOGGER.verbose(e.getMessage());
8187
}
8288

8389
try {
84-
return Instant.from(DTF_WINDOWS.parse(dateTime)).getEpochSecond();
90+
return Instant.from(DTF_WINDOWS.parse(dateToParse)).getEpochSecond();
8591
} catch (DateTimeParseException e) {
8692
LOGGER.verbose(e.getMessage());
8793
}
88-
89-
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Unable to parse date time " + dateTime));
94+
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Unable to parse date time " + dateToParse));
9095
}
9196

9297
}

sdk/identity/azure-identity/src/test/java/com/azure/identity/implementation/MSITokenTests.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88

99
import java.time.OffsetDateTime;
1010
import java.time.ZoneOffset;
11+
import java.time.temporal.ChronoUnit;
1112

1213
public class MSITokenTests {
1314
private OffsetDateTime expected = OffsetDateTime.of(2020, 1, 10, 15, 3, 28, 0, ZoneOffset.UTC);
1415

1516
@Test
1617
public void canParseLong() {
1718
MSIToken token = new MSIToken("fake_token", "1578668608", null);
18-
MSIToken token2 = new MSIToken("fake_token", null, "1578668608");
19-
MSIToken token3 = new MSIToken("fake_token", "1578668608", "1778668987");
19+
MSIToken token2 = new MSIToken("fake_token", null, "3599");
20+
MSIToken token3 = new MSIToken("fake_token", "1578668608", "3599");
2021

2122
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
22-
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
23+
Assert.assertTrue((token2.getExpiresAt().toEpochSecond() - OffsetDateTime.now().toEpochSecond()) > 3500);
2324
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
2425
}
2526

@@ -28,40 +29,50 @@ public void canParseDateTime24Hr() {
2829
MSIToken token = new MSIToken("fake_token", "01/10/2020 15:03:28 +00:00", null);
2930
MSIToken token2 = new MSIToken("fake_token", null, "01/10/2020 15:03:28 +00:00");
3031
MSIToken token3 = new MSIToken("fake_token", "01/10/2020 15:03:28 +00:00",
31-
"01/12/2020 15:03:28 +00:00");
32+
"86500");
33+
MSIToken token4 = new MSIToken("fake_token", null, "43219");
3234

3335
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
3436
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
3537
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
38+
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 12L);
3639
}
3740

3841
@Test
3942
public void canParseDateTime12Hr() {
4043
MSIToken token = new MSIToken("fake_token", "1/10/2020 3:03:28 PM +00:00", null);
4144
MSIToken token2 = new MSIToken("fake_token", null, "1/10/2020 3:03:28 PM +00:00");
4245
MSIToken token3 = new MSIToken("fake_token", "1/10/2020 3:03:28 PM +00:00",
43-
"1/12/2020 4:03:28 PM +00:00");
46+
"86500");
47+
MSIToken token4 = new MSIToken("fake_token", null, "86500");
4448

4549
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
4650
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
4751
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
52+
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 24L);
4853

4954
token = new MSIToken("fake_token", "12/20/2019 4:58:20 AM +00:00", null);
5055
token2 = new MSIToken("fake_token", null, "12/20/2019 4:58:20 AM +00:00");
5156
token3 = new MSIToken("fake_token", "12/20/2019 4:58:20 AM +00:00",
52-
"11/15/2021 4:58:20 AM +00:00");
57+
"105500");
58+
token4 = new MSIToken("fake_token", null, "105500");
5359
expected = OffsetDateTime.of(2019, 12, 20, 4, 58, 20, 0, ZoneOffset.UTC);
60+
5461
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
5562
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
5663
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
64+
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 29L);
5765

5866
token = new MSIToken("fake_token", "1/1/2020 0:00:00 PM +00:00", null);
5967
token2 = new MSIToken("fake_token", null, "1/1/2020 0:00:00 PM +00:00");
6068
token3 = new MSIToken("fake_token", "1/1/2020 0:00:00 PM +00:00",
61-
"1/1/2025 0:00:00 PM +00:00");
69+
"220800");
70+
token4 = new MSIToken("fake_token", null, "220800");
71+
6272
expected = OffsetDateTime.of(2020, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC);
6373
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
6474
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
6575
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
76+
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 61L);
6677
}
6778
}

0 commit comments

Comments
 (0)