Search in sources :

Example 1 with AuthorityEntity

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

the class ProfileControllerTest method create_test.

@Test
@WithAnonymousUser
public void create_test() throws Exception {
    ProfileCreateForm userCreateForm = new ProfileCreateForm();
    userCreateForm.setUserHandle("ORZClsZpTvWrYGl7mXL5Wg");
    userCreateForm.setFirstName("John");
    userCreateForm.setLastName("Doe");
    userCreateForm.setEmailAddress("john.doe@example.com");
    userCreateForm.setPassword("password");
    userCreateForm.setAuthenticators(Collections.emptyList());
    userCreateForm.setSingleFactorAuthenticationAllowed(true);
    UserEntity userEntity = new UserEntity();
    userEntity.setId(1);
    userEntity.setUserHandle(Base64UrlUtil.decode("ORZClsZpTvWrYGl7mXL5Wg"));
    userEntity.setFirstName("John");
    userEntity.setLastName("Doe");
    userEntity.setEmailAddress("john.doe@example.com");
    userEntity.setAuthenticators(Collections.emptyList());
    userEntity.setAuthorities(Collections.singletonList(new AuthorityEntity(0, "SINGLE_FACTOR_AUTHN_ALLOWED")));
    when(profileAppService.create(any())).thenReturn(userEntity);
    // When
    mvc.perform(post("/api/profile").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(userCreateForm)).with(SecurityMockMvcRequestPostProcessors.csrf())).andExpect(status().isOk()).andExpect(jsonPath("$.id", is(1))).andExpect(jsonPath("$.userHandle", is("ORZClsZpTvWrYGl7mXL5Wg"))).andExpect(jsonPath("$.firstName", is("John"))).andExpect(jsonPath("$.lastName", is("Doe"))).andExpect(jsonPath("$.emailAddress", is("john.doe@example.com"))).andExpect(jsonPath("$.authenticators", is(empty()))).andExpect(jsonPath("$.singleFactorAuthenticationAllowed", is(true)));
    verify(profileAppService).create(any());
}
Also used : UserEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity) WithAnonymousUser(org.springframework.security.test.context.support.WithAnonymousUser) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 2 with AuthorityEntity

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

the class GroupManagerImpl method removeGroupAuthority.

@Override
public void removeGroupAuthority(int groupId, AuthorityEntity authority) {
    GroupEntity groupEntity = groupEntityRepository.findById(groupId).orElseThrow(() -> new WebAuthnSampleEntityNotFoundException("Group not found."));
    AuthorityEntity authorityEntityEntity = authorityEntityRepository.findById(authority.getId()).orElseThrow(() -> new WebAuthnSampleEntityNotFoundException("Authority not found"));
    groupEntity.getAuthorities().remove(authorityEntityEntity);
}
Also used : WebAuthnSampleEntityNotFoundException(com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException) GroupEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)

Example 3 with AuthorityEntity

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

the class GroupManagerImpl method addGroupAuthority.

@Override
public void addGroupAuthority(int groupId, AuthorityEntity authority) {
    GroupEntity groupEntity = groupEntityRepository.findById(groupId).orElseThrow(() -> new WebAuthnSampleEntityNotFoundException("Group not found."));
    AuthorityEntity authorityEntityEntity = modelMapper.map(authority, AuthorityEntity.class);
    groupEntity.getAuthorities().add(authorityEntityEntity);
}
Also used : WebAuthnSampleEntityNotFoundException(com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException) GroupEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)

Example 4 with AuthorityEntity

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

the class AppSpecificMapper method mapForCreate.

public UserEntity mapForCreate(ProfileCreateForm profileCreateForm) {
    UserEntity userEntity = new UserEntity();
    userEntity.setId(null);
    userEntity.setUserHandle(mapFromBase64Url(profileCreateForm.getUserHandle()));
    userEntity.setFirstName(profileCreateForm.getFirstName());
    userEntity.setLastName(profileCreateForm.getLastName());
    userEntity.setEmailAddress(profileCreateForm.getEmailAddress());
    userEntity.setPassword(passwordEncoder.encode(profileCreateForm.getPassword()));
    // authenticators
    userEntity.setAuthenticators(new ArrayList<>());
    mapToAuthenticatorListForCreate(profileCreateForm.getAuthenticators(), userEntity.getAuthenticators());
    userEntity.getAuthenticators().forEach(authenticatorEntity -> authenticatorEntity.setUser(userEntity));
    // authorities
    List<AuthorityEntity> authorities = new ArrayList<>();
    if (profileCreateForm.isSingleFactorAuthenticationAllowed() == true) {
        authorities.add(new AuthorityEntity(null, "SINGLE_FACTOR_AUTHN_ALLOWED"));
    }
    userEntity.setAuthorities(authorities);
    return userEntity;
}
Also used : ArrayList(java.util.ArrayList) UserEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity) AuthorityEntity(com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)

Example 5 with AuthorityEntity

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

AuthorityEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthorityEntity)10 UserEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.UserEntity)7 GroupEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.GroupEntity)4 WebAuthnSampleEntityNotFoundException (com.webauthn4j.springframework.security.webauthn.sample.domain.exception.WebAuthnSampleEntityNotFoundException)4 Test (org.junit.Test)3 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)3 AuthenticatorForm (com.webauthn4j.springframework.security.webauthn.sample.app.api.AuthenticatorForm)2 AuthenticatorEntity (com.webauthn4j.springframework.security.webauthn.sample.domain.entity.AuthenticatorEntity)2 WithMockWebAuthnUser (com.webauthn4j.springframework.security.webauthn.sample.test.WithMockWebAuthnUser)2 Base64UrlUtil (com.webauthn4j.util.Base64UrlUtil)2 ArrayList (java.util.ArrayList)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 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 Collectors (java.util.stream.Collectors)1 Mockito.mock (org.mockito.Mockito.mock)1