org.apache.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.
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.server.authentication;
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;
import org.apache.gravitino.Config;
import org.apache.gravitino.UserPrincipal;
import org.apache.gravitino.auth.AuthConstants;
import org.apache.gravitino.auth.SignatureAlgorithmFamilyType;
import org.apache.gravitino.exceptions.UnauthorizedException;
/**
* 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 - 2024 Weber Informatics LLC | Privacy Policy