com.datastrato.gravitino.server.authentication.OAuth2TokenAuthenticator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-common Show documentation
Show all versions of server-common Show documentation
Gravitino is a high-performance, geo-distributed and federated metadata lake.
/*
* Copyright 2023 Datastrato Pvt Ltd.
* This software is licensed under the Apache License version 2.
*/
package com.datastrato.gravitino.server.authentication;
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.UserPrincipal;
import com.datastrato.gravitino.auth.AuthConstants;
import com.datastrato.gravitino.auth.SignatureAlgorithmFamilyType;
import com.datastrato.gravitino.exceptions.UnauthorizedException;
import com.google.common.base.Preconditions;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.KeyFactory;
import java.security.Principal;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
/**
* OAuth2TokenAuthenticator provides the OAuth 2.0 authentication mechanism.
* OAuth2TokenAuthenticator only supports to validate the format of JWT's Bearer Token.
*/
class OAuth2TokenAuthenticator implements Authenticator {
private long allowSkewSeconds;
private Key defaultSigningKey;
private String serviceAudience;
@Override
public boolean isDataFromToken() {
return true;
}
@Override
public Principal authenticateToken(byte[] tokenData) {
if (tokenData == null) {
throw new UnauthorizedException("Empty token authorization header");
}
String authData = new String(tokenData, StandardCharsets.UTF_8);
if (StringUtils.isBlank(authData)
|| !authData.startsWith(AuthConstants.AUTHORIZATION_BEARER_HEADER)) {
throw new UnauthorizedException("Invalid token authorization header");
}
String token = authData.substring(AuthConstants.AUTHORIZATION_BEARER_HEADER.length());
if (StringUtils.isBlank(token)) {
throw new UnauthorizedException("Blank token found");
}
// TODO: If we support multiple OAuth 2.0 servers, we should use multiple
// signing keys.
try {
JwtParser parser =
Jwts.parserBuilder()
.setAllowedClockSkewSeconds(allowSkewSeconds)
.setSigningKey(defaultSigningKey)
.build();
Jwt, Claims> jwt = parser.parseClaimsJws(token);
Object audienceObject = jwt.getBody().get(Claims.AUDIENCE);
if (audienceObject == null) {
throw new UnauthorizedException("Found null Audience in token");
}
if (audienceObject instanceof String) {
if (!serviceAudience.equals(audienceObject)) {
throw new UnauthorizedException(
"Audience in the token [%s] doesn't contain %s", audienceObject, serviceAudience);
}
} else if (audienceObject instanceof List) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy