Skip to content

Commit 580b988

Browse files
tristanexsquarejzheaux
authored andcommitted
Fix NullPointerException
- Caused by a malformed WWW-Authenticate value Closes gh-9364
1 parent acb5ae6 commit 580b988

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,6 +70,9 @@ private OAuth2Error readErrorFromWwwAuthenticate(HttpHeaders headers) {
7070
return null;
7171
}
7272
BearerTokenError bearerTokenError = getBearerToken(wwwAuthenticateHeader);
73+
if (bearerTokenError == null) {
74+
return new OAuth2Error(OAuth2ErrorCodes.SERVER_ERROR, null, null);
75+
}
7376
String errorCode = (bearerTokenError.getCode() != null) ? bearerTokenError.getCode()
7477
: OAuth2ErrorCodes.SERVER_ERROR;
7578
String errorDescription = bearerTokenError.getDescription();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandlerTests.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,4 +58,13 @@ public void handleErrorWhenErrorResponseWwwAuthenticateHeaderThenHandled() {
5858
.withMessage("[insufficient_scope] The access token expired");
5959
}
6060

61+
@Test
62+
public void handleErrorWhenErrorResponseWithInvalidWwwAuthenticateHeaderThenHandled() {
63+
String invalidWwwAuthenticateHeader = "Unauthorized";
64+
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
65+
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, invalidWwwAuthenticateHeader);
66+
assertThatExceptionOfType(OAuth2AuthorizationException.class)
67+
.isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("[server_error] ");
68+
}
69+
6170
}

0 commit comments

Comments
 (0)