Skip to content

Commit 07b8e9d

Browse files
Feature/validation errors (eu-digital-green-certificates#48)
Added different validation errors logic
1 parent d33d87a commit 07b8e9d

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed

app/src/main/java/dgca/verifier/app/android/verification/VerificationDialogFragment.kt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ import dgca.verifier.app.android.*
4444
import dgca.verifier.app.android.databinding.DialogFragmentVerificationBinding
4545
import dgca.verifier.app.android.model.CertificateData
4646
import dgca.verifier.app.android.model.CertificateModel
47-
import dgca.verifier.app.decoder.model.VerificationResult
4847

4948
@ExperimentalUnsignedTypes
5049
@AndroidEntryPoint
@@ -84,9 +83,12 @@ class VerificationDialogFragment : BottomSheetDialogFragment() {
8483

8584
viewModel.verificationResult.observe(viewLifecycleOwner, {
8685
setCertStatusUI(it.isValid())
87-
setCertStatusError(it)
86+
8887
setCertDataVisibility(it.isValid())
8988
})
89+
viewModel.verificationError.observe(viewLifecycleOwner, {
90+
setCertStatusError(it)
91+
})
9092
viewModel.certificate.observe(viewLifecycleOwner, { certificate ->
9193
if (certificate != null) {
9294
toggleButton(certificate)
@@ -145,16 +147,17 @@ class VerificationDialogFragment : BottomSheetDialogFragment() {
145147
binding.actionBtn.isVisible = true
146148
}
147149

148-
private fun setCertStatusError(verificationResult: VerificationResult) {
149-
if (verificationResult.isSignatureInvalid()) {
150-
binding.reasonForCertificateInvalidityTitle.visibility = View.VISIBLE
151-
binding.reasonForCertificateInvalidityName.visibility = View.VISIBLE
152-
binding.reasonForCertificateInvalidityName.text =
153-
getString(R.string.cryptographic_signature_not_valid)
154-
} else {
155-
binding.reasonForCertificateInvalidityTitle.visibility = View.GONE
156-
binding.reasonForCertificateInvalidityName.visibility = View.GONE
157-
}
150+
private fun setCertStatusError(verificationError: VerificationError) {
151+
binding.reasonForCertificateInvalidityTitle.visibility = View.VISIBLE
152+
binding.reasonForCertificateInvalidityName.visibility = View.VISIBLE
153+
binding.reasonForCertificateInvalidityName.text = getString(
154+
when (verificationError) {
155+
VerificationError.CERTIFICATE_EXPIRED -> R.string.certificate_is_expired
156+
VerificationError.CERTIFICATE_REVOKED -> R.string.certificate_was_revoked
157+
VerificationError.VERIFICATION_FAILED -> R.string.verification_failed
158+
VerificationError.CRYPTOGRAPHIC_SIGNATURE_INVALID -> R.string.cryptographic_signature_invalid
159+
}
160+
)
158161
}
159162

160163
private fun setCertDataVisibility(isValid: Boolean) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* ---license-start
3+
* eu-digital-green-certificates / dgca-verifier-app-android
4+
* ---
5+
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
6+
* ---
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* ---license-end
19+
*
20+
* Created by osarapulov on 5/24/21 11:39 PM
21+
*/
22+
23+
package dgca.verifier.app.android.verification
24+
25+
import dgca.verifier.app.decoder.model.VerificationResult
26+
27+
enum class VerificationError {
28+
CERTIFICATE_EXPIRED, CERTIFICATE_REVOKED, VERIFICATION_FAILED, CRYPTOGRAPHIC_SIGNATURE_INVALID
29+
}
30+
31+
internal fun VerificationResult.fetchError(noPublicKeysFound: Boolean): VerificationError? =
32+
when {
33+
isValid() -> null
34+
!isNotExpired -> VerificationError.CERTIFICATE_EXPIRED
35+
noPublicKeysFound -> VerificationError.VERIFICATION_FAILED
36+
else -> VerificationError.CRYPTOGRAPHIC_SIGNATURE_INVALID
37+
}

app/src/main/java/dgca/verifier/app/android/verification/VerificationViewModel.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class VerificationViewModel @Inject constructor(
6262
private val _verificationResult = MutableLiveData<VerificationResult>()
6363
val verificationResult: LiveData<VerificationResult> = _verificationResult
6464

65+
private val _verificationError = MutableLiveData<VerificationError>()
66+
val verificationError: LiveData<VerificationError> = _verificationError
67+
6568
private val _certificate = MutableLiveData<CertificateModel?>()
6669
val certificate: LiveData<CertificateModel?> = _certificate
6770

@@ -77,6 +80,7 @@ class VerificationViewModel @Inject constructor(
7780
_inProgress.value = true
7881
var greenCertificate: GreenCertificate? = null
7982
val verificationResult = VerificationResult()
83+
var noPublicKeysFound = true
8084

8185
withContext(Dispatchers.IO) {
8286
val plainInput = prefixValidationService.decode(code, verificationResult)
@@ -104,6 +108,7 @@ class VerificationViewModel @Inject constructor(
104108
Timber.d("Verification failed: failed to load certificate")
105109
return@withContext
106110
}
111+
noPublicKeysFound = false
107112
certificates.forEach { innerCertificate ->
108113
cryptoService.validate(cose, innerCertificate, verificationResult)
109114
if (verificationResult.coseVerified) {
@@ -112,6 +117,8 @@ class VerificationViewModel @Inject constructor(
112117
}
113118
}
114119

120+
verificationResult.fetchError(noPublicKeysFound)?.apply { _verificationError.value = this }
121+
115122
_inProgress.value = false
116123
_verificationResult.value = verificationResult
117124
_certificate.value = greenCertificate?.toCertificateModel()

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@
5353
<string name="version">Version: %1$s</string>
5454
<string name="about">About</string>
5555
<string name="settings_version_title">Verifier App</string>
56-
<string name="cryptographic_signature_not_valid">Cryptographic signature not valid.</string>
56+
<string name="certificate_is_expired">Certificate is expired.</string>
57+
<string name="certificate_was_revoked">Certificate was revoked.</string>
58+
<string name="verification_failed">Verification failed.</string>
59+
<string name="cryptographic_signature_invalid">Cryptographic signature invalid.</string>
5760
</resources>

0 commit comments

Comments
 (0)