Search in sources :

Example 1 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken in project keycloak by keycloak.

the class KeycloakAuthenticationProcessingFilterTest method testSuccessfulAuthenticationBasicAuth.

@Test
public void testSuccessfulAuthenticationBasicAuth() throws Exception {
    Authentication authentication = new KeycloakAuthenticationToken(keycloakAccount, false, authorities);
    this.setBasicAuthHeader(request);
    filter.successfulAuthentication(request, response, chain, authentication);
    verify(chain).doFilter(eq(request), eq(response));
    verify(successHandler, never()).onAuthenticationSuccess(any(HttpServletRequest.class), any(HttpServletResponse.class), any(Authentication.class));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) Authentication(org.springframework.security.core.Authentication) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 2 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken in project keycloak by keycloak.

the class KeycloakAuthenticationProviderTest method setUp.

@Before
public void setUp() throws Exception {
    Principal principal = mock(Principal.class);
    RefreshableKeycloakSecurityContext securityContext = mock(RefreshableKeycloakSecurityContext.class);
    KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, securityContext);
    token = new KeycloakAuthenticationToken(account, false);
    interactiveToken = new KeycloakAuthenticationToken(account, true);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) Principal(java.security.Principal) Before(org.junit.Before)

Example 3 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken in project keycloak by keycloak.

the class SimpleHttpFacadeTest method setup.

@Before
public void setup() {
    SecurityContext springSecurityContext = SecurityContextHolder.createEmptyContext();
    SecurityContextHolder.setContext(springSecurityContext);
    Set<String> roles = Sets.newSet("user");
    Principal principal = mock(Principal.class);
    RefreshableKeycloakSecurityContext keycloakSecurityContext = mock(RefreshableKeycloakSecurityContext.class);
    KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, keycloakSecurityContext);
    KeycloakAuthenticationToken token = new KeycloakAuthenticationToken(account, false);
    springSecurityContext.setAuthentication(token);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) Principal(java.security.Principal) Before(org.junit.Before)

Example 4 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken in project keycloak by keycloak.

the class KeycloakAuthenticationProcessingFilter method successfulAuthentication.

@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
    if (authResult instanceof KeycloakAuthenticationToken && ((KeycloakAuthenticationToken) authResult).isInteractive()) {
        super.successfulAuthentication(request, response, chain, authResult);
        return;
    }
    if (log.isDebugEnabled()) {
        log.debug("Authentication success using bearer token/basic authentication. Updating SecurityContextHolder to contain: {}", authResult);
    }
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(authResult);
    SecurityContextHolder.setContext(context);
    try {
        // Fire event
        if (this.eventPublisher != null) {
            eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
        }
        chain.doFilter(request, response);
    } finally {
        SecurityContextHolder.clearContext();
    }
}
Also used : InteractiveAuthenticationSuccessEvent(org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) SecurityContext(org.springframework.security.core.context.SecurityContext)

Example 5 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken in project keycloak by keycloak.

the class SpringSecurityRequestAuthenticator method completeBearerAuthentication.

@Override
protected void completeBearerAuthentication(KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal, String method) {
    RefreshableKeycloakSecurityContext securityContext = principal.getKeycloakSecurityContext();
    Set<String> roles = AdapterUtils.getRolesFromSecurityContext(securityContext);
    final KeycloakAccount account = new SimpleKeycloakAccount(principal, roles, securityContext);
    logger.debug("Completing bearer authentication. Bearer roles: {} ", roles);
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    context.setAuthentication(new KeycloakAuthenticationToken(account, false));
    SecurityContextHolder.setContext(context);
    request.setAttribute(KeycloakSecurityContext.class.getName(), securityContext);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) RefreshableKeycloakSecurityContext(org.keycloak.adapters.RefreshableKeycloakSecurityContext) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) SecurityContext(org.springframework.security.core.context.SecurityContext) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) OidcKeycloakAccount(org.keycloak.adapters.OidcKeycloakAccount) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount)

Aggregations

KeycloakAuthenticationToken (org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken)11 Authentication (org.springframework.security.core.Authentication)5 KeycloakAccount (org.keycloak.adapters.spi.KeycloakAccount)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Before (org.junit.Before)3 Test (org.junit.Test)3 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)3 SimpleKeycloakAccount (org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 Principal (java.security.Principal)2 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)2 KeycloakRole (org.keycloak.adapters.springsecurity.account.KeycloakRole)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 IAuthenticationBackend (eu.openanalytics.shinyproxy.auth.IAuthenticationBackend)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1