Search in sources :

Example 1 with InvalidTokenException

use of io.cdap.cdap.security.auth.InvalidTokenException in project cdap by caskdata.

the class InternalAccessEnforcer method validateAccessTokenAndIdentity.

private void validateAccessTokenAndIdentity(String principalName, Credential credential) throws AccessException {
    if (credential == null) {
        throw new IllegalStateException("Attempted to internally enforce access on null credential");
    }
    if (!credential.getType().equals(Credential.CredentialType.INTERNAL)) {
        throw new IllegalStateException("Attempted to internally enforce access on non-internal credential type");
    }
    AccessToken accessToken;
    try {
        accessToken = accessTokenCodec.decode(Base64.getDecoder().decode(credential.getValue()));
    } catch (IOException e) {
        throw new AccessException("Failed to deserialize access token", e);
    }
    try {
        tokenManager.validateSecret(accessToken);
    } catch (InvalidTokenException e) {
        throw new AccessException("Failed to validate access token", e);
    }
    UserIdentity userIdentity = accessToken.getIdentifier();
    if (!userIdentity.getUsername().equals(principalName)) {
        LOG.debug(String.format("Internal access token username differs from principal name; got token " + "name '%s', expected principal name '%s'", userIdentity.getUsername(), principalName));
    }
    if (userIdentity.getIdentifierType() == null || !userIdentity.getIdentifierType().equals(UserIdentity.IdentifierType.INTERNAL)) {
        throw new AccessException(String.format("Invalid internal access token type; got '%s', want '%s'", userIdentity.getIdentifierType(), UserIdentity.IdentifierType.INTERNAL));
    }
}
Also used : InvalidTokenException(io.cdap.cdap.security.auth.InvalidTokenException) AccessException(io.cdap.cdap.api.security.AccessException) AccessToken(io.cdap.cdap.security.auth.AccessToken) UserIdentity(io.cdap.cdap.security.auth.UserIdentity) IOException(java.io.IOException)

Aggregations

AccessException (io.cdap.cdap.api.security.AccessException)1 AccessToken (io.cdap.cdap.security.auth.AccessToken)1 InvalidTokenException (io.cdap.cdap.security.auth.InvalidTokenException)1 UserIdentity (io.cdap.cdap.security.auth.UserIdentity)1 IOException (java.io.IOException)1