Search in sources :

Example 1 with TestUserDetailsImpl

use of test.TestUserDetailsImpl in project webauthn4j-spring-security by webauthn4j.

the class WebAuthnAuthenticationProviderTest method authenticate_test.

/**
 * Verifies that authentication process passes successfully if input is correct.
 */
@Test
public void authenticate_test() {
    // Given
    byte[] credentialId = new byte[32];
    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
    UserDetails webAuthnPrincipal = new TestUserDetailsImpl("dummy", Collections.singletonList(grantedAuthority));
    WebAuthnAuthenticator webAuthnAuthenticator = mock(WebAuthnAuthenticator.class, RETURNS_DEEP_STUBS);
    when(webAuthnAuthenticator.getUserPrincipal()).thenReturn(webAuthnPrincipal);
    when(webAuthnAuthenticator.getAttestedCredentialData().getCredentialId()).thenReturn(credentialId);
    // When
    WebAuthnAuthenticationRequest request = mock(WebAuthnAuthenticationRequest.class);
    WebAuthnAuthenticationParameters parameters = mock(WebAuthnAuthenticationParameters.class);
    when(request.getCredentialId()).thenReturn(credentialId);
    when(authenticatorService.loadAuthenticatorByCredentialId(credentialId)).thenReturn(webAuthnAuthenticator);
    when(parameters.getServerProperty()).thenReturn(mock(ServerProperty.class));
    Authentication token = new WebAuthnAssertionAuthenticationToken(request, parameters, null);
    Authentication authenticatedToken = authenticationProvider.authenticate(token);
    ArgumentCaptor<AuthenticationRequest> requestCaptor = ArgumentCaptor.forClass(AuthenticationRequest.class);
    ArgumentCaptor<AuthenticationParameters> parameterCaptor = ArgumentCaptor.forClass(AuthenticationParameters.class);
    verify(webAuthnManager).validate(requestCaptor.capture(), parameterCaptor.capture());
    assertThat(authenticatedToken.getPrincipal()).isEqualTo(webAuthnPrincipal);
    assertThat(authenticatedToken.getCredentials()).isEqualTo(request);
    assertThat(authenticatedToken.getAuthorities().toArray()).containsExactly(grantedAuthority);
}
Also used : TestUserDetailsImpl(test.TestUserDetailsImpl) AuthenticationParameters(com.webauthn4j.data.AuthenticationParameters) ServerProperty(com.webauthn4j.server.ServerProperty) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) WebAuthnAuthenticator(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) UserDetails(org.springframework.security.core.userdetails.UserDetails) Authentication(org.springframework.security.core.Authentication) AuthenticationRequest(com.webauthn4j.data.AuthenticationRequest) Test(org.junit.Test)

Example 2 with TestUserDetailsImpl

use of test.TestUserDetailsImpl in project webauthn4j-spring-security by webauthn4j.

the class WebAuthnAuthenticationTokenTest method getter_methods.

@Test
public void getter_methods() {
    WebAuthnAuthenticationRequest credential = mock(WebAuthnAuthenticationRequest.class);
    UserDetails principal = new TestUserDetailsImpl("username");
    WebAuthnAuthenticationToken webAuthnAuthenticationToken = new WebAuthnAuthenticationToken(principal, credential, null);
    assertThat(webAuthnAuthenticationToken.getPrincipal()).isEqualTo(principal);
    Assertions.assertThat(webAuthnAuthenticationToken.getCredentials()).isEqualTo(credential);
}
Also used : TestUserDetailsImpl(test.TestUserDetailsImpl) UserDetails(org.springframework.security.core.userdetails.UserDetails) Test(org.junit.Test)

Example 3 with TestUserDetailsImpl

use of test.TestUserDetailsImpl in project webauthn4j-spring-security by webauthn4j.

the class WebAuthnAuthenticationTokenTest method equals_hashCode_test.

@Test
public void equals_hashCode_test() {
    WebAuthnAuthenticationRequest credential = mock(WebAuthnAuthenticationRequest.class);
    WebAuthnAuthenticationToken tokenA = new WebAuthnAuthenticationToken(new TestUserDetailsImpl("username"), credential, null);
    WebAuthnAuthenticationToken tokenB = new WebAuthnAuthenticationToken(new TestUserDetailsImpl("username"), credential, null);
    assertThat(tokenA).isEqualTo(tokenB).hasSameHashCodeAs(tokenB);
}
Also used : TestUserDetailsImpl(test.TestUserDetailsImpl) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 TestUserDetailsImpl (test.TestUserDetailsImpl)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)2 AuthenticationParameters (com.webauthn4j.data.AuthenticationParameters)1 AuthenticationRequest (com.webauthn4j.data.AuthenticationRequest)1 ServerProperty (com.webauthn4j.server.ServerProperty)1 WebAuthnAuthenticator (com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator)1 Authentication (org.springframework.security.core.Authentication)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1