diff --git a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/constants/CredentialValidatorConstants.kt b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/constants/CredentialValidatorConstants.kt index 39ece44..bf43ebe 100644 --- a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/constants/CredentialValidatorConstants.kt +++ b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/constants/CredentialValidatorConstants.kt @@ -67,6 +67,10 @@ object CredentialValidatorConstants { const val ERROR_MESSAGE_NAME = "${VALIDATION_ERROR}name should be string or array of Language Object" const val ERROR_MESSAGE_DESCRIPTION = "${VALIDATION_ERROR}description should be string or array of Language Object" + const val ERROR_MESSAGE_INVALID_VALID_FROM_MSO = "invalid validFrom in the MSO of the credential" + const val ERROR_CODE_INVALID_VALID_FROM_MSO = "${ERROR_CODE_INVALID}VALID_FROM_MSO" + const val ERROR_MESSAGE_INVALID_VALID_UNTIL_MSO = "invalid validUntil in the MSO of the credential" + const val ERROR_CODE_INVALID_VALID_UNTIL_MSO = "${ERROR_CODE_INVALID}VALID_UNTIL_MSO" const val ERROR_MESSAGE_INVALID_DATE_MSO = "invalid validUntil / validFrom in the MSO of the credential" const val ERROR_CODE_INVALID_DATE_MSO = "${ERROR_CODE_INVALID}DATE_MSO" diff --git a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidator.kt b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidator.kt index 7002df5..d2da968 100644 --- a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidator.kt +++ b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidator.kt @@ -5,12 +5,17 @@ import co.nstant.`in`.cbor.model.MajorType import co.nstant.`in`.cbor.model.Map import co.nstant.`in`.cbor.model.UnicodeString import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_DATE_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_VALID_FROM_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_VALID_UNTIL_MSO import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_DATE_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_VALID_FROM_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_VALID_UNTIL_MSO import io.mosip.vercred.vcverifier.credentialverifier.types.msomdoc.MsoMdocVerifiableCredential import io.mosip.vercred.vcverifier.credentialverifier.types.msomdoc.extractMso import io.mosip.vercred.vcverifier.exception.UnknownException import io.mosip.vercred.vcverifier.exception.ValidationException import io.mosip.vercred.vcverifier.utils.DateUtils +import io.mosip.vercred.vcverifier.utils.DateUtils.parseDate import java.util.logging.Logger class MsoMdocValidator { @@ -33,18 +38,29 @@ class MsoMdocValidator { logger.severe("validUntil / validFrom is not available in the credential's MSO") throw ValidationException(ERROR_MESSAGE_INVALID_DATE_MSO, ERROR_CODE_INVALID_DATE_MSO) } - val isCurrentTimeGreaterThanValidFrom = - DateUtils.isDatePassedCurrentDate(validFrom.toString()) - val isCurrentTimeLessThanValidUntil = - !DateUtils.isDatePassedCurrentDate(validUntil.toString()) + val isValidFromIsFutureDate = + DateUtils.isFutureDateWithTolerance(validFrom.toString()) + val isValidUntilIsPastDate = + !DateUtils.isFutureDateWithTolerance(validUntil.toString()) val isValidUntilGreaterThanValidFrom: Boolean = - DateUtils.parseDate(validUntil.toString())?.after( - DateUtils.parseDate( + parseDate(validUntil.toString())?.after( + parseDate( validFrom.toString() ) ?: return false ) ?: false - if (!(isCurrentTimeLessThanValidUntil && isCurrentTimeGreaterThanValidFrom && isValidUntilGreaterThanValidFrom)) { - logger.severe("Error while doing validity verification - invalid validUntil / validFrom in the MSO of the credential") + + if(isValidFromIsFutureDate){ + logger.severe("Error while doing validity verification - invalid validFrom in the MSO of the credential") + throw ValidationException(ERROR_MESSAGE_INVALID_VALID_FROM_MSO, ERROR_CODE_INVALID_VALID_FROM_MSO) + } + + if(isValidUntilIsPastDate){ + logger.severe("Error while doing validity verification - invalid validUntil in the MSO of the credential") + throw ValidationException(ERROR_MESSAGE_INVALID_VALID_UNTIL_MSO, ERROR_CODE_INVALID_VALID_UNTIL_MSO) + } + + if(!isValidUntilGreaterThanValidFrom){ + logger.severe("Error while doing validity verification - invalid validFrom / validUntil in the MSO of the credential") throw ValidationException(ERROR_MESSAGE_INVALID_DATE_MSO, ERROR_CODE_INVALID_DATE_MSO) } return true diff --git a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/utils/DateUtils.kt b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/utils/DateUtils.kt index 33c5992..86cd1b9 100644 --- a/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/utils/DateUtils.kt +++ b/vc-verifier/kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/utils/DateUtils.kt @@ -27,7 +27,7 @@ object DateUtils { private val logger = Logger.getLogger(DateUtils::class.java.name) - private val dateFormats = listOf( + val dateFormats = listOf( ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"), ("yyyy-MM-dd'T'HH:mm:ss'Z'") ) @@ -38,22 +38,6 @@ object DateUtils { return DATE_REGEX.matches(dateValue) } - fun isDatePassedCurrentDate(inputDateString: String): Boolean { - return try { - val inputDate: Date? = parseDate(inputDateString) - if (inputDate == null) { - logger.severe("Given date is not available in supported date formats") - return false - } - - val currentDate = Calendar.getInstance(TimeZone.getTimeZone(UTC)).time - inputDate.before(currentDate) - } catch (e: Exception) { - logger.severe("Error while comparing dates ${e.message}") - false - } - } - fun parseDate(date: String): Date? { dateFormats.forEach { try { @@ -78,10 +62,9 @@ object DateUtils { } - val issuanceDate: Date = parseDate(vcJsonObject.optString(ISSUANCE_DATE)) - ?: throw ValidationException(ERROR_ISSUANCE_DATE_INVALID, "${ERROR_CODE_INVALID}${ISSUANCE_DATE}") + val issuanceDate = vcJsonObject.optString(ISSUANCE_DATE) ?: "" - if (issuanceDate.isFutureDateWithTolerance()) { + if (isFutureDateWithTolerance(issuanceDate)) { throw ValidationException(ERROR_CURRENT_DATE_BEFORE_ISSUANCE_DATE, ERROR_CODE_CURRENT_DATE_BEFORE_ISSUANCE_DATE ) @@ -100,7 +83,7 @@ object DateUtils { } } - if (vcJsonObject.has(VALID_FROM) && !isDatePassedCurrentDate( + if (vcJsonObject.has(VALID_FROM) && isFutureDateWithTolerance( vcJsonObject.optString( VALID_FROM ) @@ -111,15 +94,20 @@ object DateUtils { } fun isVCExpired(inputDate: String): Boolean { - return inputDate.isNotEmpty() && isDatePassedCurrentDate(inputDate) + return inputDate.isNotEmpty() && !isFutureDateWithTolerance(inputDate) } -} + fun isFutureDateWithTolerance(inputDateString: String, toleranceInMilliSeconds: Long = 3000): Boolean { + val inputDate: Date? = parseDate(inputDateString) + if (inputDate == null) { + logger.severe("Given date is not available in supported date formats") + return false + } + val currentTime = System.currentTimeMillis() + val inputDateTime = inputDate.time -fun Date.isFutureDateWithTolerance(toleranceInMilliSeconds: Long = 3000): Boolean { - val currentTime = System.currentTimeMillis() - val inputDateTime = this.time + val upperBound = currentTime + toleranceInMilliSeconds + return inputDateTime > upperBound + } - val upperBound = currentTime + toleranceInMilliSeconds - return inputDateTime > upperBound } \ No newline at end of file diff --git a/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidatorTest.kt b/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidatorTest.kt index ffeba40..eedb8b3 100644 --- a/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidatorTest.kt +++ b/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/credentialverifier/validator/MsoMdocValidatorTest.kt @@ -4,8 +4,10 @@ import android.os.Build import io.mockk.clearAllMocks import io.mockk.every import io.mockk.mockkObject -import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_DATE_MSO -import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_DATE_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_VALID_FROM_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID_VALID_UNTIL_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_VALID_FROM_MSO +import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_VALID_UNTIL_MSO import io.mosip.vercred.vcverifier.exception.UnknownException import io.mosip.vercred.vcverifier.exception.ValidationException import io.mosip.vercred.vcverifier.utils.BuildConfig @@ -37,8 +39,8 @@ class MsoMdocValidatorTest { @Test fun `should return true when credential is successfully validated`() { - every { DateUtils.isDatePassedCurrentDate("2024-10-23T07:01:17Z") } returns true - every { DateUtils.isDatePassedCurrentDate("2026-10-23T07:01:17Z") } returns false + every { DateUtils.isFutureDateWithTolerance("2024-10-23T07:01:17Z") } returns false + every { DateUtils.isFutureDateWithTolerance("2026-10-23T07:01:17Z") } returns true val isVerified = MsoMdocValidator().validate( "omdkb2NUeXBldW9yZy5pc28uMTgwMTMuNS4xLm1ETGxpc3N1ZXJTaWduZWSiamlzc3VlckF1dGiEQ6EBJqEYIVkBxDCCAcAwggFloAMCAQICFH6lICTsAhkMivItOT9v6JeZubwmMAoGCCqGSM49BAMCME4xCzAJBgNVBAYTAk1LMQ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2BhYWKRoZGlnZXN0SUQAZnJhbmRvbVAFg1zMFq1oLYxHiib0UCeYcWVsZW1lbnRJZGVudGlmaWVyamJpcnRoX2RhdGVsZWxlbWVudFZhbHVlajE5OTQtMTEtMDbYGFhUpGhkaWdlc3RJRAdmcmFuZG9tUElZm1bdU7M1GlcrQPJ_ctNxZWxlbWVudElkZW50aWZpZXJqZ2l2ZW5fbmFtZWxlbGVtZW50VmFsdWVmSm9zZXBo2BhYVaRoZGlnZXN0SUQFZnJhbmRvbVB_NHtdmXkWLPqVnSgypGGWcWVsZW1lbnRJZGVudGlmaWVya2ZhbWlseV9uYW1lbGVsZW1lbnRWYWx1ZWZBZ2F0aGE=" @@ -49,27 +51,27 @@ class MsoMdocValidatorTest { @Test fun `should throw exception when current time is greater than validFrom`() { - every { DateUtils.isDatePassedCurrentDate("2024-10-23T07:01:17Z") } returns false - every { DateUtils.isDatePassedCurrentDate("2026-10-23T07:01:17Z") } returns false + every { DateUtils.isFutureDateWithTolerance("2024-10-23T07:01:17Z") } returns true + every { DateUtils.isFutureDateWithTolerance("2026-10-23T07:01:17Z") } returns true val verificationException = assertThrows(ValidationException::class.java){ MsoMdocValidator().validate("omdkb2NUeXBldW9yZy5pc28uMTgwMTMuNS4xLm1ETGxpc3N1ZXJTaWduZWSiamlzc3VlckF1dGiEQ6EBJqEYIVkBxDCCAcAwggFloAMCAQICFH6lICTsAhkMivItOT9v6JeZubwmMAoGCCqGSM49BAMCME4xCzAJBgNVBAYTAk1LMQ4wDAYDVQQIDAVNSy1LQTERMA8GA1UEBwwITW9ja0NpdHkxDTALBgNVBAoMBE1vY2sxDTALBgNVBAsMBE1vY2swHhcNMjQxMDIyMDcwMjUwWhcNMjUxMDIyMDcwMjUwWjBOMQswCQYDVQQGEwJNSzEOMAwGA1UECAwFTUstS0ExETAPBgNVBAcMCE1vY2tDaXR5MQ0wCwYDVQQKDARNb2NrMQ0wCwYDVQQLDARNb2NrMFkwEwYHKoZIzj0CAQYIKoZIzj0DA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JhbmRvbVB_NHtdmXkWLPqVnSgypGGWcWVsZW1lbnRJZGVudGlmaWVya2ZhbWlseV9uYW1lbGVsZW1lbnRWYWx1ZWZBZ2F0aGE=") } - assertEquals(ERROR_MESSAGE_INVALID_DATE_MSO,verificationException.errorMessage) - assertEquals(ERROR_CODE_INVALID_DATE_MSO,verificationException.errorCode) + assertEquals(ERROR_MESSAGE_INVALID_VALID_FROM_MSO,verificationException.errorMessage) + assertEquals(ERROR_CODE_INVALID_VALID_FROM_MSO,verificationException.errorCode) } @Test fun `should throw exception when current time is less than validUntil`() { - every { DateUtils.isDatePassedCurrentDate("2024-10-23T07:01:17Z") } returns true - every { DateUtils.isDatePassedCurrentDate("2026-10-23T07:01:17Z") } returns true + every { DateUtils.isFutureDateWithTolerance("2024-10-23T07:01:17Z") } returns false + every { DateUtils.isFutureDateWithTolerance("2026-10-23T07:01:17Z") } returns false val verificationException = assertThrows(ValidationException::class.java){ MsoMdocValidator().validate("omdkb2NUeXBldW9yZy5pc28uMTgwMTMuNS4xLm1ETGxpc3N1ZXJTaWduZWSiamlzc3VlckF1dGiEQ6EBJqEYIVkBxDCCAcAwggFloAMCAQICFH6lICTsAhkMivItOT9v6JeZubwmMAoGCCqGSM49BAMCME4xCzAJBgNVBAYTAk1LMQ4wDAYDVQQIDAVNSy1LQTERMA8GA1UEBwwITW9ja0NpdHkxDTALBgNVBAoMBE1vY2sxDTALBgNVBAsMBE1vY2swHhcNMjQxMDIyMDcwMjUwWhcNMjUxMDIyMDcwMjUwWjBOMQswCQYDVQQGEwJNSzEOMAwGA1UECAwFTUstS0ExETAPBgNVBAcMCE1vY2tDaXR5MQ0wCwYDVQQKDARNb2NrMQ0wCwYDVQQLDARNb2NrMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEjtRcOXgIyR_xqGB-M6d0qkrjQWOBGGdlPgfIfb2xW0egZAVEz_55IXCofWaprRGxX7qQTlNAZyByniay2jzhR6MhMB8wHQYDVR0OBBYEFNqAHypQY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ESSAGE_INVALID_DATE_MSO,verificationException.errorMessage) - assertEquals(ERROR_CODE_INVALID_DATE_MSO,verificationException.errorCode) + assertEquals(ERROR_MESSAGE_INVALID_VALID_UNTIL_MSO,verificationException.errorMessage) + assertEquals(ERROR_CODE_INVALID_VALID_UNTIL_MSO,verificationException.errorCode) } @Test diff --git a/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/utils/UtilsTest.kt b/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/utils/UtilsTest.kt index 127d5bb..f264aeb 100644 --- a/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/utils/UtilsTest.kt +++ b/vc-verifier/kotlin/vcverifier/src/test/java/io/mosip/vercred/vcverifier/utils/UtilsTest.kt @@ -1,8 +1,6 @@ package io.mosip.vercred.vcverifier.utils -import io.mockk.every -import io.mockk.mockk -import io.mockk.mockkStatic +import io.mosip.vercred.vcverifier.utils.DateUtils.dateFormats import org.json.JSONArray import org.junit.jupiter.api.Assertions.assertArrayEquals import org.junit.jupiter.api.Assertions.assertEquals @@ -10,8 +8,10 @@ import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import java.io.ByteArrayOutputStream -import java.util.Calendar +import java.text.SimpleDateFormat import java.util.Date +import java.util.Locale +import java.util.TimeZone class UtilsTest { @@ -19,6 +19,19 @@ class UtilsTest { private val utils = Util() private val dateUtils = DateUtils + private fun convertDateToUtcString(date: Date): String? { + dateFormats.forEach { + try { + val utcFormat = SimpleDateFormat(it, Locale.getDefault()).apply { + timeZone = TimeZone.getTimeZone("UTC") + } + return utcFormat.format(date) + } catch (_: Exception) { + } + } + return null + } + @Test fun `test validate date invalid`() { val result = dateUtils.isValidDate("123456789") @@ -69,22 +82,19 @@ class UtilsTest { @Test fun `date expired`() { - val result = dateUtils.isVCExpired("2024-09-02T17:36:13.644Z") + val result = dateUtils.isVCExpired("2024-11-27T13:49:13.644Z") assertTrue(result) } @Test fun `date not expired`() { - val calendar: Calendar = mockk(relaxed = true) - mockkStatic(Calendar::class) - every { Calendar.getInstance() } returns calendar - val result = dateUtils.isDatePassedCurrentDate("2024-11-02T17:36:13.644Z") + val result = dateUtils.isVCExpired("2034-11-02T17:36:13.644Z") assertFalse(result) } @Test fun `invalid date`() { - val result = dateUtils.isDatePassedCurrentDate("12345") + val result = dateUtils.isFutureDateWithTolerance("12345") assertFalse(result) } @@ -135,25 +145,32 @@ class UtilsTest { } @Test - fun `test when issuanceDate time is not future date and 10 seconds less than currentDate time `() { + fun `test when issuanceDate time is not future date and less than currentDate time `() { val currentDate = Date() - val issuanceDate = Date(currentDate.time-10000) - val result = issuanceDate.isFutureDateWithTolerance() + val issuanceDate = Date(currentDate.time - 10000) + val issuanceDateString = convertDateToUtcString(issuanceDate) + + val result = dateUtils.isFutureDateWithTolerance(issuanceDateString.orEmpty()) assertFalse(result) } @Test fun `test when issuanceDate time is not future date and 3 seconds less than currentDate time `() { val currentDate = Date() - val issuanceDate = Date(currentDate.time-3000) - val result = issuanceDate.isFutureDateWithTolerance() + val issuanceDate = Date(currentDate.time - 3000) + val issuanceDateString = convertDateToUtcString(issuanceDate) + + val result = dateUtils.isFutureDateWithTolerance(issuanceDateString.orEmpty()) assertFalse(result) } @Test fun `test when issuanceDate time equal to future date time`() { - val issuanceDate = Date() - val result = issuanceDate.isFutureDateWithTolerance() + val currentDate = Date() + val issuanceDate = Date(currentDate.time) + val issuanceDateString = convertDateToUtcString(issuanceDate) + + val result = dateUtils.isFutureDateWithTolerance(issuanceDateString.orEmpty()) assertFalse(result) } @@ -161,18 +178,26 @@ class UtilsTest { @Test fun `test when issuanceDate time is future date time but within tolerance range`() { val currentDate = Date() - val issuanceDate = Date(currentDate.time+3000) + val issuanceDate = Date(currentDate.time + 3000) + val issuanceDateString = convertDateToUtcString(issuanceDate) - val result = issuanceDate.isFutureDateWithTolerance() + val result = dateUtils.isFutureDateWithTolerance(issuanceDateString.orEmpty()) assertFalse(result) } @Test fun `test when issuanceDate time is future date time but outside tolerance range`() { val currentDate = Date() - val issuanceDate = Date(currentDate.time+5000) + val issuanceDate = Date(currentDate.time + 5000) + val issuanceDateString = convertDateToUtcString(issuanceDate) - val result = issuanceDate.isFutureDateWithTolerance() + val result = dateUtils.isFutureDateWithTolerance(issuanceDateString.orEmpty()) assertTrue(result) } + + @Test + fun `test when issuanceDate is empty`() { + val result = dateUtils.isFutureDateWithTolerance("") + assertFalse(result) + } } \ No newline at end of file