-
Notifications
You must be signed in to change notification settings - Fork 273
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
fix: add signature verification to IdTokenVerifier #861
Conversation
...oauth-client/src/test/java/com/google/api/client/auth/openidconnect/IdTokenVerifierTest.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
Lint is failing:
|
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
return (issuers == null || idToken.verifyIssuer(issuers)) | ||
&& (audience == null || idToken.verifyAudience(audience)) | ||
&& idToken.verifyTime(clock.currentTimeMillis(), acceptableTimeSkewSeconds); | ||
public boolean verify(IdToken idToken) throws VerificationException { |
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.
Would you improve the Javadoc that explains the newly-added case?
Creating a new function IdToken.verifySignature()
will fit better in the implementation.
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.
I'm surprised the breaking change detector doesn't see adding a checked exception as a breaking change.
It also feels a bit weird to have 2 different mechanisms for indicating an invalid token. In the google-auth-library implementation, we only throw VerificationException
s (to be able to communicate why it failed). This legacy implementation already is hiding why the token in invalid.
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.
Another option, in line with Tomo's suggestion is to add a new verifySignature
method that throws VerificationException
. verify
could call verifySignature
and catch the exception and return false
(to not change the interface)
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.
Good catch, thanks, especially the exception :)
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
...oauth-client/src/test/java/com/google/api/client/auth/openidconnect/IdTokenVerifierTest.java
Show resolved
Hide resolved
jwks = response.parseAs(JsonWebKeySet.class); | ||
} catch (IOException io) { | ||
return ImmutableMap.of(); |
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.
This doesn't show any error or exceptions when the certificate doesn't have the expected format. Is there.a good way to report the problem?
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.
The suggestion definitely makes sense, I left it as is since it looks as intentional. I tried to figure out the background for this and it looks like - it is related to LoadingCache implementation, like unable to throw checked exception from runner or throwing RuntimeExceptions etc. My call is to leave this specific one as is to avoid gotchas because:
- it does not seem to be a pain point for other library
- we want to error on the side of reliability at the moment, we can improve experience later
- library is already officially deprecated
@chingor13 are you ok with that?
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.
How about logging (java.util.logging.Logger
) with warn level?
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/Environment.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
this.publicKeyCache = | ||
CacheBuilder.newBuilder() | ||
.expireAfterWrite(1, TimeUnit.HOURS) | ||
.build(new PublicKeyLoader(builder.httpTransportFactory)); |
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.
httpTransportFactory
is now required or you'll get a NPE later. We generally prefer to use preconditions to throw the NPE early for required attributes (using guava's Preconditions.checkNotNull
)
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.
Good catch, thanks, I think here we want to set a default ourselves in case of null, otherwise it is going to be a breaking change
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
return (issuers == null || idToken.verifyIssuer(issuers)) | ||
&& (audience == null || idToken.verifyAudience(audience)) | ||
&& idToken.verifyTime(clock.currentTimeMillis(), acceptableTimeSkewSeconds); | ||
public boolean verify(IdToken idToken) throws VerificationException { |
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.
I'm surprised the breaking change detector doesn't see adding a checked exception as a breaking change.
It also feels a bit weird to have 2 different mechanisms for indicating an invalid token. In the google-auth-library implementation, we only throw VerificationException
s (to be able to communicate why it failed). This legacy implementation already is hiding why the token in invalid.
return (issuers == null || idToken.verifyIssuer(issuers)) | ||
&& (audience == null || idToken.verifyAudience(audience)) | ||
&& idToken.verifyTime(clock.currentTimeMillis(), acceptableTimeSkewSeconds); | ||
public boolean verify(IdToken idToken) throws VerificationException { |
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.
Another option, in line with Tomo's suggestion is to add a new verifySignature
method that throws VerificationException
. verify
could call verifySignature
and catch the exception and return false
(to not change the interface)
…penidconnect/IdTokenVerifier.java Co-authored-by: Tomo Suzuki <[email protected]>
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
The failing lint check is addressed in #863 (merged) |
…penidconnect/IdTokenVerifier.java Co-authored-by: Tomo Suzuki <[email protected]>
…penidconnect/IdTokenVerifier.java Co-authored-by: Tomo Suzuki <[email protected]>
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Show resolved
Hide resolved
google-oauth-client/src/main/java/com/google/api/client/auth/openidconnect/IdTokenVerifier.java
Outdated
Show resolved
Hide resolved
…penidconnect/IdTokenVerifier.java Co-authored-by: Tomo Suzuki <[email protected]>
@chingor13 PTAL |
…ogleapis#861) * previously missing signature validation ported from google-auth-library-java * test cases Co-authored-by: Tomo Suzuki <[email protected]> (cherry picked from commit 22419d6)
…ogleapis#861) * previously missing signature validation ported from google-auth-library-java * test cases Co-authored-by: Tomo Suzuki <[email protected]> (cherry picked from commit 22419d6)
…ogleapis#861) * previously missing signature validation ported from google-auth-library-java * test cases Co-authored-by: Tomo Suzuki <[email protected]> (cherry picked from commit 22419d6)
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.
Nice
Porting the functionality from google-auth-library-java, see https://github.com/googleapis/google-auth-library-java/blob/1434839e704621dc21f902c81cb67e66f7ef1a05/oauth2_http/java/com/google/auth/oauth2/TokenVerifier.java#L81
Fixes #786 ☕️