1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.statementservice.domain
18 
19 import android.content.pm.verify.domain.DomainVerificationInfo
20 import android.content.pm.verify.domain.DomainVerificationManager
21 
22 /**
23  * Wraps known [DomainVerificationManager] status codes so that they can be used in a when
24  * statement. Unknown codes are coerced to [VerifyStatus.UNKNOWN] and should be treated as
25  * unverified.
26  *
27  * Also includes error codes specific to this implementation of the domain verification agent.
28  * These must be stable across all versions, as codes are persisted to disk. They do not
29  * technically have to be stable across different device factory resets, since they will be reset
30  * once the apps are re-initialized, but easier to keep them unique forever.
31  */
32 enum class VerifyStatus(val value: Int) {
33     NO_RESPONSE(DomainVerificationInfo.STATE_NO_RESPONSE),
34     SUCCESS(DomainVerificationInfo.STATE_SUCCESS),
35 
36     UNKNOWN(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED),
37     FAILURE_LEGACY_UNSUPPORTED_WILDCARD(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 1),
38     FAILURE_REJECTED_BY_SERVER(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 2),
39     FAILURE_TIMEOUT(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 3),
40     FAILURE_UNKNOWN(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 4),
41     FAILURE_REDIRECT(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 5),
42 
43     // Failed to retrieve signature information from PackageManager
44     FAILURE_PACKAGE_MANAGER(DomainVerificationInfo.STATE_FIRST_VERIFIER_DEFINED + 6);
45 
46     companion object {
47         fun shouldRetry(state: Int): Boolean {
48             if (state == DomainVerificationInfo.STATE_UNMODIFIABLE) {
49                 return false
50             }
51 
52             val status = values().find { it.value == state } ?: return true
53             return when (status) {
54                 SUCCESS,
55                 FAILURE_LEGACY_UNSUPPORTED_WILDCARD,
56                 FAILURE_REJECTED_BY_SERVER,
57                 FAILURE_PACKAGE_MANAGER,
58                 UNKNOWN -> false
59                 NO_RESPONSE,
60                 FAILURE_TIMEOUT,
61                 FAILURE_UNKNOWN,
62                 FAILURE_REDIRECT -> true
63             }
64         }
65     }
66 }
67