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));
}
}
Aggregations