Skip to content

Commit d96e9de

Browse files
authored
Exception Improvements (#254)
* Add null checks for MsalException error code references * Better exception handling for invalid tokens * Better exception handling for invalid tokens
1 parent c1ff728 commit d96e9de

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private AuthenticationResult acquireTokenWithDeviceCode(DeviceCode deviceCode,
5858
try {
5959
return acquireTokenByAuthorisationGrantSupplier.execute();
6060
} catch (MsalServiceException ex) {
61-
if (ex.errorCode().equals(AUTHORIZATION_PENDING)) {
61+
if (ex.errorCode() != null && ex.errorCode().equals(AUTHORIZATION_PENDING)) {
6262
TimeUnit.SECONDS.sleep(deviceCode.interval());
6363
} else {
6464
throw ex;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public IAuthenticationResult get() {
7171
String error = StringHelper.EMPTY_STRING;
7272
if (ex instanceof MsalException) {
7373
MsalException exception = ((MsalException) ex);
74-
if(exception.errorCode() != null){
74+
if (exception.errorCode() != null){
7575
apiEvent.setApiErrorCode(exception.errorCode());
7676
}
7777
} else {
78-
if(ex.getCause() != null){
78+
if (ex.getCause() != null){
7979
error = ex.getCause().toString();
8080
}
8181
}
@@ -136,7 +136,7 @@ private void logException(Exception ex) {
136136

137137
if (ex instanceof MsalClientException) {
138138
MsalClientException exception = (MsalClientException) ex;
139-
if (exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
139+
if (exception.errorCode() != null && exception.errorCode().equalsIgnoreCase(AuthenticationErrorCode.CACHE_MISS)) {
140140
clientApplication.log.debug(logMessage, ex);
141141
return;
142142
}

src/main/java/com/microsoft/aad/msal4j/TokenResponse.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ static Long getLongValue(JSONObject jsonObject, String key) throws ParseExceptio
5757
static TokenResponse parseJsonObject(final JSONObject jsonObject)
5858
throws ParseException {
5959

60-
final AccessToken accessToken = AccessToken.parse(jsonObject);
61-
final RefreshToken refreshToken = RefreshToken.parse(jsonObject);
62-
6360
// In same cases such as client credentials there isn't an id token. Instead of a null value
6461
// use an empty string in order to avoid an IllegalArgumentException from OIDCTokens.
6562
String idTokenValue = "";
@@ -93,7 +90,16 @@ static TokenResponse parseJsonObject(final JSONObject jsonObject)
9390
foci = JSONObjectUtils.getString(jsonObject, "foci");
9491
}
9592

96-
return new TokenResponse(accessToken, refreshToken,idTokenValue, scopeValue, clientInfo,
97-
expiresIn, ext_expires_in, foci);
93+
try {
94+
final AccessToken accessToken = AccessToken.parse(jsonObject);
95+
final RefreshToken refreshToken = RefreshToken.parse(jsonObject);
96+
return new TokenResponse(accessToken, refreshToken, idTokenValue, scopeValue, clientInfo,
97+
expiresIn, ext_expires_in, foci);
98+
} catch (ParseException e) {
99+
throw new MsalClientException("Invalid or missing token, could not parse. If using B2C, information on a potential B2C issue and workaround can be found here: https://aka.ms/msal4j-b2c-known-issues",
100+
AuthenticationErrorCode.INVALID_JSON);
101+
} catch (Exception e) {
102+
throw new MsalClientException(e);
103+
}
98104
}
99105
}

0 commit comments

Comments
 (0)