-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix oauth2 and jwt authentificaor interference #13662
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
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| package io.trino.server.security.oauth2; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.server.security.AbstractBearerAuthenticator; | ||
| import io.trino.server.security.AuthenticationException; | ||
| import io.trino.server.security.UserMapping; | ||
|
|
@@ -42,6 +43,7 @@ | |
| public class OAuth2Authenticator | ||
| extends AbstractBearerAuthenticator | ||
| { | ||
| private static final Logger log = Logger.get(OAuth2Authenticator.class); | ||
| private final OAuth2Client client; | ||
| private final String principalField; | ||
| private final Optional<String> groupsField; | ||
|
|
@@ -64,7 +66,12 @@ public OAuth2Authenticator(OAuth2Client client, OAuth2Config config, TokenRefres | |
| protected Optional<Identity> createIdentity(String token) | ||
| throws UserMappingException | ||
| { | ||
| TokenPair tokenPair = tokenPairSerializer.deserialize(token); | ||
| Optional<TokenPair> deserializeToken = deserializeToken(token); | ||
| if (deserializeToken.isEmpty()) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| TokenPair tokenPair = deserializeToken.get(); | ||
| if (tokenPair.getExpiration().before(Date.from(Instant.now()))) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
@@ -80,11 +87,22 @@ protected Optional<Identity> createIdentity(String token) | |
| return Optional.of(builder.build()); | ||
| } | ||
|
|
||
| private Optional<TokenPair> deserializeToken(String token) | ||
| { | ||
| try { | ||
| return Optional.of(tokenPairSerializer.deserialize(token)); | ||
| } | ||
| catch (RuntimeException ex) { | ||
|
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. Do we want to just catch
Member
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. Can we narrow down the catching ? What if the token fails because it is damaged or expired ?
Member
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. Ok so this catch is so wide on purpose. Right now there are plenty of reasons why token deserialization might fail. We might be having invalid token (on purpose - like in this bug scenario), token might have expired - meaning that both access token and refresh token are either expired or soon to be expired, encryption keys might have been rolled out (after restart) etc. All of the above suggests that we need to discard the token, but we should not request any specific handling of these errors. Rather I think we should just discard this token and issue another challenge to the client - so business as usual. I could narrow catching to IllegalArgumentException (with allowing the possibility that some other valid case might not be handled atm), but then I would like to add this to official contract of the TokenPairSerializer interface as a throws. Catching implementation specific exceptions is not a best design decision.
Praveen2112 marked this conversation as resolved.
Outdated
|
||
| log.debug(ex, "Failed to deserialize token"); | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected AuthenticationException needAuthentication(ContainerRequestContext request, Optional<String> currentToken, String message) | ||
| { | ||
| return currentToken | ||
| .map(tokenPairSerializer::deserialize) | ||
| .flatMap(this::deserializeToken) | ||
| .flatMap(tokenRefresher::refreshToken) | ||
| .map(refreshId -> request.getUriInfo().getBaseUri().resolve(getTokenUri(refreshId))) | ||
| .map(tokenUri -> new AuthenticationException(message, format("Bearer x_token_server=\"%s\"", tokenUri))) | ||
|
|
||
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.
There is a typo in the commit message.
Can you please explain why in the commit message too?