use of org.jboss.pnc.bacon.auth.KeycloakClientException in project bacon by project-ncl.
the class PncClientHelper method getBearerToken.
/**
* Return null if it couldn't get the authentication token. This generally means that the credentials are not valid
*
* @param keycloakConfig the keycloak config
* @return the token, or null if we couldn't get it
*/
private static String getBearerToken(KeycloakConfig keycloakConfig) {
log.debug("Authenticating to keycloak");
try {
KeycloakClient client = new DirectKeycloakClientImpl();
Credential credential;
if (keycloakConfig.isServiceAccount()) {
credential = client.getCredentialServiceAccount(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getUsername(), keycloakConfig.getClientSecret());
} else {
credential = client.getCredential(keycloakConfig.getUrl(), keycloakConfig.getRealm(), keycloakConfig.getClientId(), keycloakConfig.getUsername());
}
return credential.getAccessToken();
} catch (KeycloakClientException e) {
throw new FatalException("Keycloak authentication failed!", e);
}
}
Aggregations