Skip to content

Commit

Permalink
Parse expire_in attribute as seconds from now (Azure#32306)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
Co-authored-by: Vinay Gera <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2022
1 parent fcf8237 commit 664833d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ public MSIToken(
@JsonProperty(value = "access_token") String token,
@JsonProperty(value = "expires_on") String expiresOn,
@JsonProperty(value = "expires_in") String expiresIn) {
super(token, EPOCH.plusSeconds(parseDateToEpochSeconds(CoreUtils.isNullOrEmpty(expiresOn) ? expiresIn
: expiresOn)));
super(token, EPOCH.plusSeconds(parseToEpochSeconds(expiresOn, expiresIn)));
this.accessToken = token;
this.expiresOn = expiresOn;
this.expiresOn = expiresOn;
this.expiresIn = expiresIn;
}

Expand All @@ -67,26 +66,32 @@ public String getToken() {
return accessToken;
}

private static Long parseDateToEpochSeconds(String dateTime) {
private static Long parseToEpochSeconds(String expiresOn, String expiresIn) {
String dateToParse = CoreUtils.isNullOrEmpty(expiresOn) ? expiresIn : expiresOn;

try {
return Long.parseLong(dateTime);
Long seconds = Long.parseLong(dateToParse);
if (!CoreUtils.isNullOrEmpty(expiresOn)) {
return seconds;
} else {
return OffsetDateTime.now(ZoneOffset.UTC).plusSeconds(seconds).toEpochSecond();
}
} catch (NumberFormatException e) {
LOGGER.verbose(e.getMessage());
}

try {
return Instant.from(DTF.parse(dateTime)).getEpochSecond();
return Instant.from(DTF.parse(dateToParse)).getEpochSecond();
} catch (DateTimeParseException e) {
LOGGER.verbose(e.getMessage());
}

try {
return Instant.from(DTF_WINDOWS.parse(dateTime)).getEpochSecond();
return Instant.from(DTF_WINDOWS.parse(dateToParse)).getEpochSecond();
} catch (DateTimeParseException e) {
LOGGER.verbose(e.getMessage());
}

throw LOGGER.logExceptionAsError(new IllegalArgumentException("Unable to parse date time " + dateTime));
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Unable to parse date time " + dateToParse));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

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

@Test
public void canParseLong() {
MSIToken token = new MSIToken("fake_token", "1578668608", null);
MSIToken token2 = new MSIToken("fake_token", null, "1578668608");
MSIToken token3 = new MSIToken("fake_token", "1578668608", "1778668987");
MSIToken token2 = new MSIToken("fake_token", null, "3599");
MSIToken token3 = new MSIToken("fake_token", "1578668608", "3599");

Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
Assert.assertTrue((token2.getExpiresAt().toEpochSecond() - OffsetDateTime.now().toEpochSecond()) > 3500);
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
}

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

Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 12L);
}

@Test
public void canParseDateTime12Hr() {
MSIToken token = new MSIToken("fake_token", "1/10/2020 3:03:28 PM +00:00", null);
MSIToken token2 = new MSIToken("fake_token", null, "1/10/2020 3:03:28 PM +00:00");
MSIToken token3 = new MSIToken("fake_token", "1/10/2020 3:03:28 PM +00:00",
"1/12/2020 4:03:28 PM +00:00");
"86500");
MSIToken token4 = new MSIToken("fake_token", null, "86500");

Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 24L);

token = new MSIToken("fake_token", "12/20/2019 4:58:20 AM +00:00", null);
token2 = new MSIToken("fake_token", null, "12/20/2019 4:58:20 AM +00:00");
token3 = new MSIToken("fake_token", "12/20/2019 4:58:20 AM +00:00",
"11/15/2021 4:58:20 AM +00:00");
"105500");
token4 = new MSIToken("fake_token", null, "105500");
expected = OffsetDateTime.of(2019, 12, 20, 4, 58, 20, 0, ZoneOffset.UTC);

Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 29L);

token = new MSIToken("fake_token", "1/1/2020 0:00:00 PM +00:00", null);
token2 = new MSIToken("fake_token", null, "1/1/2020 0:00:00 PM +00:00");
token3 = new MSIToken("fake_token", "1/1/2020 0:00:00 PM +00:00",
"1/1/2025 0:00:00 PM +00:00");
"220800");
token4 = new MSIToken("fake_token", null, "220800");

expected = OffsetDateTime.of(2020, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC);
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token2.getExpiresAt().toEpochSecond());
Assert.assertEquals(expected.toEpochSecond(), token3.getExpiresAt().toEpochSecond());
Assert.assertTrue(ChronoUnit.HOURS.between(OffsetDateTime.now(), token4.getExpiresAt()) == 61L);
}
}

0 comments on commit 664833d

Please sign in to comment.