Skip to content

Commit

Permalink
Add expires_on support in Az cli cred flow (Azure#38406)
Browse files Browse the repository at this point in the history
  • Loading branch information
g2vinay authored Feb 6, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 2609aa5 commit bed5cc5
Showing 2 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -120,6 +120,9 @@ public abstract class IdentityClientBase {
private static final String SDK_NAME = "name";
private static final String SDK_VERSION = "version";
private static final ClientOptions DEFAULT_CLIENT_OPTIONS = new ClientOptions();

private static final OffsetDateTime EPOCH = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

private final Map<String, String> properties;


@@ -640,13 +643,10 @@ AccessToken getTokenFromAzureCLIAuthentication(StringBuilder azCommand) {
Map<String, String> objectMap = SERIALIZER_ADAPTER.deserialize(processOutput, Map.class,
SerializerEncoding.JSON);
String accessToken = objectMap.get("accessToken");
String time = objectMap.get("expiresOn");
String timeToSecond = time.substring(0, time.indexOf("."));
String timeJoinedWithT = String.join("T", timeToSecond.split(" "));
OffsetDateTime expiresOn = LocalDateTime.parse(timeJoinedWithT, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.atZone(ZoneId.systemDefault())
.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
token = new AccessToken(accessToken, expiresOn);
OffsetDateTime tokenExpiry;

tokenExpiry = getTokenExpiryOffsetDateTime(objectMap);
token = new AccessToken(accessToken, tokenExpiry);
} catch (IOException | InterruptedException e) {
IllegalStateException ex = new IllegalStateException(redactInfo(e.getMessage()));
ex.setStackTrace(e.getStackTrace());
@@ -655,6 +655,28 @@ AccessToken getTokenFromAzureCLIAuthentication(StringBuilder azCommand) {
return token;
}

static OffsetDateTime getTokenExpiryOffsetDateTime(Map<String, String> objectMap) {
OffsetDateTime tokenExpiry;
if (objectMap.containsKey("expires_on")) {
Long seconds = Long.parseLong(objectMap.get("expires_on"));
tokenExpiry = EPOCH.plusSeconds(seconds);
} else {
String time = objectMap.get("expiresOn");
tokenExpiry = parseExpiresOnTime(time);
}
return tokenExpiry;
}

static OffsetDateTime parseExpiresOnTime(String time) {
OffsetDateTime tokenExpiry;
String timeToSecond = time.substring(0, time.indexOf("."));
String timeJoinedWithT = String.join("T", timeToSecond.split(" "));
tokenExpiry = LocalDateTime.parse(timeJoinedWithT, DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.atZone(ZoneId.systemDefault())
.toOffsetDateTime().withOffsetSameInstant(ZoneOffset.UTC);
return tokenExpiry;
}

AccessToken getTokenFromAzureDeveloperCLIAuthentication(StringBuilder azdCommand) {
AccessToken token;
try {
Original file line number Diff line number Diff line change
@@ -47,6 +47,8 @@
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

@@ -89,8 +91,24 @@ public void testValidSecret() {
})
.verifyComplete();
});
}


@Test
public void testExpiresOnParsingAzureCli() {
// setup
Map<String, String> tokenDetails = new HashMap<>();

//Epoch equivalent of "2023-10-31 21:59:10.000000" is 1698814750;
String expiresOn = "2023-10-31 21:59:10.000000";
tokenDetails.put("expiresOn", expiresOn);
OffsetDateTime offsetDateTime = IdentityClientBase.getTokenExpiryOffsetDateTime(tokenDetails);
Assertions.assertEquals(offsetDateTime.toEpochSecond(),
IdentityClientBase.parseExpiresOnTime(expiresOn).toEpochSecond());

// Test the scenario with expires_on present, it should be given priority.
tokenDetails.put("expires_on", "1572371520");
offsetDateTime = IdentityClientBase.getTokenExpiryOffsetDateTime(tokenDetails);
Assertions.assertEquals(offsetDateTime.toEpochSecond(), 1572371520);
}

@Test

0 comments on commit bed5cc5

Please sign in to comment.