Skip to content
Prev Previous commit
Next Next commit
code refactor
  • Loading branch information
g2vinay committed Sep 3, 2025
commit e49239dd599b7e9edfacaa5234adc9c52ffd4bbf
Original file line number Diff line number Diff line change
Expand Up @@ -480,39 +480,12 @@ private Mono<AccessToken> getAccessTokenFromPowerShell(TokenRequestContext reque
Map<String, String> objectMap = reader.readMap(JsonReader::getString);
String accessToken = objectMap.get("Token");
String time = objectMap.get("ExpiresOn");
OffsetDateTime expiresInstant = null;

if (!CoreUtils.isNullOrEmpty(time)) {
try {
expiresInstant = OffsetDateTime.parse(time).withOffsetSameInstant(ZoneOffset.UTC);
} catch (DateTimeParseException ex) {

final String prefix = "/Date(";
final String suffix = ")/";
if (time.length() > prefix.length() + suffix.length()
&& time.startsWith(prefix)
&& time.endsWith(suffix)) {
String digits = time.substring(prefix.length(), time.length() - suffix.length());
boolean allDigits = true;
for (int i = 0; i < digits.length(); i++) {
if (!Character.isDigit(digits.charAt(i))) {
allDigits = false;
break;
}
}
if (allDigits) {
try {
long epochMs = Long.parseLong(digits);
expiresInstant
= OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMs), ZoneOffset.UTC);
} catch (NumberFormatException ignore) {
// leave expiresInstant null -> handled downstream
}
}
}
}
OffsetDateTime expiresOn = PowerShellUtil.parseExpiresOn(time);
if (expiresOn == null) {
return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options,
new CredentialUnavailableException("Encountered error when deserializing ExpiresOn time from PowerShell response.")));
}
return Mono.just(new AccessToken(accessToken, expiresInstant));
return Mono.just(new AccessToken(accessToken, expiresOn));
} catch (IOException e) {
return Mono.error(LoggingUtil.logCredentialUnavailableException(LOGGER, options,
new CredentialUnavailableException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

package com.azure.identity.implementation.util;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;

import com.azure.core.util.CoreUtils;

/**
* Utility class for powershell auth related ops .
*/
Expand Down Expand Up @@ -48,4 +55,42 @@ public static String getPwshCommand(String tenantId, String scope, String sep) {
+ "}" + sep
+ "$customToken | ConvertTo-Json -Compress";
}

/**
* Parse ExpiresOn returned from PowerShell. Supports ISO timestamps and the .NET "/Date(<ms>)/" form.
*
* @param time the string value returned by PowerShell
* @return parsed OffsetDateTime in UTC or null if unable to parse
*/
public static OffsetDateTime parseExpiresOn(String time) {
if (CoreUtils.isNullOrEmpty(time)) {
return null;
}

// Try ISO first
try {
return OffsetDateTime.parse(time).withOffsetSameInstant(ZoneOffset.UTC);
} catch (DateTimeParseException ignore) {
// fall through to .NET style parsing
}

final String prefix = "/Date(";
final String suffix = ")/";
if (time.length() > prefix.length() + suffix.length()
&& time.startsWith(prefix) && time.endsWith(suffix)) {
String digits = time.substring(prefix.length(), time.length() - suffix.length());
for (int i = 0; i < digits.length(); i++) {
if (!Character.isDigit(digits.charAt(i))) {
return null;
}
}
try {
long epochMs = Long.parseLong(digits);
return OffsetDateTime.ofInstant(Instant.ofEpochMilli(epochMs), ZoneOffset.UTC);
} catch (NumberFormatException ignore) {
return null;
}
}
return null;
}
}
Loading