Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate token for service account with prefix of 'service-account-apikey' #2456

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public Authentication authenticate(Authentication authentication)
try {
authentication = atlasKeycloakAuthenticationProvider.authenticate(authentication);
} catch (KeycloakAuthenticationException ex) {
// this exception signals that the authentication process for Keycloak has failed
// possibly due to token introspection issues or an inactive client.
throw new AtlasAuthenticationException("Authentication failed.");
} catch (Exception ex) {
LOG.error("Error while Keycloak authentication", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ public Authentication authenticate(Authentication authentication) {
authentication = new KeycloakAuthenticationToken(token.getAccount(), token.isInteractive(), grantedAuthorities);
}
}
if(authentication.getName().startsWith("service-account")) {

// Introspect token online with keycloak server if request is made by service account
if (authentication.getName().startsWith("service-account-apikey")) {
LOG.info("Validating request for clientId: {}", authentication.getName().substring("service-account-".length()));
try{
KeycloakAuthenticationToken keycloakToken = (KeycloakAuthenticationToken)authentication;
try {
KeycloakAuthenticationToken keycloakToken = (KeycloakAuthenticationToken) authentication;
String bearerToken = keycloakToken.getAccount().getKeycloakSecurityContext().getTokenString();
TokenMetadataRepresentation introspectToken = atlasKeycloakClient.introspectToken(bearerToken);
if(Objects.nonNull(introspectToken) && introspectToken.isActive()) {
if (Objects.nonNull(introspectToken) && introspectToken.isActive()) {
authentication.setAuthenticated(true);
} else {
// if the client is inactive throw KeycloakAuthenticationException
handleInvalidApiKey(authentication);
}
} catch (Exception e) {
LOG.error("Keycloak Authentication failed : {}", e.getMessage());
throw new KeycloakAuthenticationException("Keycloak Authentication failed", e.getCause());
}
}
Expand Down
Loading