Repository navigation
test(oauth2): add mTLS in-process socket tests and live GCP WIF integration tests #14220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dadeda6
5cc87e0
73c5a0a
8034525
17aae1f
f2d752e
a5e69ed
031a594
fdfea4f
4266196
7ac9e63
13e8be0
44ca6d7
ffe2b6d
08e026b
9313161
6e5f7fe
ddd5ea8
b47ab2a
fb1a9c8
22556ff
849c4e5
1f062f2
d28d8c8
4e06ff0
231e06c
ad119fd
45aa304
d5317c2
f96ff1a
683b765
efa9d9e
091a91b
395e3af
e8300d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ | |
| import com.google.api.client.http.HttpResponseException; | ||
| import com.google.api.client.json.GenericJson; | ||
| import com.google.api.client.json.JsonParser; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import org.jspecify.annotations.NullMarked; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
|
|
@@ -99,20 +99,67 @@ int getHttpStatusCode() { | |
| return httpStatusCode; | ||
| } | ||
|
|
||
| static OAuthException createFromHttpResponseException(HttpResponseException e) | ||
| throws IOException { | ||
| JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser((e).getContent()); | ||
| GenericJson errorResponse = parser.parseAndClose(GenericJson.class); | ||
|
|
||
| String errorCode = (String) errorResponse.get("error"); | ||
| String errorDescription = null; | ||
| String errorUri = null; | ||
| if (errorResponse.containsKey("error_description")) { | ||
| errorDescription = (String) errorResponse.get("error_description"); | ||
| static OAuthException createFromHttpResponseException(HttpResponseException e) { | ||
| String content = e.getContent(); | ||
| if (content == null || content.trim().isEmpty()) { | ||
| OAuthException oauthException = | ||
| new OAuthException( | ||
| "http_error_" + e.getStatusCode(), e.getStatusMessage(), null, e.getStatusCode()); | ||
| oauthException.initCause(e); | ||
| return oauthException; | ||
| } | ||
| if (errorResponse.containsKey("error_uri")) { | ||
| errorUri = (String) errorResponse.get("error_uri"); | ||
| try { | ||
| JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser(content); | ||
| GenericJson errorResponse = parser.parseAndClose(GenericJson.class); | ||
| if (errorResponse == null) { | ||
| OAuthException oauthException = | ||
| new OAuthException( | ||
| "http_error_" + e.getStatusCode(), e.getStatusMessage(), null, e.getStatusCode()); | ||
| oauthException.initCause(e); | ||
| return oauthException; | ||
| } | ||
|
|
||
| String errorCode = null; | ||
| String errorDescription = null; | ||
| Object rawError = errorResponse.get("error"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an explicit if (errorResponse == null) check and added createFromHttpResponseException_jsonLiteralNull() in OAuthExceptionTest. |
||
| if (rawError instanceof String) { | ||
| errorCode = (String) rawError; | ||
| } else if (rawError instanceof Map) { | ||
| Map<?, ?> errorMap = (Map<?, ?>) rawError; | ||
| if (errorMap.get("status") instanceof String) { | ||
| errorCode = (String) errorMap.get("status"); | ||
| } | ||
| if (errorMap.get("message") instanceof String) { | ||
| errorDescription = (String) errorMap.get("message"); | ||
| } | ||
| } | ||
| if (errorCode == null) { | ||
| errorCode = "http_error_" + e.getStatusCode(); | ||
| } | ||
| String errorUri = null; | ||
| if (errorResponse.get("error_description") instanceof String) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a server returns a valid JSON object without "error_description" or a nested "error.message" map (such as {"message": "Certificate expired"} or {}), errorDescription stays null and drops both content and e.getStatusMessage(). Can we fall back when errorDescription == null so the error details are not lost?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
| errorDescription = (String) errorResponse.get("error_description"); | ||
| } else if (errorDescription == null && errorResponse.get("message") instanceof String) { | ||
| errorDescription = (String) errorResponse.get("message"); | ||
| } | ||
| if (errorDescription == null && !(rawError instanceof String)) { | ||
| errorDescription = | ||
| errorResponse.isEmpty() && e.getStatusMessage() != null | ||
| ? e.getStatusMessage() | ||
| : content; | ||
| } | ||
| if (errorResponse.get("error_uri") instanceof String) { | ||
| errorUri = (String) errorResponse.get("error_uri"); | ||
| } | ||
| OAuthException oauthException = | ||
| new OAuthException(errorCode, errorDescription, errorUri, e.getStatusCode()); | ||
| oauthException.initCause(e); | ||
| return oauthException; | ||
| } catch (Exception parseException) { | ||
| OAuthException oauthException = | ||
| new OAuthException("http_error_" + e.getStatusCode(), content, null, e.getStatusCode()); | ||
| oauthException.initCause(e); | ||
| return oauthException; | ||
| } | ||
| return new OAuthException(errorCode, errorDescription, errorUri, e.getStatusCode()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This method catches
Exceptioninternally and returns anOAuthExceptionon every path. We can safely remove thethrows IOExceptiondeclaration and the unused import.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed throws IOException from createFromHttpResponseException and dropped the unused java.io.IOException import.