Search in sources :

Example 21 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class UndertowAuthenticationMechanism method authenticate.

@Override
public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {
    UndertowHttpFacade facade = createFacade(exchange);
    KeycloakDeployment deployment = deploymentContext.resolveDeployment(facade);
    if (!deployment.isConfigured()) {
        return AuthenticationMechanismOutcome.NOT_ATTEMPTED;
    }
    nodesRegistrationManagement.tryRegister(deployment);
    AdapterTokenStore tokenStore = getTokenStore(exchange, facade, deployment, securityContext);
    RequestAuthenticator authenticator = new UndertowRequestAuthenticator(facade, deployment, confidentialPort, securityContext, exchange, tokenStore);
    return keycloakAuthenticate(exchange, securityContext, authenticator);
}
Also used : RequestAuthenticator(org.keycloak.adapters.RequestAuthenticator) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) AdapterTokenStore(org.keycloak.adapters.AdapterTokenStore)

Example 22 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class UndertowAuthenticatedActionsHandler method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    OIDCUndertowHttpFacade facade = new OIDCUndertowHttpFacade(exchange);
    KeycloakDeployment deployment = deploymentContext.resolveDeployment(facade);
    if (deployment != null && deployment.isConfigured()) {
        AuthenticatedActionsHandler handler = new AuthenticatedActionsHandler(deployment, facade);
        if (handler.handledRequest())
            return;
    }
    next.handleRequest(exchange);
}
Also used : AuthenticatedActionsHandler(org.keycloak.adapters.AuthenticatedActionsHandler) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment)

Example 23 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class ElytronCookieTokenStore method isCached.

@Override
public boolean isCached(RequestAuthenticator authenticator) {
    KeycloakDeployment deployment = httpFacade.getDeployment();
    KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = CookieTokenStore.getPrincipalFromCookie(deployment, httpFacade, this);
    if (principal == null) {
        log.debug("Account was not in cookie or was invalid, returning null");
        return false;
    }
    ElytronAccount account = new ElytronAccount(principal);
    if (!deployment.getRealm().equals(account.getKeycloakSecurityContext().getRealm())) {
        log.debug("Account in session belongs to a different realm than for this request.");
        return false;
    }
    boolean active = account.checkActive();
    if (!active) {
        active = account.tryRefresh();
    }
    if (active) {
        log.debug("Cached account found");
        restoreRequest();
        httpFacade.authenticationComplete(account, true);
        return true;
    } else {
        log.debug("Account was not active, removing cookie and returning false");
        CookieTokenStore.removeCookie(deployment, httpFacade);
        return false;
    }
}
Also used : RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment)

Example 24 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class ElytronCookieTokenStore method checkCurrentToken.

@Override
public void checkCurrentToken() {
    KeycloakDeployment deployment = httpFacade.getDeployment();
    KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = CookieTokenStore.getPrincipalFromCookie(deployment, httpFacade, this);
    if (principal == null) {
        return;
    }
    RefreshableKeycloakSecurityContext securityContext = principal.getKeycloakSecurityContext();
    if (securityContext.isActive() && !securityContext.getDeployment().isAlwaysRefreshToken())
        return;
    // FYI: A refresh requires same scope, so same roles will be set.  Otherwise, refresh will fail and token will
    // not be updated
    boolean success = securityContext.refreshExpiredToken(false);
    if (success && securityContext.isActive())
        return;
    saveAccountInfo(new ElytronAccount(principal));
}
Also used : RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment)

Example 25 with KeycloakDeployment

use of org.keycloak.adapters.KeycloakDeployment in project keycloak by keycloak.

the class ElytronCookieTokenStore method logout.

@Override
public void logout(boolean glo) {
    KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = CookieTokenStore.getPrincipalFromCookie(this.httpFacade.getDeployment(), this.httpFacade, this);
    if (principal == null) {
        return;
    }
    CookieTokenStore.removeCookie(this.httpFacade.getDeployment(), this.httpFacade);
    if (glo) {
        KeycloakSecurityContext ksc = (KeycloakSecurityContext) principal.getKeycloakSecurityContext();
        if (ksc == null) {
            return;
        }
        KeycloakDeployment deployment = httpFacade.getDeployment();
        if (!deployment.isBearerOnly() && ksc != null && ksc instanceof RefreshableKeycloakSecurityContext) {
            ((RefreshableKeycloakSecurityContext) ksc).logout(deployment);
        }
    }
}
Also used : RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment)

Aggregations

KeycloakDeployment (org.keycloak.adapters.KeycloakDeployment)69 Test (org.junit.Test)21 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)21 PolicyEnforcer (org.keycloak.adapters.authorization.PolicyEnforcer)20 AuthorizationContext (org.keycloak.AuthorizationContext)16 OIDCHttpFacade (org.keycloak.adapters.OIDCHttpFacade)14 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)12 AdapterDeploymentContext (org.keycloak.adapters.AdapterDeploymentContext)11 OAuthClient (org.keycloak.testsuite.util.OAuthClient)11 AdapterTokenStore (org.keycloak.adapters.AdapterTokenStore)10 InputStream (java.io.InputStream)9 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)9 AuthenticatedActionsHandler (org.keycloak.adapters.AuthenticatedActionsHandler)9 FileInputStream (java.io.FileInputStream)7 FileNotFoundException (java.io.FileNotFoundException)7 HashMap (java.util.HashMap)7 KeycloakConfigResolver (org.keycloak.adapters.KeycloakConfigResolver)6 AuthChallenge (org.keycloak.adapters.spi.AuthChallenge)6 AuthOutcome (org.keycloak.adapters.spi.AuthOutcome)6 List (java.util.List)5