Search in sources :

Example 1 with KeycloakClientException

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);
    }
}
Also used : KeycloakClientException(org.jboss.pnc.bacon.auth.KeycloakClientException) Credential(org.jboss.pnc.bacon.auth.model.Credential) FatalException(org.jboss.pnc.bacon.common.exception.FatalException) KeycloakClient(org.jboss.pnc.bacon.auth.spi.KeycloakClient) DirectKeycloakClientImpl(org.jboss.pnc.bacon.auth.DirectKeycloakClientImpl)

Aggregations

DirectKeycloakClientImpl (org.jboss.pnc.bacon.auth.DirectKeycloakClientImpl)1 KeycloakClientException (org.jboss.pnc.bacon.auth.KeycloakClientException)1 Credential (org.jboss.pnc.bacon.auth.model.Credential)1 KeycloakClient (org.jboss.pnc.bacon.auth.spi.KeycloakClient)1 FatalException (org.jboss.pnc.bacon.common.exception.FatalException)1