Search in sources :

Example 6 with WebAuthnAuthenticatorService

use of com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService in project webauthn4j-spring-security by webauthn4j.

the class AssertionOptionsProviderImplTest method getRpId_with_rpIdProvider.

@Test
public void getRpId_with_rpIdProvider() {
    RpIdProvider rpIdProvider = (HttpServletRequest) -> "example.com";
    WebAuthnAuthenticatorService authenticatorService = mock(WebAuthnAuthenticatorService.class);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    AssertionOptionsProviderImpl optionsProvider = new AssertionOptionsProviderImpl(rpIdProvider, authenticatorService, challengeRepository);
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertThat(optionsProvider.getRpId(request)).isEqualTo("example.com");
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test) Challenge(com.webauthn4j.data.client.challenge.Challenge) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) AuthenticationExtensionsClientInputs(com.webauthn4j.data.extension.client.AuthenticationExtensionsClientInputs) Mockito(org.mockito.Mockito) List(java.util.List) WebAuthnAuthenticator(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) Collections(java.util.Collections) com.webauthn4j.data(com.webauthn4j.data) ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test)

Example 7 with WebAuthnAuthenticatorService

use of com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService in project webauthn4j-spring-security by webauthn4j.

the class AttestationOptionsProviderImplTest method getAttestationOptions_test.

@Test
public void getAttestationOptions_test() {
    Challenge challenge = new DefaultChallenge();
    byte[] credentialId = new byte[] { 0x01, 0x23, 0x45 };
    Set<AuthenticatorTransport> transports = Collections.singleton(AuthenticatorTransport.INTERNAL);
    RpIdProviderImpl rpIdProvider = new RpIdProviderImpl();
    WebAuthnAuthenticatorService authenticatorService = mock(WebAuthnAuthenticatorService.class);
    WebAuthnAuthenticator authenticator = mock(WebAuthnAuthenticator.class, RETURNS_DEEP_STUBS);
    when(authenticator.getTransports()).thenReturn(transports);
    List<WebAuthnAuthenticator> authenticators = Collections.singletonList(authenticator);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setServerName("example.com");
    when(authenticatorService.loadAuthenticatorsByUserPrincipal(any())).thenReturn(authenticators);
    when(authenticator.getAttestedCredentialData().getCredentialId()).thenReturn(credentialId);
    when(challengeRepository.loadOrGenerateChallenge(mockRequest)).thenReturn(challenge);
    AttestationOptionsProviderImpl optionsProvider = new AttestationOptionsProviderImpl(rpIdProvider, authenticatorService, challengeRepository);
    optionsProvider.setRpName("rpName");
    optionsProvider.setPubKeyCredParams(Collections.singletonList(new PublicKeyCredentialParameters(PublicKeyCredentialType.PUBLIC_KEY, COSEAlgorithmIdentifier.ES256)));
    optionsProvider.setRegistrationTimeout(10000L);
    optionsProvider.setRegistrationExtensions(new AuthenticationExtensionsClientInputs<>());
    AttestationOptions attestationOptions = optionsProvider.getAttestationOptions(mockRequest, new UsernamePasswordAuthenticationToken("username", null));
    assertThat(attestationOptions.getRp().getId()).isEqualTo("example.com");
    assertThat(attestationOptions.getRp().getName()).isEqualTo("rpName");
    assertThat(attestationOptions.getUser()).isEqualTo(new PublicKeyCredentialUserEntity("username".getBytes(), "username", "username"));
    assertThat(attestationOptions.getChallenge()).isEqualTo(challenge);
    assertThat(attestationOptions.getPubKeyCredParams()).isEqualTo(Collections.singletonList(new PublicKeyCredentialParameters(PublicKeyCredentialType.PUBLIC_KEY, COSEAlgorithmIdentifier.ES256)));
    assertThat(attestationOptions.getTimeout()).isEqualTo(10000L);
    assertThat(attestationOptions.getExcludeCredentials()).containsExactly(new PublicKeyCredentialDescriptor(PublicKeyCredentialType.PUBLIC_KEY, credentialId, transports));
    assertThat(attestationOptions.getExtensions()).isEqualTo(new AuthenticationExtensionsClientInputs<>());
}
Also used : ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Challenge(com.webauthn4j.data.client.challenge.Challenge) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) WebAuthnAuthenticator(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test)

Example 8 with WebAuthnAuthenticatorService

use of com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService in project webauthn4j-spring-security by webauthn4j.

the class AttestationOptionsProviderImplTest method getRpId_with_rpIdProvider.

@Test
public void getRpId_with_rpIdProvider() {
    RpIdProvider rpIdProvider = (HttpServletRequest) -> "example.com";
    WebAuthnAuthenticatorService authenticatorService = mock(WebAuthnAuthenticatorService.class);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    AttestationOptionsProviderImpl optionsProvider = new AttestationOptionsProviderImpl(rpIdProvider, authenticatorService, challengeRepository);
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertThat(optionsProvider.getRpId(request)).isEqualTo("example.com");
}
Also used : COSEAlgorithmIdentifier(com.webauthn4j.data.attestation.statement.COSEAlgorithmIdentifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test) Challenge(com.webauthn4j.data.client.challenge.Challenge) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) AuthenticationExtensionsClientInputs(com.webauthn4j.data.extension.client.AuthenticationExtensionsClientInputs) Mockito(org.mockito.Mockito) List(java.util.List) WebAuthnAuthenticator(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator) Lists(org.assertj.core.util.Lists) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) PrincipalNotFoundException(com.webauthn4j.springframework.security.exception.PrincipalNotFoundException) Collections(java.util.Collections) com.webauthn4j.data(com.webauthn4j.data) ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test)

Example 9 with WebAuthnAuthenticatorService

use of com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService in project webauthn4j-spring-security by webauthn4j.

the class AttestationOptionsProviderImplTest method getAttestationOptions_with_non_existing_principal_test.

@Test
public void getAttestationOptions_with_non_existing_principal_test() {
    Challenge challenge = new DefaultChallenge();
    RpIdProviderImpl rpIdProvider = new RpIdProviderImpl();
    WebAuthnAuthenticatorService authenticatorService = mock(WebAuthnAuthenticatorService.class);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setServerName("example.com");
    when(authenticatorService.loadAuthenticatorsByUserPrincipal(any())).thenThrow(new PrincipalNotFoundException("dummy"));
    when(challengeRepository.loadOrGenerateChallenge(mockRequest)).thenReturn(challenge);
    AttestationOptionsProviderImpl optionsProvider = new AttestationOptionsProviderImpl(rpIdProvider, authenticatorService, challengeRepository);
    optionsProvider.setRpName("rpName");
    optionsProvider.setPubKeyCredParams(Collections.singletonList(new PublicKeyCredentialParameters(PublicKeyCredentialType.PUBLIC_KEY, COSEAlgorithmIdentifier.ES256)));
    optionsProvider.setRegistrationTimeout(10000L);
    optionsProvider.setRegistrationExtensions(new AuthenticationExtensionsClientInputs<>());
    AttestationOptions attestationOptions = optionsProvider.getAttestationOptions(mockRequest, new UsernamePasswordAuthenticationToken("username", null));
    assertThat(attestationOptions.getRp().getId()).isEqualTo("example.com");
    assertThat(attestationOptions.getRp().getName()).isEqualTo("rpName");
    assertThat(attestationOptions.getUser()).isEqualTo(new PublicKeyCredentialUserEntity("username".getBytes(), "username", "username"));
    assertThat(attestationOptions.getChallenge()).isEqualTo(challenge);
    assertThat(attestationOptions.getPubKeyCredParams()).isEqualTo(Collections.singletonList(new PublicKeyCredentialParameters(PublicKeyCredentialType.PUBLIC_KEY, COSEAlgorithmIdentifier.ES256)));
    assertThat(attestationOptions.getTimeout()).isEqualTo(10000L);
    assertThat(attestationOptions.getExcludeCredentials()).isEmpty();
    assertThat(attestationOptions.getExtensions()).isEqualTo(new AuthenticationExtensionsClientInputs<>());
}
Also used : ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Challenge(com.webauthn4j.data.client.challenge.Challenge) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) DefaultChallenge(com.webauthn4j.data.client.challenge.DefaultChallenge) PrincipalNotFoundException(com.webauthn4j.springframework.security.exception.PrincipalNotFoundException) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test)

Example 10 with WebAuthnAuthenticatorService

use of com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService in project webauthn4j-spring-security by webauthn4j.

the class AttestationOptionsProviderImplTest method getRpId_with_static_rpId.

@Test
public void getRpId_with_static_rpId() {
    WebAuthnAuthenticatorService authenticatorService = mock(WebAuthnAuthenticatorService.class);
    ChallengeRepository challengeRepository = mock(ChallengeRepository.class);
    AttestationOptionsProviderImpl optionsProvider = new AttestationOptionsProviderImpl(authenticatorService, challengeRepository);
    optionsProvider.setRpId("example.com");
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertThat(optionsProvider.getRpId(request)).isEqualTo("example.com");
}
Also used : ChallengeRepository(com.webauthn4j.springframework.security.challenge.ChallengeRepository) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WebAuthnAuthenticatorService(com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService) Test(org.junit.Test)

Aggregations

WebAuthnAuthenticatorService (com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticatorService)10 ChallengeRepository (com.webauthn4j.springframework.security.challenge.ChallengeRepository)9 Test (org.junit.Test)9 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 Challenge (com.webauthn4j.data.client.challenge.Challenge)5 DefaultChallenge (com.webauthn4j.data.client.challenge.DefaultChallenge)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 WebAuthnAuthenticator (com.webauthn4j.springframework.security.authenticator.WebAuthnAuthenticator)4 com.webauthn4j.data (com.webauthn4j.data)2 AuthenticationExtensionsClientInputs (com.webauthn4j.data.extension.client.AuthenticationExtensionsClientInputs)2 PrincipalNotFoundException (com.webauthn4j.springframework.security.exception.PrincipalNotFoundException)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Mockito (org.mockito.Mockito)2 COSEAlgorithmIdentifier (com.webauthn4j.data.attestation.statement.COSEAlgorithmIdentifier)1 Lists (org.assertj.core.util.Lists)1 ApplicationContext (org.springframework.context.ApplicationContext)1