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