Skip to content

Commit

Permalink
Support AM/PM in MSI token (Azure#7356)
Browse files Browse the repository at this point in the history
* Support AM/PM in MSI token

* Fix tests

* Use SerializerAdapter

* opens implementation to jackson

* Remove deserialization in msi token test

* Revert back to default locale

* Remove unused import
  • Loading branch information
jianghaolu authored Jan 13, 2020
1 parent 10e4bd5 commit 0c76069
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ public final class MSIToken extends AccessToken {
* @param token the token string.
* @param expiresOn the expiration time.
*/
public MSIToken(String token, OffsetDateTime expiresOn) {
super(token, expiresOn);
}

@JsonCreator
private MSIToken(
public MSIToken(
@JsonProperty(value = "access_token") String token,
@JsonProperty(value = "expires_on") String expiresOn) {
this(token, EPOCH.plusSeconds(parseDateToEpochSeconds(expiresOn)));
super(token, EPOCH.plusSeconds(parseDateToEpochSeconds(expiresOn)));
this.accessToken = token;
this.expiresOn = expiresOn;
}
Expand All @@ -53,29 +49,27 @@ public String getToken() {
return accessToken;
}

@Override
public OffsetDateTime getExpiresAt() {
return EPOCH.plusSeconds(parseDateToEpochSeconds(this.expiresOn));
}

@Override
public boolean isExpired() {
OffsetDateTime now = OffsetDateTime.now();
OffsetDateTime expireOn = EPOCH.plusSeconds(parseDateToEpochSeconds(this.expiresOn));
return now.plusMinutes(5).isAfter(expireOn);
}

private static Long parseDateToEpochSeconds(String dateTime) {
ClientLogger logger = new ClientLogger(MSIToken.class);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss XXX");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/yyyy H:mm:ss XXX");
// This is the format for app service on Windows as of API version 2017-09-01.
// The format is changed to Unix timestamp in 2019-08-01 but this API version
// has not been deployed to Linux app services.
DateTimeFormatter dtfWindows = DateTimeFormatter.ofPattern("M/d/yyyy K:mm:ss a XXX");
try {
return Long.parseLong(dateTime);
} catch (NumberFormatException e) {
logger.error(e.getMessage());
}

try {
return Instant.from(dtf.parse(dateTime)).toEpochMilli() / 1000L;
return Instant.from(dtf.parse(dateTime)).getEpochSecond();
} catch (DateTimeParseException e) {
logger.error(e.getMessage());
}

try {
return Instant.from(dtfWindows.parse(dateTime)).getEpochSecond();
} catch (DateTimeParseException e) {
logger.error(e.getMessage());
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/identity/azure-identity/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
requires org.reactivestreams;

exports com.azure.identity;

opens com.azure.identity.implementation to com.fasterxml.jackson.databind;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.identity.implementation;

import org.junit.Assert;
import org.junit.Test;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

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

@Test
public void canParseLong() {
MSIToken token = new MSIToken("fake_token", "1578668608");
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
}

@Test
public void canParseDateTime24Hr() {
MSIToken token = new MSIToken("fake_token", "01/10/2020 15:03:28 +00:00");
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
}

@Test
public void canParseDateTime12Hr() {
MSIToken token = new MSIToken("fake_token", "1/10/2020 3:03:28 PM +00:00");
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());

token = new MSIToken("fake_token", "12/20/2019 4:58:20 AM +00:00");
expected = OffsetDateTime.of(2019, 12, 20, 4, 56, 20, 0, ZoneOffset.UTC);
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());

token = new MSIToken("fake_token", "1/1/2020 0:00:00 PM +00:00");
expected = OffsetDateTime.of(2020, 1, 1, 11, 58, 0, 0, ZoneOffset.UTC);
Assert.assertEquals(expected.toEpochSecond(), token.getExpiresAt().toEpochSecond());
}
}

0 comments on commit 0c76069

Please sign in to comment.