Search in sources :

Example 1 with AuthenticatorForm

use of com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm 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 2 with AuthenticatorForm

use of com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm in project webauthn4j-spring-security by webauthn4j.

the class AppSpecificMapper method mapToAuthenticatorForm.

private AuthenticatorForm mapToAuthenticatorForm(AuthenticatorEntity authenticatorEntity) {
    AuthenticatorForm authenticatorForm = new AuthenticatorForm();
    authenticatorForm.setId(authenticatorEntity.getId());
    authenticatorForm.setCredentialId(Base64UrlUtil.encodeToString(authenticatorEntity.getAttestedCredentialData().getCredentialId()));
    authenticatorForm.setName(authenticatorEntity.getName());
    return authenticatorForm;
}
Also used : AuthenticatorForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm)

Example 3 with AuthenticatorForm

use of com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm in project webauthn4j-spring-security by webauthn4j.

the class AppSpecificMapper method mapForUpdate.

public UserEntity mapForUpdate(ProfileUpdateForm profileUpdateForm, UserEntity userEntity) {
    userEntity.setUserHandle(mapFromBase64Url(profileUpdateForm.getUserHandle()));
    userEntity.setFirstName(profileUpdateForm.getFirstName());
    userEntity.setLastName(profileUpdateForm.getLastName());
    userEntity.setEmailAddress(profileUpdateForm.getEmailAddress());
    // authenticators
    List<AuthenticatorForm> authenticatorForms = profileUpdateForm.getAuthenticators();
    mapToAuthenticatorListForUpdate(authenticatorForms, userEntity.getAuthenticators());
    userEntity.getAuthenticators().forEach(authenticatorEntity -> authenticatorEntity.setUser(userEntity));
    // authorities
    List<AuthorityEntity> authorities = userEntity.getAuthorities();
    if (profileUpdateForm.isSingleFactorAuthenticationAllowed() == true) {
        if (authorities.stream().anyMatch(authorityEntity -> authorityEntity.getAuthority().equals("SINGLE_FACTOR_AUTHN_ALLOWED"))) {
        // nop
        } else {
            authorities.add(new AuthorityEntity(null, "SINGLE_FACTOR_AUTHN_ALLOWED"));
        }
    } else {
        authorities.clear();
    }
    return userEntity;
}
Also used : AuthenticatorForm(com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)

Aggregations

AuthenticatorForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm)3 AuthorityEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)2 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 AuthenticatorEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity)1 UserEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity)1 WebAuthnSampleEntityNotFoundException (com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException)1 Base64UrlUtil (com.webauthn4j.util.Base64UrlUtil)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)1 Component (org.springframework.stereotype.Component)1