1 /*
2 * Copyright 2024 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 androidx.credentials.internal
18
19 import android.content.Context
20 import android.graphics.drawable.Icon
21 import android.os.Build
22 import android.os.Bundle
23 import androidx.annotation.RequiresApi
24 import androidx.annotation.RestrictTo
25 import androidx.credentials.CreateCredentialRequest
26 import androidx.credentials.CreatePasswordRequest
27 import androidx.credentials.CreatePublicKeyCredentialRequest
28 import androidx.credentials.R
29 import androidx.credentials.exceptions.CreateCredentialCancellationException
30 import androidx.credentials.exceptions.CreateCredentialCustomException
31 import androidx.credentials.exceptions.CreateCredentialException
32 import androidx.credentials.exceptions.CreateCredentialInterruptedException
33 import androidx.credentials.exceptions.CreateCredentialNoCreateOptionException
34 import androidx.credentials.exceptions.CreateCredentialProviderConfigurationException
35 import androidx.credentials.exceptions.CreateCredentialUnknownException
36 import androidx.credentials.exceptions.CreateCredentialUnsupportedException
37 import androidx.credentials.exceptions.GetCredentialCancellationException
38 import androidx.credentials.exceptions.GetCredentialCustomException
39 import androidx.credentials.exceptions.GetCredentialException
40 import androidx.credentials.exceptions.GetCredentialInterruptedException
41 import androidx.credentials.exceptions.GetCredentialProviderConfigurationException
42 import androidx.credentials.exceptions.GetCredentialUnknownException
43 import androidx.credentials.exceptions.GetCredentialUnsupportedException
44 import androidx.credentials.exceptions.NoCredentialException
45 import androidx.credentials.exceptions.publickeycredential.CreatePublicKeyCredentialDomException
46 import androidx.credentials.exceptions.publickeycredential.CreatePublicKeyCredentialException
47 import androidx.credentials.exceptions.publickeycredential.GetPublicKeyCredentialDomException
48 import androidx.credentials.exceptions.publickeycredential.GetPublicKeyCredentialException
49
50 /** Take the create request's `credentialData` and add SDK specific values to it. */
51 @RequiresApi(Build.VERSION_CODES.M)
52 @RestrictTo(RestrictTo.Scope.LIBRARY)
getFinalCreateCredentialDatanull53 fun getFinalCreateCredentialData(
54 request: CreateCredentialRequest,
55 context: Context,
56 ): Bundle {
57 val createCredentialData = request.credentialData
58 val displayInfoBundle = request.displayInfo.toBundle()
59 displayInfoBundle.putParcelable(
60 CreateCredentialRequest.DisplayInfo.BUNDLE_KEY_CREDENTIAL_TYPE_ICON,
61 Icon.createWithResource(
62 context,
63 when (request) {
64 is CreatePasswordRequest -> R.drawable.ic_password
65 is CreatePublicKeyCredentialRequest -> R.drawable.ic_passkey
66 else -> R.drawable.ic_other_sign_in
67 }
68 )
69 )
70 createCredentialData.putBundle(
71 CreateCredentialRequest.DisplayInfo.BUNDLE_KEY_REQUEST_DISPLAY_INFO,
72 displayInfoBundle
73 )
74 return createCredentialData
75 }
76
77 @RestrictTo(RestrictTo.Scope.LIBRARY)
toJetpackGetExceptionnull78 fun toJetpackGetException(errorType: String, errorMsg: CharSequence?): GetCredentialException {
79
80 return when (errorType) {
81 android.credentials.GetCredentialException.TYPE_NO_CREDENTIAL ->
82 NoCredentialException(errorMsg)
83 android.credentials.GetCredentialException.TYPE_USER_CANCELED ->
84 GetCredentialCancellationException(errorMsg)
85 android.credentials.GetCredentialException.TYPE_INTERRUPTED ->
86 GetCredentialInterruptedException(errorMsg)
87 android.credentials.GetCredentialException.TYPE_UNKNOWN ->
88 GetCredentialUnknownException(errorMsg)
89 GetCredentialProviderConfigurationException
90 .TYPE_GET_CREDENTIAL_PROVIDER_CONFIGURATION_EXCEPTION ->
91 GetCredentialProviderConfigurationException(errorMsg)
92 GetCredentialUnsupportedException.TYPE_GET_CREDENTIAL_UNSUPPORTED_EXCEPTION ->
93 GetCredentialUnsupportedException(errorMsg)
94 else -> {
95 if (
96 errorType.startsWith(
97 GetPublicKeyCredentialDomException.TYPE_GET_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION
98 )
99 ) {
100 GetPublicKeyCredentialException.createFrom(errorType, errorMsg?.toString())
101 } else {
102 GetCredentialCustomException(errorType, errorMsg)
103 }
104 }
105 }
106 }
107
108 @RestrictTo(RestrictTo.Scope.LIBRARY)
toJetpackCreateExceptionnull109 fun toJetpackCreateException(
110 errorType: String,
111 errorMsg: CharSequence? = null
112 ): CreateCredentialException {
113 return when (errorType) {
114 android.credentials.CreateCredentialException.TYPE_NO_CREATE_OPTIONS ->
115 CreateCredentialNoCreateOptionException(errorMsg)
116 android.credentials.CreateCredentialException.TYPE_USER_CANCELED ->
117 CreateCredentialCancellationException(errorMsg)
118 android.credentials.CreateCredentialException.TYPE_INTERRUPTED ->
119 CreateCredentialInterruptedException(errorMsg)
120 android.credentials.CreateCredentialException.TYPE_UNKNOWN ->
121 CreateCredentialUnknownException(errorMsg)
122 CreateCredentialProviderConfigurationException
123 .TYPE_CREATE_CREDENTIAL_PROVIDER_CONFIGURATION_EXCEPTION ->
124 CreateCredentialProviderConfigurationException(errorMsg)
125 CreateCredentialUnsupportedException.TYPE_CREATE_CREDENTIAL_UNSUPPORTED_EXCEPTION ->
126 CreateCredentialUnsupportedException(errorMsg)
127 else -> {
128 if (
129 errorType.startsWith(
130 CreatePublicKeyCredentialDomException
131 .TYPE_CREATE_PUBLIC_KEY_CREDENTIAL_DOM_EXCEPTION
132 )
133 ) {
134 CreatePublicKeyCredentialException.createFrom(errorType, errorMsg?.toString())
135 } else {
136 CreateCredentialCustomException(errorType, errorMsg)
137 }
138 }
139 }
140 }
141