Search in sources :

Example 1 with KeycloakRole

use of org.keycloak.adapters.springsecurity.account.KeycloakRole in project keycloak by keycloak.

the class SpringSecurityTokenStoreTest method testIsCached.

@Test
public void testIsCached() throws Exception {
    Authentication authentication = new PreAuthenticatedAuthenticationToken("foo", "bar", Collections.singleton(new KeycloakRole("ROLE_FOO")));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    assertFalse(store.isCached(requestAuthenticator));
}
Also used : KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Authentication(org.springframework.security.core.Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Example 2 with KeycloakRole

use of org.keycloak.adapters.springsecurity.account.KeycloakRole in project keycloak by keycloak.

the class SpringSecurityTokenStoreTest method testSaveAccountInfoInvalidAuthenticationType.

@Test(expected = IllegalStateException.class)
public void testSaveAccountInfoInvalidAuthenticationType() throws Exception {
    OidcKeycloakAccount account = new SimpleKeycloakAccount(principal, Collections.singleton("FOO"), keycloakSecurityContext);
    Authentication authentication = new PreAuthenticatedAuthenticationToken("foo", "bar", Collections.singleton(new KeycloakRole("ROLE_FOO")));
    SecurityContextHolder.getContext().setAuthentication(authentication);
    store.saveAccountInfo(account);
}
Also used : OidcKeycloakAccount(org.keycloak.adapters.OidcKeycloakAccount) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Authentication(org.springframework.security.core.Authentication) SimpleKeycloakAccount(org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Example 3 with KeycloakRole

use of org.keycloak.adapters.springsecurity.account.KeycloakRole in project keycloak by keycloak.

the class KeycloakClientRequestFactoryTest method testGetKeycloakSecurityContextInvalidAuthentication.

@Test(expected = IllegalStateException.class)
public void testGetKeycloakSecurityContextInvalidAuthentication() throws Exception {
    SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken("foo", "bar", Collections.singleton(new KeycloakRole("baz"))));
    factory.getKeycloakSecurityContext();
}
Also used : KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Example 4 with KeycloakRole

use of org.keycloak.adapters.springsecurity.account.KeycloakRole 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 5 with KeycloakRole

use of org.keycloak.adapters.springsecurity.account.KeycloakRole in project shinyproxy by openanalytics.

the class KeycloakAuthenticationBackend method keycloakAuthenticationProvider.

protected KeycloakAuthenticationProvider keycloakAuthenticationProvider() {
    return new KeycloakAuthenticationProvider() {

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) super.authenticate(authentication);
            List<GrantedAuthority> auth = token.getAuthorities().stream().map(t -> t.getAuthority().toUpperCase()).map(a -> a.startsWith("ROLE_") ? a : "ROLE_" + a).map(a -> new KeycloakRole(a)).collect(Collectors.toList());
            return new KeycloakAuthenticationToken2(token.getAccount(), auth);
        }
    };
}
Also used : ServletException(javax.servlet.ServletException) SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) HashMap(java.util.HashMap) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) KeycloakPreAuthActionsFilter(org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) Inject(javax.inject.Inject) AuthenticationManagerBuilder(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) RegisterSessionAuthenticationStrategy(org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider) Map(java.util.Map) AdapterDeploymentContextFactoryBean(org.keycloak.adapters.springsecurity.AdapterDeploymentContextFactoryBean) HttpSessionManager(org.keycloak.adapters.springsecurity.management.HttpSessionManager) KeycloakAuthenticationEntryPoint(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationEntryPoint) AuthenticationException(org.springframework.security.core.AuthenticationException) LogoutFilter(org.springframework.security.web.authentication.logout.LogoutFilter) KeycloakConfigResolver(org.keycloak.adapters.KeycloakConfigResolver) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) KeycloakAuthenticationException(org.keycloak.adapters.springsecurity.KeycloakAuthenticationException) Request(org.keycloak.adapters.spi.HttpFacade.Request) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakDeploymentBuilder(org.keycloak.adapters.KeycloakDeploymentBuilder) KeycloakAuthenticationProcessingFilter(org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter) BasicAuthenticationFilter(org.springframework.security.web.authentication.www.BasicAuthenticationFilter) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) Collection(java.util.Collection) AuthenticationEntryPoint(org.springframework.security.web.AuthenticationEntryPoint) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) IAuthenticationBackend(eu.openanalytics.shinyproxy.auth.IAuthenticationBackend) Serializable(java.io.Serializable) GrantedAuthority(org.springframework.security.core.GrantedAuthority) List(java.util.List) Component(org.springframework.stereotype.Component) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) SessionRegistryImpl(org.springframework.security.core.session.SessionRegistryImpl) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Environment(org.springframework.core.env.Environment) Bean(org.springframework.context.annotation.Bean) Authentication(org.springframework.security.core.Authentication) KeycloakLogoutHandler(org.keycloak.adapters.springsecurity.authentication.KeycloakLogoutHandler) AdapterConfig(org.keycloak.representations.adapters.config.AdapterConfig) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider)

Aggregations

KeycloakRole (org.keycloak.adapters.springsecurity.account.KeycloakRole)5 Test (org.junit.Test)3 Authentication (org.springframework.security.core.Authentication)3 KeycloakAuthenticationToken (org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)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 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 ServletException (javax.servlet.ServletException)1 AdapterDeploymentContext (org.keycloak.adapters.AdapterDeploymentContext)1 KeycloakConfigResolver (org.keycloak.adapters.KeycloakConfigResolver)1 KeycloakDeployment (org.keycloak.adapters.KeycloakDeployment)1 KeycloakDeploymentBuilder (org.keycloak.adapters.KeycloakDeploymentBuilder)1