Skip to content
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

Correct assumption that OIDC access token is always a JWT #8250

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
*/
package io.trino.server.security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
import io.trino.spi.security.BasicPrincipal;
import io.trino.spi.security.Identity;

import javax.ws.rs.container.ContainerRequestContext;

import java.security.Principal;
import java.util.List;
import java.util.Optional;

import static com.google.common.net.HttpHeaders.AUTHORIZATION;
import static java.lang.String.format;
Expand All @@ -30,12 +29,10 @@
public abstract class AbstractBearerAuthenticator
implements Authenticator
{
private final String principalField;
private final UserMapping userMapping;

protected AbstractBearerAuthenticator(String principalField, UserMapping userMapping)
protected AbstractBearerAuthenticator(UserMapping userMapping)
{
this.principalField = requireNonNull(principalField, "principalField is null");
this.userMapping = requireNonNull(userMapping, "userMapping is null");
}

Expand All @@ -50,14 +47,14 @@ public Identity authenticate(ContainerRequestContext request, String token)
throws AuthenticationException
{
try {
Jws<Claims> claimsJws = parseClaimsJws(token);
String principal = claimsJws.getBody().get(principalField, String.class);
if (principal == null) {
Optional<Principal> principal = extractPrincipalFromToken(token);
if (principal.isEmpty()) {
throw needAuthentication(request, "Invalid credentials");
}
String authenticatedUser = userMapping.mapUser(principal);

String authenticatedUser = userMapping.mapUser(principal.get().getName());
return Identity.forUser(authenticatedUser)
.withPrincipal(new BasicPrincipal(principal))
.withPrincipal(principal.get())
.build();
}
catch (JwtException | UserMappingException e) {
Expand Down Expand Up @@ -91,7 +88,7 @@ public String extractToken(ContainerRequestContext request)
return token;
}

protected abstract Jws<Claims> parseClaimsJws(String jws);
protected abstract Optional<Principal> extractPrincipalFromToken(String token);

protected abstract AuthenticationException needAuthentication(ContainerRequestContext request, String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,32 @@
*/
package io.trino.server.security.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SigningKeyResolver;
import io.trino.server.security.AbstractBearerAuthenticator;
import io.trino.server.security.AuthenticationException;
import io.trino.spi.security.BasicPrincipal;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;

import java.security.Principal;
import java.util.Optional;

import static io.trino.server.security.UserMapping.createUserMapping;

public class JwtAuthenticator
extends AbstractBearerAuthenticator
{
private final JwtParser jwtParser;
private final String principalField;

@Inject
public JwtAuthenticator(JwtAuthenticatorConfig config, SigningKeyResolver signingKeyResolver)
{
super(config.getPrincipalField(), createUserMapping(config.getUserMappingPattern(), config.getUserMappingFile()));
super(createUserMapping(config.getUserMappingPattern(), config.getUserMappingFile()));
principalField = config.getPrincipalField();

JwtParser jwtParser = Jwts.parser()
.setSigningKeyResolver(signingKeyResolver);
Expand All @@ -49,9 +53,12 @@ public JwtAuthenticator(JwtAuthenticatorConfig config, SigningKeyResolver signin
}

@Override
protected Jws<Claims> parseClaimsJws(String jws)
protected Optional<Principal> extractPrincipalFromToken(String token)
{
return jwtParser.parseClaimsJws(jws);
return Optional.ofNullable(jwtParser.parseClaimsJws(token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that moving jwtParser.. part to the next line would create an additional indent and thus improve readability slightly.

.getBody()
.get(principalField, String.class))
.map(BasicPrincipal::new);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@
*/
package io.trino.server.security.oauth2;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.trino.server.security.AbstractBearerAuthenticator;
import io.trino.server.security.AuthenticationException;
import io.trino.spi.security.BasicPrincipal;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;

import java.net.URI;
import java.security.Principal;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

import static io.trino.server.security.UserMapping.createUserMapping;
Expand All @@ -34,18 +36,26 @@ public class OAuth2Authenticator
extends AbstractBearerAuthenticator
{
private final OAuth2Service service;
private final String principalField;

@Inject
public OAuth2Authenticator(OAuth2Service service, OAuth2Config config)
{
super(config.getPrincipalField(), createUserMapping(config.getUserMappingPattern(), config.getUserMappingFile()));
super(createUserMapping(config.getUserMappingPattern(), config.getUserMappingFile()));
this.service = requireNonNull(service, "service is null");
this.principalField = config.getPrincipalField();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant this

}

@Override
protected Jws<Claims> parseClaimsJws(String jws)
protected Optional<Principal> extractPrincipalFromToken(String token)
{
return service.parseClaimsJws(jws);
Map<String, Object> claims = service.convertTokenToClaims(token);
if (claims.containsKey(principalField)) {
return Optional.ofNullable(claims.get(principalField))
.map(String.class::cast)
.map(BasicPrincipal::new);
}
return Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.common.io.BaseEncoding;
import com.google.common.io.Resources;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SigningKeyResolver;
Expand All @@ -34,6 +33,7 @@
import java.time.Instant;
import java.time.temporal.TemporalAmount;
import java.util.Date;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -177,11 +177,12 @@ private Claims parseState(String state)
}
}

public Jws<Claims> parseClaimsJws(String token)
public Map<String, Object> convertTokenToClaims(String token)
{
return Jwts.parser()
.setSigningKeyResolver(signingKeyResolver)
.parseClaimsJws(token);
.parseClaimsJws(token)
.getBody();
}

public String getSuccessHtml()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import com.google.common.collect.ImmutableSet;
import io.airlift.log.Logger;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtException;
import io.trino.server.security.UserMapping;
import io.trino.server.security.UserMappingException;
Expand All @@ -33,6 +31,7 @@
import javax.ws.rs.core.Response.ResponseBuilder;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.base.MoreObjects.firstNonNull;
Expand Down Expand Up @@ -87,7 +86,7 @@ public void filter(ContainerRequestContext request)
request.abortWith(Response.seeOther(DISABLED_LOCATION_URI).build());
return;
}
Optional<Claims> claims = getAccessToken(request).map(Jws::getBody);
Optional<Map<String, Object>> claims = getAccessToken(request);
if (claims.isEmpty()) {
needAuthentication(request);
return;
Expand Down Expand Up @@ -115,12 +114,12 @@ public void filter(ContainerRequestContext request)
}
}

private Optional<Jws<Claims>> getAccessToken(ContainerRequestContext request)
private Optional<Map<String, Object>> getAccessToken(ContainerRequestContext request)
{
return OAuthWebUiCookie.read(request.getCookies().get(OAUTH2_COOKIE))
.flatMap(token -> {
try {
return Optional.ofNullable(service.parseClaimsJws(token));
return Optional.ofNullable(service.convertTokenToClaims(token));
}
catch (JwtException | IllegalArgumentException e) {
LOG.debug("Unable to parse JWT token: " + e.getMessage(), e);
Expand Down