Skip to content

Commit 1d81ee5

Browse files
authored
Various fixes/improvements (#756)
* Remove x-client-cpu header * Improve behavior when futures time out * Lower log level for silent success * Improve error logging style
1 parent ebdd4fc commit 1d81ee5

File tree

9 files changed

+20
-39
lines changed

9 files changed

+20
-39
lines changed

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AccountsSupplier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ public Set<IAccount> get() {
2626
(clientApplication.clientId());
2727

2828
} catch (Exception ex) {
29-
clientApplication.log.error(
30-
LogHelper.createMessage("Execution of " + this.getClass() + " failed.",
31-
msalRequest.headers().getHeaderCorrelationIdValue()), ex);
29+
clientApplication.log.warn(
30+
LogHelper.createMessage(
31+
String.format("Execution of %s failed: %s", this.getClass(), ex.getMessage()),
32+
msalRequest.headers().getHeaderCorrelationIdValue()));
3233

3334
throw new CompletionException(ex);
3435
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByDeviceCodeFlowSupplier.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
5353

5454
while (getCurrentSystemTimeInSeconds() < expirationTimeInSeconds) {
5555
if (deviceCodeFlowRequest.futureReference().get().isCancelled()) {
56-
throw new InterruptedException("Acquire token Device Code Flow was interrupted");
56+
throw new InterruptedException("Device code flow was cancelled before acquiring a token");
57+
}
58+
if (deviceCodeFlowRequest.futureReference().get().isCompletedExceptionally()) {
59+
throw new InterruptedException("Device code flow had an exception before acquiring a token");
5760
}
5861
try {
5962
return acquireTokenByAuthorisationGrantSupplier.execute();

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenByInteractiveFlowSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private AuthorizationResult getAuthorizationResultFromHttpListener() {
209209
expirationTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) + 1;
210210
}
211211

212-
while (result == null && !interactiveRequest.futureReference().get().isCancelled()) {
212+
while (result == null && !interactiveRequest.futureReference().get().isDone()) {
213213
if (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) > expirationTime) {
214214
LOG.warn(String.format("Listener timed out after %S seconds, no authorization code was returned from the server during that time.", timeFromParameters));
215215
break;

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AcquireTokenSilentSupplier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ AuthenticationResult execute() throws Exception {
109109
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, AuthenticationErrorCode.CACHE_MISS);
110110
}
111111

112-
log.info("Returning token from cache");
112+
log.debug("Returning token from cache");
113113

114114
return res;
115115
}

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/AuthenticationResultSupplier.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ public IAuthenticationResult get() {
9595
msalRequest.requestContext().correlationId(),
9696
error);
9797

98-
logException(ex);
98+
clientApplication.log.warn(
99+
LogHelper.createMessage(
100+
String.format("Execution of %s failed: %s", this.getClass(), ex.getMessage()),
101+
msalRequest.headers().getHeaderCorrelationIdValue()));
102+
99103
throw new CompletionException(ex);
100104
}
101105
}
@@ -135,26 +139,6 @@ private void logResult(AuthenticationResult result, HttpHeaders headers) {
135139
}
136140
}
137141

138-
private void logException(Exception ex) {
139-
140-
String logMessage = LogHelper.createMessage(
141-
"Execution of " + this.getClass() + " failed.",
142-
msalRequest.headers().getHeaderCorrelationIdValue());
143-
144-
if (ex instanceof MsalClientException) {
145-
MsalClientException exception = (MsalClientException) ex;
146-
if (exception.errorCode() != null && exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
147-
clientApplication.log.debug(logMessage, ex);
148-
return;
149-
}
150-
} else if (ex instanceof MsalAzureSDKException) {
151-
clientApplication.log.debug(ex.getMessage(), ex);
152-
return;
153-
}
154-
155-
clientApplication.log.error(logMessage, ex);
156-
}
157-
158142
private ApiEvent initializeApiEvent(MsalRequest msalRequest) {
159143
ApiEvent apiEvent = new ApiEvent(clientApplication.logPii());
160144
msalRequest.requestContext().telemetryRequestId(

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/HttpHeaders.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ final class HttpHeaders {
1616
static final String PRODUCT_VERSION_HEADER_NAME = "x-client-VER";
1717
static final String PRODUCT_VERSION_HEADER_VALUE = getProductVersion();
1818

19-
static final String CPU_HEADER_NAME = "x-client-CPU";
20-
static final String CPU_HEADER_VALUE = System.getProperty("os.arch");
21-
2219
static final String OS_HEADER_NAME = "x-client-OS";
2320
static final String OS_HEADER_VALUE = System.getProperty("os.name");
2421

@@ -78,7 +75,6 @@ private void initializeHeaders(Map<String, String> extraHttpHeaders) {
7875
init.accept(PRODUCT_HEADER_NAME, PRODUCT_HEADER_VALUE);
7976
init.accept(PRODUCT_VERSION_HEADER_NAME, PRODUCT_VERSION_HEADER_VALUE);
8077
init.accept(OS_HEADER_NAME, OS_HEADER_VALUE);
81-
init.accept(CPU_HEADER_NAME, CPU_HEADER_VALUE);
8278
init.accept(REQUEST_CORRELATION_ID_IN_RESPONSE_HEADER_NAME, REQUEST_CORRELATION_ID_IN_RESPONSE_HEADER_VALUE);
8379
init.accept(CORRELATION_ID_HEADER_NAME, this.correlationIdHeaderValue);
8480

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/LogHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
final class LogHelper {
77

88
static String createMessage(String originalMessage, String correlationId) {
9-
return String.format("[Correlation ID: %s] " + originalMessage,
10-
correlationId);
9+
return String.format("[Correlation ID: %s] %s", correlationId, originalMessage);
1110
}
1211

1312
static String getPiiScrubbedDetails(Throwable ex) {

msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/RemoveAccountRunnable.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ public void run() {
2525
(clientApplication.clientId(), account);
2626

2727
} catch (Exception ex) {
28-
clientApplication.log.error(
29-
LogHelper.createMessage("Execution of " + this.getClass() + " failed.",
30-
requestContext.correlationId()), ex);
28+
clientApplication.log.warn(
29+
LogHelper.createMessage(
30+
String.format("Execution of %s failed: %s", this.getClass(), ex.getMessage()),
31+
requestContext.correlationId()));
3132

3233
throw new CompletionException(ex);
3334
}

msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/HttpHeaderTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ void testHttpHeaderConstructor() {
4343
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_HEADER_NAME), HttpHeaders.PRODUCT_HEADER_VALUE);
4444
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_VERSION_HEADER_NAME), HttpHeaders.PRODUCT_VERSION_HEADER_VALUE);
4545
assertEquals(httpHeaderMap.get(HttpHeaders.OS_HEADER_NAME), HttpHeaders.OS_HEADER_VALUE);
46-
assertEquals(httpHeaderMap.get(HttpHeaders.CPU_HEADER_NAME), HttpHeaders.CPU_HEADER_VALUE);
4746
assertEquals(httpHeaderMap.get(HttpHeaders.APPLICATION_NAME_HEADER_NAME), "app-name");
4847
assertEquals(httpHeaderMap.get(HttpHeaders.APPLICATION_VERSION_HEADER_NAME), "app-version");
4948
assertEquals(httpHeaderMap.get(HttpHeaders.CORRELATION_ID_HEADER_NAME), "correlation-id");
@@ -70,7 +69,6 @@ void testHttpHeaderConstructor_valuesNotSet() {
7069
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_HEADER_NAME), HttpHeaders.PRODUCT_HEADER_VALUE);
7170
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_VERSION_HEADER_NAME), HttpHeaders.PRODUCT_VERSION_HEADER_VALUE);
7271
assertEquals(httpHeaderMap.get(HttpHeaders.OS_HEADER_NAME), HttpHeaders.OS_HEADER_VALUE);
73-
assertEquals(httpHeaderMap.get(HttpHeaders.CPU_HEADER_NAME), HttpHeaders.CPU_HEADER_VALUE);
7472
assertNull(httpHeaderMap.get(HttpHeaders.APPLICATION_NAME_HEADER_NAME));
7573
assertNull(httpHeaderMap.get(HttpHeaders.APPLICATION_VERSION_HEADER_NAME));
7674
assertNotNull(httpHeaderMap.get(HttpHeaders.CORRELATION_ID_HEADER_NAME));
@@ -163,7 +161,6 @@ void testHttpHeaderConstructor_extraHttpHeadersOverwriteLibraryHeaders() {
163161
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_HEADER_NAME), HttpHeaders.PRODUCT_HEADER_VALUE);
164162
assertEquals(httpHeaderMap.get(HttpHeaders.PRODUCT_VERSION_HEADER_NAME), HttpHeaders.PRODUCT_VERSION_HEADER_VALUE);
165163
assertEquals(httpHeaderMap.get(HttpHeaders.OS_HEADER_NAME), HttpHeaders.OS_HEADER_VALUE);
166-
assertEquals(httpHeaderMap.get(HttpHeaders.CPU_HEADER_NAME), HttpHeaders.CPU_HEADER_VALUE);
167164
assertEquals(httpHeaderMap.get(HttpHeaders.APPLICATION_VERSION_HEADER_NAME), "app-version");
168165
assertEquals(httpHeaderMap.get(HttpHeaders.CORRELATION_ID_HEADER_NAME), "correlation-id");
169166

0 commit comments

Comments
 (0)