Search in sources :

Example 1 with AuthenticatorEntity

use of com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity in project webauthn4j-spring-security by webauthn4j.

the class AppSpecificMapper method mapForCreate.

private AuthenticatorEntity mapForCreate(AuthenticatorForm authenticatorForm) {
    AuthenticatorEntity authenticatorEntity = new AuthenticatorEntity();
    authenticatorEntity.setName(authenticatorForm.getName());
    authenticatorEntity.setAttestationStatement(authenticatorForm.getAttestationObject().getAttestationObject().getAttestationStatement());
    authenticatorEntity.setAttestedCredentialData(authenticatorForm.getAttestationObject().getAttestationObject().getAuthenticatorData().getAttestedCredentialData());
    return authenticatorEntity;
}
Also used : AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity)

Example 2 with AuthenticatorEntity

use of com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity in project webauthn4j-spring-security by webauthn4j.

the class AppSpecificMapper method mapToAuthenticatorListForUpdate.

private List<AuthenticatorEntity> mapToAuthenticatorListForUpdate(List<AuthenticatorForm> authenticatorForms, List<AuthenticatorEntity> authenticatorEntities) {
    int[] sortedKeptIds = authenticatorForms.stream().filter(authenticator -> authenticator.getId() != null).mapToInt(AuthenticatorForm::getId).sorted().toArray();
    for (AuthenticatorForm authenticatorForm : authenticatorForms) {
        Integer id = authenticatorForm.getId();
        // addExtension new authenticator
        if (id == null) {
            authenticatorEntities.add(mapForCreate(authenticatorForm));
        } else // update existing authenticator
        {
            AuthenticatorEntity correspondingAuthenticatorEntity = authenticatorEntities.stream().filter(item -> item.getId().equals(id)).findFirst().orElseThrow(() -> new WebAuthnSampleEntityNotFoundException("Corresponding authenticator is not found."));
            mapForUpdate(authenticatorForm, correspondingAuthenticatorEntity);
        }
    }
    // delete authenticatorEntities if it is not included in authenticatorForms
    authenticatorEntities.removeIf(authenticatorEntity -> {
        Integer id = authenticatorEntity.getId();
        if (id == null) {
            return false;
        }
        return Arrays.binarySearch(sortedKeptIds, id) < 0;
    });
    return authenticatorEntities;
}
Also used : WebAuthnSampleEntityNotFoundException(com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException) Arrays(java.util.Arrays) AuthenticatorForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm) ProfileCreateForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileCreateForm) ProfileForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileForm) UserEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity) AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) Base64UrlUtil(com.webauthn4j.util.Base64UrlUtil) ArrayList(java.util.ArrayList) Component(org.springframework.stereotype.Component) List(java.util.List) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity) ProfileUpdateForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileUpdateForm) AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity) AuthenticatorForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm) WebAuthnSampleEntityNotFoundException(com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException)

Example 3 with AuthenticatorEntity

use of com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity in project webauthn4j-spring-security by webauthn4j.

the class AuthenticatorManagerImpl method updateCounter.

@Override
public void updateCounter(byte[] credentialId, long counter) throws CredentialIdNotFoundException {
    AuthenticatorEntity authenticatorEntity = authenticatorEntityRepository.findOneByCredentialId(credentialId).orElseThrow(() -> new CredentialIdNotFoundException("AuthenticatorEntity not found"));
    authenticatorEntity.setCounter(counter);
}
Also used : AuthenticatorEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity) CredentialIdNotFoundException(com.webauthn4j.springframework.security.exception.CredentialIdNotFoundException)

Example 4 with AuthenticatorEntity

use of com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity 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

AuthenticatorEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity)4 AuthorityEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)2 UserEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity)2 Base64UrlUtil (com.webauthn4j.util.Base64UrlUtil)2 Arrays (java.util.Arrays)2 List (java.util.List)2 WebAuthnAuthenticationRequest (com.webauthn4j.springframework.security.WebAuthnAuthenticationRequest)1 WebAuthnAuthenticationToken (com.webauthn4j.springframework.security.WebAuthnAuthenticationToken)1 CredentialIdNotFoundException (com.webauthn4j.springframework.security.exception.CredentialIdNotFoundException)1 AuthenticatorForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm)1 ProfileCreateForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileCreateForm)1 ProfileForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileForm)1 ProfileUpdateForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.ProfileUpdateForm)1 GroupEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity)1 WebAuthnSampleEntityNotFoundException (com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException)1 ArrayList (java.util.ArrayList)1 Collectors (java.util.stream.Collectors)1 Mockito.mock (org.mockito.Mockito.mock)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Authentication (org.springframework.security.core.Authentication)1