Search in sources :

Example 6 with KeycloakAuthenticationToken

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

the class KeycloakLogoutHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    keycloakAuthenticationToken = mock(KeycloakAuthenticationToken.class);
    keycloakLogoutHandler = new KeycloakLogoutHandler(adapterDeploymentContext);
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    when(adapterDeploymentContext.resolveDeployment(any(HttpFacade.class))).thenReturn(keycloakDeployment);
    when(keycloakAuthenticationToken.getAccount()).thenReturn(keycloakAccount);
    when(keycloakAccount.getKeycloakSecurityContext()).thenReturn(session);
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpFacade(org.keycloak.adapters.spi.HttpFacade) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Before(org.junit.Before)

Example 7 with KeycloakAuthenticationToken

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

the class KeycloakClientRequestFactory method getKeycloakSecurityContext.

/**
 * Returns the {@link KeycloakSecurityContext} from the Spring {@link SecurityContextHolder}'s {@link Authentication}.
 *
 * @return the current <code>KeycloakSecurityContext</code>
 */
protected KeycloakSecurityContext getKeycloakSecurityContext() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    KeycloakAuthenticationToken token;
    KeycloakSecurityContext context;
    if (authentication == null) {
        throw new IllegalStateException("Cannot set authorization header because there is no authenticated principal");
    }
    if (!KeycloakAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
        throw new IllegalStateException(String.format("Cannot set authorization header because Authentication is of type %s but %s is required", authentication.getClass(), KeycloakAuthenticationToken.class));
    }
    token = (KeycloakAuthenticationToken) authentication;
    context = token.getAccount().getKeycloakSecurityContext();
    return context;
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakSecurityContext(org.keycloak.KeycloakSecurityContext) Authentication(org.springframework.security.core.Authentication)

Example 8 with KeycloakAuthenticationToken

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

the class KeycloakAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) authentication;
    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    for (String role : token.getAccount().getRoles()) {
        grantedAuthorities.add(new KeycloakRole(role));
    }
    return new KeycloakAuthenticationToken(token.getAccount(), token.isInteractive(), mapAuthorities(grantedAuthorities));
}
Also used : KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList)

Example 9 with KeycloakAuthenticationToken

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

the class KeycloakAuthenticationProcessingFilterTest method testSuccessfulAuthenticationInteractive.

@Test
public void testSuccessfulAuthenticationInteractive() throws Exception {
    request.setRequestURI("http://host" + KeycloakAuthenticationEntryPoint.DEFAULT_LOGIN_URI + "?query");
    Authentication authentication = new KeycloakAuthenticationToken(keycloakAccount, true, authorities);
    filter.successfulAuthentication(request, response, chain, authentication);
    verify(successHandler).onAuthenticationSuccess(eq(request), eq(response), eq(authentication));
    verify(chain, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.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 10 with KeycloakAuthenticationToken

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

the class KeycloakAuthenticationProcessingFilterTest method testSuccessfulAuthenticationBearer.

@Test
public void testSuccessfulAuthenticationBearer() throws Exception {
    Authentication authentication = new KeycloakAuthenticationToken(keycloakAccount, false, authorities);
    this.setBearerAuthHeader(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)

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