From a5f4313fca96182210c5e80b84d61097fa96cc6e Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Thu, 8 Feb 2024 18:18:59 +0530 Subject: [PATCH 1/8] ES-541 Signed-off-by: Venkata Saidurga Polamraju --- .../core/dto/ClientDetailCreateRequestV2.java | 3 ++- .../core/validator/ClientNameLang.java | 24 +++++++++++++++++ .../validator/ClientNameLangValidator.java | 27 +++++++++++++++++++ ...ClientMgmtControllerParameterizedTest.java | 6 +++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLang.java create mode 100644 esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java index 569daf42b..f662fd2c3 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java @@ -6,6 +6,7 @@ package io.mosip.esignet.core.dto; import io.mosip.esignet.core.constants.ErrorConstants; +import io.mosip.esignet.core.validator.ClientNameLang; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -22,7 +23,7 @@ public class ClientDetailCreateRequestV2 extends ClientDetailCreateRequest { @NotEmpty(message = ErrorConstants.INVALID_CLIENT_NAME) - private Map<@Size(message= ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min=3, max=3) String, + private Map<@Size(message= ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min=3, max=3) @ClientNameLang String, @NotBlank(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_VALUE) String> clientNameLangMap; public ClientDetailCreateRequestV2(String clientId, String clientName, Map publicKey, String relyingPartyId, diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLang.java b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLang.java new file mode 100644 index 000000000..f1a4e1607 --- /dev/null +++ b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLang.java @@ -0,0 +1,24 @@ +package io.mosip.esignet.core.validator; + +import io.mosip.esignet.core.constants.ErrorConstants; + +import javax.validation.Constraint; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD, TYPE_USE}) +@Retention(RUNTIME) +@Constraint(validatedBy = ClientNameLangValidator.class) +@Documented +public @interface ClientNameLang { + String message() default ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java new file mode 100644 index 000000000..362b42cf5 --- /dev/null +++ b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java @@ -0,0 +1,27 @@ +package io.mosip.esignet.core.validator; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.Locale; +import java.util.Set; + +public class ClientNameLangValidator implements ConstraintValidator { + + @Override + public void initialize(ClientNameLang constraintAnnotation) { + } + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + Locale[] availableLocales = Locale.getAvailableLocales(); + boolean isValid = false; + + for (Locale locale : availableLocales) { + if (value.equals(locale.getISO3Language())) { + isValid = true; + break; + } + } + return isValid; + } +} \ No newline at end of file diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java index 006b890ac..e08be6450 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java @@ -80,6 +80,7 @@ public class ClientMgmtControllerParameterizedTest { private static Map jwk = TestUtil.generateJWK_RSA().toPublicJWK().toJSONObject(); + // private static Map jwk2 = TestUtil.generateJWK_RSA().toPublicJWK().toJSONObject(); private ClientDetailCreateRequestV2 clientDetailCreateRequestV2; private ClientDetailUpdateRequestV2 clientDetailUpdateRequestV2; private String clientIdQueryParam; @@ -114,6 +115,11 @@ public ClientMgmtControllerParameterizedTest(String title, ClientDetailCreateReq Arrays.asList("mosip:idp:acr:static-code"), "https://logo-url/png", Arrays.asList("https://logo-url/png"), Arrays.asList("authorization_code"), Arrays.asList("private_key_jwt"),new HashMap(){{put("eng", "clientname");}}), null, null, ErrorConstants.INVALID_CLIENT_NAME }, + { "With Invalid Language_code", new ClientDetailCreateRequestV2("client-id", "clientname", jwk, + "rp-id", Arrays.asList("given_name"), + Arrays.asList("mosip:idp:acr:static-code"), "https://logo-url/png", + Arrays.asList("https://logo-url/png"), Arrays.asList("authorization_code"), + Arrays.asList("private_key_jwt"),new HashMap(){{put("abc", "clientname");}}), null, null, ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY }, { "With Invalid public key", new ClientDetailCreateRequestV2("client-id", "Test client", new HashMap<>(), "rp-id", Arrays.asList("given_name"), Arrays.asList("mosip:idp:acr:static-code"), "https://logo-url/png", From 1b0d0319b6f9dc228cfa806ee768cae66238b349 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Thu, 8 Feb 2024 18:25:20 +0530 Subject: [PATCH 2/8] ES-541 Signed-off-by: Venkata Saidurga Polamraju --- .../controllers/ClientMgmtControllerParameterizedTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java index e08be6450..ac20497f9 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java @@ -80,7 +80,6 @@ public class ClientMgmtControllerParameterizedTest { private static Map jwk = TestUtil.generateJWK_RSA().toPublicJWK().toJSONObject(); - // private static Map jwk2 = TestUtil.generateJWK_RSA().toPublicJWK().toJSONObject(); private ClientDetailCreateRequestV2 clientDetailCreateRequestV2; private ClientDetailUpdateRequestV2 clientDetailUpdateRequestV2; private String clientIdQueryParam; From 1f6d2f9eae728a6f4af0c698e6e3a4daebac8ce8 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Fri, 9 Feb 2024 10:38:49 +0530 Subject: [PATCH 3/8] reviewed changes Signed-off-by: Venkata Saidurga Polamraju --- .../core/dto/ClientDetailUpdateRequestV2.java | 3 ++- .../core/validator/ClientNameLangValidator.java | 15 +++++---------- .../ClientMgmtControllerParameterizedTest.java | 4 ++++ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java index 52f770c86..fb6d32c9c 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java @@ -6,6 +6,7 @@ package io.mosip.esignet.core.dto; import io.mosip.esignet.core.constants.ErrorConstants; +import io.mosip.esignet.core.validator.ClientNameLang; import lombok.Data; import lombok.NoArgsConstructor; @@ -18,7 +19,7 @@ public class ClientDetailUpdateRequestV2 extends ClientDetailUpdateRequest { @NotEmpty(message = ErrorConstants.INVALID_CLIENT_NAME) - private Map<@Size(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min = 3, max = 3) String, + private Map<@Size(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min = 3, max = 3) @ClientNameLang String, @NotBlank(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_VALUE) String> clientNameLangMap; public ClientDetailUpdateRequestV2(String logUri, List redirectUris, List userClaims, List authContextRefs, String status, List grantTypes, String clientName, List clientAuthMethods, Map clientNameLangMap){ diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java index 362b42cf5..69d6f9b59 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/validator/ClientNameLangValidator.java @@ -2,26 +2,21 @@ import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; +import java.util.Arrays; import java.util.Locale; -import java.util.Set; public class ClientNameLangValidator implements ConstraintValidator { + private static final Locale[] availableLocales = Locale.getAvailableLocales(); + @Override public void initialize(ClientNameLang constraintAnnotation) { } @Override public boolean isValid(String value, ConstraintValidatorContext context) { - Locale[] availableLocales = Locale.getAvailableLocales(); - boolean isValid = false; - - for (Locale locale : availableLocales) { - if (value.equals(locale.getISO3Language())) { - isValid = true; - break; - } - } + boolean isValid = Arrays.stream(availableLocales) + .anyMatch(locale -> value.equals(locale.getISO3Language())); return isValid; } } \ No newline at end of file diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java index ac20497f9..d4aaa5e01 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/ClientMgmtControllerParameterizedTest.java @@ -189,6 +189,10 @@ public ClientMgmtControllerParameterizedTest(String title, ClientDetailCreateReq Arrays.asList("https://logo-url/png"),Arrays.asList("given_name"), Arrays.asList("mosip:idp:acr:static-code"), "ACTIVE", Arrays.asList("authorization_code"), "client-name#1", Arrays.asList("private_key_jwt"),new HashMap(){{put("eng", "clientname");}}), "cid#1", "invalid_client_id" }, + { "update with invalid language_code", null, new ClientDetailUpdateRequestV2("https://logo-url/png", + Arrays.asList("https://logo-url/png"),Arrays.asList("given_name"), + Arrays.asList("mosip:idp:acr:static-code"), "ACTIVE", Arrays.asList("authorization_code"), + "client-name", Arrays.asList("private_key_jwt"),new HashMap(){{put("abc", "clientname");}}), "cid#1", "invalid_language_code" }, { "update client-details", new ClientDetailCreateRequestV2("client-id-up1", "client-name", TestUtil.generateJWK_RSA().toPublicJWK().toJSONObject(), "rp-id", Arrays.asList("given_name"), From 0592fe1902d6a725c1b0899589ba5f6e54c330cf Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Wed, 14 Feb 2024 11:47:29 +0530 Subject: [PATCH 4/8] [ES-759] Signed-off-by: Venkata Saidurga Polamraju --- .../mosip/esignet/api/dto/AuthChallenge.java | 2 + .../mosip/esignet/api/validator/Format.java | 25 +++++++++++ .../api/validator/FormatValidator.java | 41 +++++++++++++++++++ .../resources/application-local.properties | 1 + .../AuthorizationControllerTest.java | 33 ++++++++++++++- .../controllers/KeyBindingControllerTest.java | 2 +- .../LinkedAuthorizationControllerTest.java | 20 ++++----- .../mosip/esignet/flows/AuthCodeFlowTest.java | 2 +- .../flows/AuthorizationAPIFlowTest.java | 4 +- .../resources/application-test.properties | 1 + 10 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java create mode 100644 esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java index 23efa919f..bbf32ba4f 100644 --- a/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java @@ -6,11 +6,13 @@ package io.mosip.esignet.api.dto; import io.mosip.esignet.api.util.ErrorConstants; +import io.mosip.esignet.api.validator.Format; import lombok.Data; import javax.validation.constraints.NotBlank; @Data +@Format public class AuthChallenge { @NotBlank(message = ErrorConstants.INVALID_AUTH_FACTOR_TYPE) diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java new file mode 100644 index 000000000..da943b100 --- /dev/null +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java @@ -0,0 +1,25 @@ +package io.mosip.esignet.api.validator; + +import io.mosip.esignet.api.util.ErrorConstants; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD, TYPE_USE}) +@Retention(RUNTIME) +@Constraint(validatedBy = FormatValidator.class) +@Documented +public @interface Format { + String message() default ErrorConstants.INVALID_CHALLENGE_FORMAT; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java new file mode 100644 index 000000000..10efa5da9 --- /dev/null +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java @@ -0,0 +1,41 @@ +package io.mosip.esignet.api.validator; + +import io.mosip.esignet.api.dto.AuthChallenge; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.util.List; +import java.util.Map; + + +@Component +public class FormatValidator implements ConstraintValidator { + @Value("#{${mosip.esignet.supported-formats}}") + private Map supportedFormats; + + @Override + public void initialize(Format constraintAnnotation) { + } + + @Override + public boolean isValid(AuthChallenge authChallenge, ConstraintValidatorContext context) { + Object supportedFormatType = supportedFormats.get(authChallenge.getAuthFactorType()); + if(supportedFormatType instanceof List) { + List supportedFormatsList = (List) supportedFormatType; + if (!supportedFormatsList.contains(authChallenge.getFormat())) { + return false; + } + } else if (supportedFormatType instanceof String) { + String supportedFormat = (String) supportedFormatType; + if (!supportedFormat.equals(authChallenge.getFormat())) { + return false; + } + } else { + return false; + } + return true; + } + +} \ No newline at end of file diff --git a/esignet-service/src/main/resources/application-local.properties b/esignet-service/src/main/resources/application-local.properties index fa8634c88..7fa4025bd 100644 --- a/esignet-service/src/main/resources/application-local.properties +++ b/esignet-service/src/main/resources/application-local.properties @@ -12,6 +12,7 @@ mosip.esignet.access-token-expire-seconds=3600 mosip.esignet.link-code-expire-in-secs=60 mosip.esignet.authentication-expire-in-secs=60 mosip.esignet.cnonce-expire-seconds=20 +mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt'} mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \ '${server.servlet.path}/authorization/authenticate', \ diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java index f8bc1f194..80951de06 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java @@ -733,7 +733,7 @@ public void authenticateEndUser_withValidDetails_returnSuccessResponse() throws AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setChallenge("12345"); authChallenge.setAuthFactorType("OTP"); - authChallenge.setFormat("numeric"); + authChallenge.setFormat("alpha-numeric"); List authChallengeList = new ArrayList<>(); authChallengeList.add(authChallenge); @@ -786,7 +786,36 @@ public void authenticateEndUser_withInvalidTimestamp_returnErrorResponse() throw } @Test - public void authenticateEndUser_withInvalidTransectionId_returnErrorResponse() throws Exception { + public void authenticateEndUser_withInvalidFormat_returnErrorResponse() throws Exception { + AuthRequest authRequest = new AuthRequest(); + authRequest.setIndividualId("1234567890"); + authRequest.setTransactionId("1234567890"); + + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setChallenge("1234567890"); + authChallenge.setAuthFactorType("OTP"); + authChallenge.setFormat("jwt"); + + List authChallengeList = new ArrayList<>(); + authChallengeList.add(authChallenge); + + authRequest.setChallengeList(authChallengeList); + + RequestWrapper wrapper = new RequestWrapper<>(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(authRequest); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + mockMvc.perform(post("/authorization/v2/authenticate") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT)) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format")); + } + + @Test + public void authenticateEndUser_withInvalidTransactionId_returnErrorResponse() throws Exception { AuthRequest authRequest = new AuthRequest(); authRequest.setIndividualId("1234567890"); diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java index 4dbcea334..0f9bab8d5 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java @@ -265,7 +265,7 @@ public void bindWallet_withAuthChallengeEmptyFactorAndEmptyChallenge_thenFail() List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); - Assert.assertTrue(responseWrapper.getErrors().size() == 3); + Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java index fad6dad2d..9bff798ba 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java @@ -295,7 +295,7 @@ public void authenticate_withValidRequest_thenPass() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("link-transaction-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -319,7 +319,7 @@ public void authenticate_withException_thenFail() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("link-transaction-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -342,7 +342,7 @@ public void authenticate_withInvalidTransactionId_thenFail() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId(" "); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -364,7 +364,7 @@ public void authenticate_withInvalidIndividualId_thenFail() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("txn-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -412,7 +412,7 @@ public void authenticate_withInvalidChallengeList_thenFail() throws Exception { List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); - Assert.assertTrue(responseWrapper.getErrors().size() == 3); + Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); @@ -662,7 +662,7 @@ public void authenticateV2_withValidRequest_thenPass() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("link-transaction-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -686,7 +686,7 @@ public void authenticateV2_withException_thenFail() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("link-transaction-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -709,7 +709,7 @@ public void authenticateV2_withInvalidTransactionId_thenFail() throws Exception LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId(" "); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -731,7 +731,7 @@ public void authenticateV2_withInvalidIndividualId_thenFail() throws Exception { LinkedKycAuthRequest linkedKycAuthRequest = new LinkedKycAuthRequest(); linkedKycAuthRequest.setLinkedTransactionId("txn-id"); AuthChallenge authChallenge = new AuthChallenge(); - authChallenge.setFormat("format"); + authChallenge.setFormat("alpha-numeric"); authChallenge.setAuthFactorType("OTP"); authChallenge.setChallenge("challenge"); linkedKycAuthRequest.setChallengeList(Arrays.asList(authChallenge)); @@ -779,7 +779,7 @@ public void authenticateV2_withInvalidChallengeList_thenFail() throws Exception List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); - Assert.assertTrue(responseWrapper.getErrors().size() == 3); + Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); diff --git a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java index c9859af7d..63031bb3c 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java @@ -283,7 +283,7 @@ private ResponseWrapper authenticate(String transactionId) throws AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("34789"); - authChallenge.setFormat("number"); + authChallenge.setFormat("numeric"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); diff --git a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java index 31bcbfdaa..31d128d02 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java @@ -403,7 +403,7 @@ private ResponseWrapper authenticateWithInvalidPin(String transact AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("1234453"); - authChallenge.setFormat("number"); + authChallenge.setFormat("numeric"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); @@ -428,7 +428,7 @@ private ResponseWrapper authenticate(String transactionId) throws AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("34789"); - authChallenge.setFormat("number"); + authChallenge.setFormat("numeric"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); diff --git a/esignet-service/src/test/resources/application-test.properties b/esignet-service/src/test/resources/application-test.properties index d4c1843c6..51e277339 100644 --- a/esignet-service/src/test/resources/application-test.properties +++ b/esignet-service/src/test/resources/application-test.properties @@ -10,6 +10,7 @@ mosip.esignet.supported-id-regex=\\S* mosip.esignet.id-token-expire-seconds=3600 mosip.esignet.access-token.expire.seconds=3600 mosip.esignet.link-code-expire-in-secs=60 +mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt', 'PIN': 'numeric'} mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \ '${server.servlet.path}/authorization/authenticate', \ From a840162dd8e73b3e8a90d9d9ff2af16190fa4361 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Fri, 16 Feb 2024 20:00:14 +0530 Subject: [PATCH 5/8] reviewed changes Signed-off-by: Venkata Saidurga Polamraju --- .../core/constants/ErrorConstants.java | 1 + .../core/dto/ClientDetailCreateRequestV2.java | 2 +- .../core/dto/ClientDetailUpdateRequestV2.java | 2 +- .../mosip/esignet/api/dto/AuthChallenge.java | 4 ++-- .../esignet/api/util/ErrorConstants.java | 1 + .../{Format.java => TypeFormatMapping.java} | 6 ++--- ...r.java => TypeFormatMappingValidator.java} | 23 +++++-------------- .../AuthorizationControllerTest.java | 4 ++-- .../controllers/KeyBindingControllerTest.java | 3 ++- .../LinkedAuthorizationControllerTest.java | 18 ++++----------- 10 files changed, 24 insertions(+), 40 deletions(-) rename esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/{Format.java => TypeFormatMapping.java} (84%) rename esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/{FormatValidator.java => TypeFormatMappingValidator.java} (52%) diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java b/esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java index d60d1dc55..005d12968 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/constants/ErrorConstants.java @@ -84,4 +84,5 @@ public class ErrorConstants { public static final String PROOF_HEADER_INVALID_ALG = "proof_header_invalid_alg"; public static final String PROOF_HEADER_INVALID_KEY = "proof_header_invalid_key"; public static final String PROOF_HEADER_AMBIGUOUS_KEY = "proof_header_ambiguous_key"; + public static final String INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE = "invalid_challenge_format_for_auth_factor_type"; } diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java index f662fd2c3..15ebc69a0 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailCreateRequestV2.java @@ -23,7 +23,7 @@ public class ClientDetailCreateRequestV2 extends ClientDetailCreateRequest { @NotEmpty(message = ErrorConstants.INVALID_CLIENT_NAME) - private Map<@Size(message= ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min=3, max=3) @ClientNameLang String, + private Map<@ClientNameLang String, @NotBlank(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_VALUE) String> clientNameLangMap; public ClientDetailCreateRequestV2(String clientId, String clientName, Map publicKey, String relyingPartyId, diff --git a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java index fb6d32c9c..08483f915 100644 --- a/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java +++ b/esignet-core/src/main/java/io/mosip/esignet/core/dto/ClientDetailUpdateRequestV2.java @@ -19,7 +19,7 @@ public class ClientDetailUpdateRequestV2 extends ClientDetailUpdateRequest { @NotEmpty(message = ErrorConstants.INVALID_CLIENT_NAME) - private Map<@Size(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_KEY, min = 3, max = 3) @ClientNameLang String, + private Map<@ClientNameLang String, @NotBlank(message = ErrorConstants.INVALID_CLIENT_NAME_MAP_VALUE) String> clientNameLangMap; public ClientDetailUpdateRequestV2(String logUri, List redirectUris, List userClaims, List authContextRefs, String status, List grantTypes, String clientName, List clientAuthMethods, Map clientNameLangMap){ diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java index bbf32ba4f..501b35455 100644 --- a/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/dto/AuthChallenge.java @@ -6,13 +6,13 @@ package io.mosip.esignet.api.dto; import io.mosip.esignet.api.util.ErrorConstants; -import io.mosip.esignet.api.validator.Format; +import io.mosip.esignet.api.validator.TypeFormatMapping; import lombok.Data; import javax.validation.constraints.NotBlank; @Data -@Format +@TypeFormatMapping public class AuthChallenge { @NotBlank(message = ErrorConstants.INVALID_AUTH_FACTOR_TYPE) diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/util/ErrorConstants.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/util/ErrorConstants.java index 9accf4665..919958801 100644 --- a/esignet-integration-api/src/main/java/io/mosip/esignet/api/util/ErrorConstants.java +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/util/ErrorConstants.java @@ -15,6 +15,7 @@ public class ErrorConstants { public static final String INVALID_AUTH_FACTOR_TYPE="invalid_auth_factor_type"; public static final String INVALID_CHALLENGE="invalid_challenge"; public static final String INVALID_CHALLENGE_FORMAT = "invalid_challenge_format"; + public static final String INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE = "invalid_challenge_format_for_auth_factor_type"; public static final String BINDING_AUTH_FAILED = "binding_auth_failed"; public static final String VCI_EXCHANGE_FAILED = "vci_exchange_failed"; public static final String NOT_IMPLEMENTED = "not_implemented"; diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMapping.java similarity index 84% rename from esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java rename to esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMapping.java index da943b100..1ff4a1ef8 100644 --- a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/Format.java +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMapping.java @@ -14,10 +14,10 @@ @Target({FIELD, TYPE_USE}) @Retention(RUNTIME) -@Constraint(validatedBy = FormatValidator.class) +@Constraint(validatedBy = TypeFormatMappingValidator.class) @Documented -public @interface Format { - String message() default ErrorConstants.INVALID_CHALLENGE_FORMAT; +public @interface TypeFormatMapping { + String message() default ErrorConstants.INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE; Class[] groups() default {}; diff --git a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMappingValidator.java similarity index 52% rename from esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java rename to esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMappingValidator.java index 10efa5da9..98dee5c10 100644 --- a/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/FormatValidator.java +++ b/esignet-integration-api/src/main/java/io/mosip/esignet/api/validator/TypeFormatMappingValidator.java @@ -6,36 +6,25 @@ import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; -import java.util.List; import java.util.Map; - @Component -public class FormatValidator implements ConstraintValidator { +public class TypeFormatMappingValidator implements ConstraintValidator { + @Value("#{${mosip.esignet.supported-formats}}") private Map supportedFormats; @Override - public void initialize(Format constraintAnnotation) { + public void initialize(TypeFormatMapping constraintAnnotation) { } @Override public boolean isValid(AuthChallenge authChallenge, ConstraintValidatorContext context) { Object supportedFormatType = supportedFormats.get(authChallenge.getAuthFactorType()); - if(supportedFormatType instanceof List) { - List supportedFormatsList = (List) supportedFormatType; - if (!supportedFormatsList.contains(authChallenge.getFormat())) { - return false; - } - } else if (supportedFormatType instanceof String) { + if (supportedFormatType != null ) { String supportedFormat = (String) supportedFormatType; - if (!supportedFormat.equals(authChallenge.getFormat())) { - return false; - } - } else { - return false; + return supportedFormat.equals(authChallenge.getFormat()); } - return true; + return false; } - } \ No newline at end of file diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java index 80951de06..1ea010926 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java @@ -810,8 +810,8 @@ public void authenticateEndUser_withInvalidFormat_returnErrorResponse() throws E .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$.errors").isNotEmpty()) - .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT)) - .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format")); + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE)) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format_for_auth_factor_type")); } @Test diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java index 0f9bab8d5..768ac54bd 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/KeyBindingControllerTest.java @@ -263,12 +263,13 @@ public void bindWallet_withAuthChallengeEmptyFactorAndEmptyChallenge_thenFail() .andExpect(status().isOk()) .andExpect(jsonPath("$.errors").isNotEmpty()).andReturn(); - List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); + List errorCodes = Arrays.asList(INVALID_CHALLENGE_FORMAT,INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE,INVALID_CHALLENGE); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(3)).getErrorCode())); } /*@Test diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java index 9bff798ba..2517b4a11 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/LinkedAuthorizationControllerTest.java @@ -1,16 +1,6 @@ package io.mosip.esignet.controllers; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_AUTH_FACTOR_TYPE; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_CHALLENGE; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_CHALLENGE_FORMAT; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_IDENTIFIER; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_LINK_CODE; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_OTP_CHANNEL; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_TRANSACTION_ID; -import static io.mosip.esignet.core.constants.ErrorConstants.RESPONSE_TIMEOUT; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_ACCEPTED_CLAIM; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_PERMITTED_SCOPE; -import static io.mosip.esignet.core.constants.ErrorConstants.INVALID_SIGNATURE_FORMAT; +import static io.mosip.esignet.core.constants.ErrorConstants.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -410,12 +400,13 @@ public void authenticate_withInvalidChallengeList_thenFail() throws Exception { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); - List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); + List errorCodes = Arrays.asList(INVALID_CHALLENGE_FORMAT,INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE,INVALID_CHALLENGE); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(3)).getErrorCode())); } @Test @@ -777,12 +768,13 @@ public void authenticateV2_withInvalidChallengeList_thenFail() throws Exception .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()).andReturn(); - List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE, INVALID_CHALLENGE_FORMAT); + List errorCodes = Arrays.asList(INVALID_CHALLENGE_FORMAT,INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE,INVALID_CHALLENGE); ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); Assert.assertTrue(responseWrapper.getErrors().size() == 4); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(2)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(3)).getErrorCode())); } @Test From f9bbdb808eba7de0bf4dd7d7f7fb665cd488e549 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Sun, 18 Feb 2024 14:51:16 +0530 Subject: [PATCH 6/8] added testcases Signed-off-by: Venkata Saidurga Polamraju --- .../AuthorizationControllerTest.java | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java index 1ea010926..2d987cb57 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java @@ -814,6 +814,64 @@ public void authenticateEndUser_withInvalidFormat_returnErrorResponse() throws E .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format_for_auth_factor_type")); } + @Test + public void authenticateEndUser_withBlankFormat_returnErrorResponse() throws Exception { + AuthRequest authRequest = new AuthRequest(); + authRequest.setIndividualId("1234567890"); + authRequest.setTransactionId("1234567890"); + + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setChallenge("1234567890"); + authChallenge.setAuthFactorType("OTP"); + authChallenge.setFormat(""); + + List authChallengeList = new ArrayList<>(); + authChallengeList.add(authChallenge); + + authRequest.setChallengeList(authChallengeList); + + RequestWrapper wrapper = new RequestWrapper<>(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(authRequest); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + mockMvc.perform(post("/authorization/v2/authenticate") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT)) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0].format: invalid_challenge_format")); + } + + @Test + public void authenticateEndUser_withNullFormat_returnErrorResponse() throws Exception { + AuthRequest authRequest = new AuthRequest(); + authRequest.setIndividualId("1234567890"); + authRequest.setTransactionId("1234567890"); + + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setChallenge("1234567890"); + authChallenge.setAuthFactorType("OTP"); + authChallenge.setFormat(null); + + List authChallengeList = new ArrayList<>(); + authChallengeList.add(authChallenge); + + authRequest.setChallengeList(authChallengeList); + + RequestWrapper wrapper = new RequestWrapper<>(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(authRequest); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + mockMvc.perform(post("/authorization/v2/authenticate") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE)) + .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format_for_auth_factor_type")); + } + @Test public void authenticateEndUser_withInvalidTransactionId_returnErrorResponse() throws Exception { AuthRequest authRequest = new AuthRequest(); @@ -886,7 +944,7 @@ public void getAuthorizationCode_withValidDetails_thenSuccessResposne() throws E } @Test - public void getAuthorizationCode_withInValidAcceptedClaim_thenErrorResposne() throws Exception { + public void getAuthorizationCode_withInValidAcceptedClaim_thenErrorResponse() throws Exception { AuthCodeRequest authCodeRequest = new AuthCodeRequest(); authCodeRequest.setTransactionId("1234567890"); authCodeRequest.setAcceptedClaims(Arrays.asList("name","")); From c0c112c3527d65ff6ef96652b4389358f97bc14c Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Wed, 21 Feb 2024 22:08:08 +0530 Subject: [PATCH 7/8] reviwed changes Signed-off-by: Venkata Saidurga Polamraju --- .../AuthorizationControllerTest.java | 95 +++++++++++++++++-- 1 file changed, 85 insertions(+), 10 deletions(-) diff --git a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java index 2d987cb57..2dfc69755 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/controllers/AuthorizationControllerTest.java @@ -9,6 +9,7 @@ import io.mosip.esignet.api.dto.AuthChallenge; import io.mosip.esignet.api.spi.AuditPlugin; import io.mosip.esignet.core.dto.*; +import io.mosip.esignet.core.dto.Error; import io.mosip.esignet.core.dto.vci.ParsedAccessToken; import io.mosip.esignet.core.exception.EsignetException; import io.mosip.esignet.core.spi.AuthorizationService; @@ -18,6 +19,7 @@ import io.mosip.esignet.services.AuthorizationHelperService; import io.mosip.esignet.services.CacheUtilService; import io.mosip.esignet.vci.services.VCICacheService; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,6 +29,7 @@ import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -37,6 +40,7 @@ import java.util.List; import static io.mosip.esignet.core.constants.Constants.UTC_DATETIME_PATTERN; +import static io.mosip.esignet.core.constants.ErrorConstants.*; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -814,6 +818,72 @@ public void authenticateEndUser_withInvalidFormat_returnErrorResponse() throws E .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format_for_auth_factor_type")); } + @Test + public void authenticateEndUser_withNullAuthFactorType_returnErrorResponse() throws Exception { + AuthRequest authRequest = new AuthRequest(); + authRequest.setIndividualId("1234567890"); + authRequest.setTransactionId("1234567890"); + + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setChallenge("1234567890"); + authChallenge.setAuthFactorType(null); + authChallenge.setFormat("jwt"); + + List authChallengeList = new ArrayList<>(); + authChallengeList.add(authChallenge); + + authRequest.setChallengeList(authChallengeList); + + RequestWrapper wrapper = new RequestWrapper<>(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(authRequest); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + MvcResult mvcResult=mockMvc.perform(post("/authorization/v2/authenticate") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andReturn(); + + List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE); + ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); + Assert.assertTrue(responseWrapper.getErrors().size() == 2); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); + } + + @Test + public void authenticateEndUser_withNullAuthChallenge_returnErrorResponse() throws Exception { + AuthRequest authRequest = new AuthRequest(); + authRequest.setIndividualId("1234567890"); + authRequest.setTransactionId("1234567890"); + + AuthChallenge authChallenge = new AuthChallenge(); + authChallenge.setChallenge("1234567890"); + authChallenge.setAuthFactorType(null); + authChallenge.setFormat(null); + + List authChallengeList = new ArrayList<>(); + authChallengeList.add(authChallenge); + + authRequest.setChallengeList(authChallengeList); + + RequestWrapper wrapper = new RequestWrapper<>(); + wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); + wrapper.setRequest(authRequest); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); + MvcResult mvcResult=mockMvc.perform(post("/authorization/v2/authenticate") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andReturn(); + + List errorCodes = Arrays.asList(INVALID_AUTH_FACTOR_TYPE, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE,INVALID_CHALLENGE_FORMAT); + ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); + Assert.assertTrue(responseWrapper.getErrors().size() == 3); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); + } + @Test public void authenticateEndUser_withBlankFormat_returnErrorResponse() throws Exception { AuthRequest authRequest = new AuthRequest(); @@ -834,13 +904,16 @@ public void authenticateEndUser_withBlankFormat_returnErrorResponse() throws Exc wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); wrapper.setRequest(authRequest); when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); - mockMvc.perform(post("/authorization/v2/authenticate") + MvcResult mvcResult=mockMvc.perform(post("/authorization/v2/authenticate") .content(objectMapper.writeValueAsString(wrapper)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.errors").isNotEmpty()) - .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT)) - .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0].format: invalid_challenge_format")); + .andExpect(status().isOk()).andReturn(); + List errorCodes = Arrays.asList(INVALID_CHALLENGE_FORMAT, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE); + ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); + Assert.assertTrue(responseWrapper.getErrors().size() == 2); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); + } @Test @@ -863,13 +936,15 @@ public void authenticateEndUser_withNullFormat_returnErrorResponse() throws Exce wrapper.setRequestTime(IdentityProviderUtil.getUTCDateTime()); wrapper.setRequest(authRequest); when(authorizationService.authenticateUserV2(authRequest)).thenReturn(new AuthResponseV2()); - mockMvc.perform(post("/authorization/v2/authenticate") + MvcResult mvcResult=mockMvc.perform(post("/authorization/v2/authenticate") .content(objectMapper.writeValueAsString(wrapper)) .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.errors").isNotEmpty()) - .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE)) - .andExpect(jsonPath("$.errors[0].errorMessage").value("request.challengeList[0]: invalid_challenge_format_for_auth_factor_type")); + .andExpect(status().isOk()).andReturn(); + List errorCodes = Arrays.asList(INVALID_CHALLENGE_FORMAT, INVALID_CHALLENGE_FORMAT_FOR_AUTH_FACTOR_TYPE); + ResponseWrapper responseWrapper = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ResponseWrapper.class); + Assert.assertTrue(responseWrapper.getErrors().size() == 2); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(0)).getErrorCode())); + Assert.assertTrue(errorCodes.contains(((Error)responseWrapper.getErrors().get(1)).getErrorCode())); } @Test From b316dd94b5c98f0466161e0ef3546400ba6a9e66 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Thu, 22 Feb 2024 09:15:36 +0530 Subject: [PATCH 8/8] reviwed changes Signed-off-by: Venkata Saidurga Polamraju --- .../src/main/resources/application-local.properties | 2 +- .../test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java | 2 +- .../java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java | 4 ++-- .../src/test/resources/application-test.properties | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/esignet-service/src/main/resources/application-local.properties b/esignet-service/src/main/resources/application-local.properties index 7fa4025bd..13ad8f6e1 100644 --- a/esignet-service/src/main/resources/application-local.properties +++ b/esignet-service/src/main/resources/application-local.properties @@ -12,7 +12,7 @@ mosip.esignet.access-token-expire-seconds=3600 mosip.esignet.link-code-expire-in-secs=60 mosip.esignet.authentication-expire-in-secs=60 mosip.esignet.cnonce-expire-seconds=20 -mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt'} +mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt', 'PIN': 'number', 'KBA': 'base64url-encoded-json'} mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \ '${server.servlet.path}/authorization/authenticate', \ diff --git a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java index 63031bb3c..c9859af7d 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthCodeFlowTest.java @@ -283,7 +283,7 @@ private ResponseWrapper authenticate(String transactionId) throws AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("34789"); - authChallenge.setFormat("numeric"); + authChallenge.setFormat("number"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); diff --git a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java index 31d128d02..31bcbfdaa 100644 --- a/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java +++ b/esignet-service/src/test/java/io/mosip/esignet/flows/AuthorizationAPIFlowTest.java @@ -403,7 +403,7 @@ private ResponseWrapper authenticateWithInvalidPin(String transact AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("1234453"); - authChallenge.setFormat("numeric"); + authChallenge.setFormat("number"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); @@ -428,7 +428,7 @@ private ResponseWrapper authenticate(String transactionId) throws AuthChallenge authChallenge = new AuthChallenge(); authChallenge.setAuthFactorType("PIN"); authChallenge.setChallenge("34789"); - authChallenge.setFormat("numeric"); + authChallenge.setFormat("number"); kycAuthDto.setChallengeList(Arrays.asList(authChallenge)); kycAuthDto.setTransactionId(transactionId); diff --git a/esignet-service/src/test/resources/application-test.properties b/esignet-service/src/test/resources/application-test.properties index 51e277339..7e53ef823 100644 --- a/esignet-service/src/test/resources/application-test.properties +++ b/esignet-service/src/test/resources/application-test.properties @@ -10,7 +10,7 @@ mosip.esignet.supported-id-regex=\\S* mosip.esignet.id-token-expire-seconds=3600 mosip.esignet.access-token.expire.seconds=3600 mosip.esignet.link-code-expire-in-secs=60 -mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt', 'PIN': 'numeric'} +mosip.esignet.supported-formats={'OTP': 'alpha-numeric', 'PWD': 'alpha-numeric', 'BIO': 'encoded-json', 'WLA': 'jwt', 'PIN': 'number', 'KBA': 'base64url-encoded-json'} mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authorization/send-otp', \ '${server.servlet.path}/authorization/authenticate', \