use of com.nimbusds.openid.connect.sdk.AuthenticationRequest in project di-authentication-api by alphagov.
the class AuthorizationServiceTest method shouldReturnErrorWhenNonceIsNotIncludedInAuthRequest.
@Test
void shouldReturnErrorWhenNonceIsNotIncludedInAuthRequest() {
ResponseType responseType = new ResponseType(ResponseType.Value.CODE);
Scope scope = new Scope();
scope.add(OIDCScopeValue.OPENID);
when(dynamoClientService.getClient(CLIENT_ID.toString())).thenReturn(Optional.of(generateClientRegistry(REDIRECT_URI.toString(), CLIENT_ID.toString())));
AuthenticationRequest authRequest = new AuthenticationRequest.Builder(responseType, scope, new ClientID(CLIENT_ID), REDIRECT_URI).state(new State()).build();
Optional<ErrorObject> errorObject = authorizationService.validateAuthRequest(authRequest);
assertThat(errorObject, equalTo(Optional.of(new ErrorObject(OAuth2Error.INVALID_REQUEST_CODE, "Request is missing nonce parameter"))));
}
use of com.nimbusds.openid.connect.sdk.AuthenticationRequest in project di-authentication-api by alphagov.
the class AuthorizationServiceTest method shouldReturnErrorWhenStateIsNotIncludedInAuthRequest.
@Test
void shouldReturnErrorWhenStateIsNotIncludedInAuthRequest() {
ResponseType responseType = new ResponseType(ResponseType.Value.CODE);
Scope scope = new Scope();
scope.add(OIDCScopeValue.OPENID);
when(dynamoClientService.getClient(CLIENT_ID.toString())).thenReturn(Optional.of(generateClientRegistry(REDIRECT_URI.toString(), CLIENT_ID.toString())));
AuthenticationRequest authRequest = new AuthenticationRequest.Builder(responseType, scope, new ClientID(CLIENT_ID), REDIRECT_URI).nonce(new Nonce()).build();
Optional<ErrorObject> errorObject = authorizationService.validateAuthRequest(authRequest);
assertThat(errorObject, equalTo(Optional.of(new ErrorObject(OAuth2Error.INVALID_REQUEST_CODE, "Request is missing state parameter"))));
}
use of com.nimbusds.openid.connect.sdk.AuthenticationRequest in project di-authentication-api by alphagov.
the class AuthorizationServiceTest method shouldSuccessfullyValidateAuthRequestWhenIdentityValuesAreIncludedInVtrAttribute.
@Test
void shouldSuccessfullyValidateAuthRequestWhenIdentityValuesAreIncludedInVtrAttribute() {
Scope scope = new Scope();
scope.add(OIDCScopeValue.OPENID);
when(dynamoClientService.getClient(CLIENT_ID.toString())).thenReturn(Optional.of(generateClientRegistry(REDIRECT_URI.toString(), CLIENT_ID.toString())));
ResponseType responseType = new ResponseType(ResponseType.Value.CODE);
AuthenticationRequest authRequest = generateAuthRequest(REDIRECT_URI.toString(), responseType, scope, jsonArrayOf("P2.Cl.Cm", "P2.Cl"), Optional.empty());
Optional<ErrorObject> errorObject = authorizationService.validateAuthRequest(authRequest);
assertThat(errorObject, equalTo(Optional.empty()));
}
use of com.nimbusds.openid.connect.sdk.AuthenticationRequest in project di-authentication-api by alphagov.
the class IdentityHelper method identityRequired.
public static boolean identityRequired(Map<String, List<String>> authRequestParams) {
AuthenticationRequest authRequest;
try {
authRequest = AuthenticationRequest.parse(authRequestParams);
} catch (ParseException e) {
throw new RuntimeException();
}
List<String> vtr = authRequest.getCustomParameter("vtr");
VectorOfTrust vectorOfTrust = VectorOfTrust.parseFromAuthRequestAttribute(vtr);
return Objects.nonNull(vectorOfTrust.getLevelOfConfidence());
}
use of com.nimbusds.openid.connect.sdk.AuthenticationRequest in project di-authentication-api by alphagov.
the class TokenHandlerTest method shouldReturn200ForSuccessfulTokenRequest.
@ParameterizedTest
@MethodSource("validVectorValues")
public void shouldReturn200ForSuccessfulTokenRequest(String vectorValue, boolean clientRegistryConsent, boolean expectedConsentRequired, boolean clientIdInHeader) throws JOSEException {
KeyPair keyPair = generateRsaKeyPair();
UserProfile userProfile = generateUserProfile();
SignedJWT signedJWT = generateIDToken(CLIENT_ID, PUBLIC_SUBJECT, "issuer-url", new ECKeyGenerator(Curve.P_256).algorithm(JWSAlgorithm.ES256).generate());
OIDCTokenResponse tokenResponse = new OIDCTokenResponse(new OIDCTokens(signedJWT, accessToken, refreshToken));
PrivateKeyJWT privateKeyJWT = generatePrivateKeyJWT(keyPair.getPrivate());
ClientRegistry clientRegistry = generateClientRegistry(keyPair, clientRegistryConsent);
when(tokenService.validateTokenRequestParams(anyString())).thenReturn(Optional.empty());
when(clientService.getClient(eq(CLIENT_ID))).thenReturn(Optional.of(clientRegistry));
when(tokenService.getClientIDFromPrivateKeyJWT(anyString())).thenReturn(Optional.of(CLIENT_ID));
when(tokenService.validatePrivateKeyJWT(anyString(), eq(clientRegistry.getPublicKey()), eq(BASE_URI), eq(CLIENT_ID))).thenReturn(Optional.empty());
String authCode = new AuthorizationCode().toString();
AuthenticationRequest authenticationRequest = generateAuthRequest(JsonArrayHelper.jsonArrayOf(vectorValue));
VectorOfTrust vtr = VectorOfTrust.parseFromAuthRequestAttribute(authenticationRequest.getCustomParameter("vtr"));
when(authorisationCodeService.getExchangeDataForCode(authCode)).thenReturn(Optional.of(new AuthCodeExchangeData().setEmail(TEST_EMAIL).setClientSessionId(CLIENT_SESSION_ID).setClientSession(new ClientSession(authenticationRequest.toParameters(), LocalDateTime.now(), vtr))));
when(dynamoService.getUserProfileByEmail(eq(TEST_EMAIL))).thenReturn(userProfile);
when(tokenService.generateTokenResponse(CLIENT_ID, INTERNAL_SUBJECT, SCOPES, Map.of("nonce", NONCE), PUBLIC_SUBJECT, vtr.retrieveVectorOfTrustForToken(), userProfile.getClientConsent(), expectedConsentRequired, null, false)).thenReturn(tokenResponse);
APIGatewayProxyResponseEvent result = generateApiGatewayRequest(privateKeyJWT, authCode, CLIENT_ID, clientIdInHeader);
assertThat(result, hasStatus(200));
assertTrue(result.getBody().contains(refreshToken.getValue()));
assertTrue(result.getBody().contains(accessToken.getValue()));
}
Aggregations