Search in sources :

Example 1 with WebAuthnAuthenticationToken

use of com.webauthn4j.springframework.security.WebAuthnAuthenticationToken in project webauthn4j-spring-security by webauthn4j.

the class WithMockWebAuthnUserSecurityContextFactory method createSecurityContext.

/**
 * Create a {@link SecurityContext} given an Annotation.
 *
 * @param user the {@link WithMockWebAuthnUser} to create the {@link SecurityContext}
 *             from. Cannot be null.
 * @return the {@link SecurityContext} to use. Cannot be null.
 */
@Override
public SecurityContext createSecurityContext(WithMockWebAuthnUser user) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    List<AuthorityEntity> authorities = Arrays.stream(user.authorities()).map((name) -> new AuthorityEntity(null, name)).collect(Collectors.toList());
    List<GroupEntity> groups = Arrays.stream(user.groups()).map(GroupEntity::new).collect(Collectors.toList());
    List<AuthenticatorEntity> authenticatorEntities = Arrays.stream(user.authenticators()).map((name) -> {
        AuthenticatorEntity authenticatorEntity = new AuthenticatorEntity();
        authenticatorEntity.setName(name);
        return authenticatorEntity;
    }).collect(Collectors.toList());
    UserEntity principal = new UserEntity();
    principal.setId(user.id());
    principal.setUserHandle(Base64UrlUtil.decode(user.userHandleBase64Url()));
    principal.setFirstName(user.firstName());
    principal.setLastName(user.lastName());
    principal.setEmailAddress(user.emailAddress());
    principal.setGroups(groups);
    principal.setAuthorities(authorities);
    principal.setAuthenticators(authenticatorEntities);
    principal.setLocked(user.locked());
    WebAuthnAuthenticationRequest request = mock(WebAuthnAuthenticationRequest.class);
    Authentication auth = new WebAuthnAuthenticationToken(principal, request, principal.getAuthorities());
    context.setAuthentication(auth);
    return context;
}
Also used : Arrays(java.util.Arrays) UserEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity) GroupEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity) WithSecurityContextFactory(org.springframework.security.test.context.support.WithSecurityContextFactory) AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity) Base64UrlUtil(com.webauthn4j.util.Base64UrlUtil) Collectors(java.util.stream.Collectors) WebAuthnAuthenticationToken(com.webauthn4j.springframework.security.WebAuthnAuthenticationToken) List(java.util.List) WebAuthnAuthenticationRequest(com.webauthn4j.springframework.security.WebAuthnAuthenticationRequest) SecurityContext(org.springframework.security.core.context.SecurityContext) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Mockito.mock(org.mockito.Mockito.mock) AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity) WebAuthnAuthenticationRequest(com.webauthn4j.springframework.security.WebAuthnAuthenticationRequest) GroupEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UserEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity) WebAuthnAuthenticationToken(com.webauthn4j.springframework.security.WebAuthnAuthenticationToken) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)

Aggregations

WebAuthnAuthenticationRequest (com.webauthn4j.springframework.security.WebAuthnAuthenticationRequest)1 WebAuthnAuthenticationToken (com.webauthn4j.springframework.security.WebAuthnAuthenticationToken)1 AuthenticatorEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity)1 AuthorityEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)1 GroupEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity)1 UserEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity)1 Base64UrlUtil (com.webauthn4j.util.Base64UrlUtil)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Mockito.mock (org.mockito.Mockito.mock)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 SecurityContextHolder (org.springframework.security.core.context.SecurityContextHolder)1 WithSecurityContextFactory (org.springframework.security.test.context.support.WithSecurityContextFactory)1