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);
}
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;
}
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));
}
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));
}
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));
}
Aggregations