Search in sources :

Example 1 with OAuthBearerIllegalTokenException

use of org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerIllegalTokenException in project kafka by apache.

the class LoginAccessTokenValidator method validate.

/**
 * Accepts an OAuth JWT access token in base-64 encoded format, validates, and returns an
 * OAuthBearerToken.
 *
 * @param accessToken Non-<code>null</code> JWT access token
 * @return {@link OAuthBearerToken}
 * @throws ValidateException Thrown on errors performing validation of given token
 */
@SuppressWarnings("unchecked")
public OAuthBearerToken validate(String accessToken) throws ValidateException {
    SerializedJwt serializedJwt = new SerializedJwt(accessToken);
    Map<String, Object> payload;
    try {
        payload = OAuthBearerUnsecuredJws.toMap(serializedJwt.getPayload());
    } catch (OAuthBearerIllegalTokenException e) {
        throw new ValidateException(String.format("Could not validate the access token: %s", e.getMessage()), e);
    }
    Object scopeRaw = getClaim(payload, scopeClaimName);
    Collection<String> scopeRawCollection;
    if (scopeRaw instanceof String)
        scopeRawCollection = Collections.singletonList((String) scopeRaw);
    else if (scopeRaw instanceof Collection)
        scopeRawCollection = (Collection<String>) scopeRaw;
    else
        scopeRawCollection = Collections.emptySet();
    Number expirationRaw = (Number) getClaim(payload, EXPIRATION_CLAIM_NAME);
    String subRaw = (String) getClaim(payload, subClaimName);
    Number issuedAtRaw = (Number) getClaim(payload, ISSUED_AT_CLAIM_NAME);
    Set<String> scopes = ClaimValidationUtils.validateScopes(scopeClaimName, scopeRawCollection);
    long expiration = ClaimValidationUtils.validateExpiration(EXPIRATION_CLAIM_NAME, expirationRaw != null ? expirationRaw.longValue() * 1000L : null);
    String subject = ClaimValidationUtils.validateSubject(subClaimName, subRaw);
    Long issuedAt = ClaimValidationUtils.validateIssuedAt(ISSUED_AT_CLAIM_NAME, issuedAtRaw != null ? issuedAtRaw.longValue() * 1000L : null);
    OAuthBearerToken token = new BasicOAuthBearerToken(accessToken, scopes, expiration, subject, issuedAt);
    return token;
}
Also used : OAuthBearerToken(org.apache.kafka.common.security.oauthbearer.OAuthBearerToken) OAuthBearerIllegalTokenException(org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerIllegalTokenException) Collection(java.util.Collection)

Aggregations

Collection (java.util.Collection)1 OAuthBearerToken (org.apache.kafka.common.security.oauthbearer.OAuthBearerToken)1 OAuthBearerIllegalTokenException (org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerIllegalTokenException)1