1 /*
2  * Copyright 2022 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
18 
19 import android.os.Bundle
20 import androidx.credentials.internal.FrameworkClassParsingException
21 import androidx.credentials.internal.RequestValidationHelper
22 
23 /**
24  * Represents the user's passkey credential granted by the user for app sign-in.
25  *
26  * @property authenticationResponseJson the public key credential authentication response in JSON
27  *   format that follows the standard webauthn json format shown at
28  *   [this w3c link](https://w3c.github.io/webauthn/#dictdef-authenticationresponsejson)
29  */
30 class PublicKeyCredential
31 private constructor(
32     val authenticationResponseJson: String,
33     data: Bundle,
34 ) : Credential(TYPE_PUBLIC_KEY_CREDENTIAL, data) {
35 
36     /**
37      * Constructs a [PublicKeyCredential].
38      *
39      * @param authenticationResponseJson the public key credential authentication response in JSON
40      *   format that follows the standard webauthn json format shown at
41      *   [this w3c link](https://w3c.github.io/webauthn/#dictdef-authenticationresponsejson)
42      * @throws NullPointerException If [authenticationResponseJson] is null
43      * @throws IllegalArgumentException If [authenticationResponseJson] is empty, or if it is not a
44      *   valid JSON
45      */
46     constructor(
47         authenticationResponseJson: String
48     ) : this(authenticationResponseJson, toBundle(authenticationResponseJson))
49 
<lambda>null50     init {
51         require(RequestValidationHelper.isValidJSON(authenticationResponseJson)) {
52             "authenticationResponseJson must not be empty, and must be a valid JSON"
53         }
54     }
55 
56     /** Companion constants / helpers for [PublicKeyCredential]. */
57     companion object {
58         /** The type value for public key credential related operations. */
59         const val TYPE_PUBLIC_KEY_CREDENTIAL: String =
60             "androidx.credentials.TYPE_PUBLIC_KEY_CREDENTIAL"
61 
62         /** The Bundle key value for the public key credential subtype (privileged or regular). */
63         internal const val BUNDLE_KEY_SUBTYPE = "androidx.credentials.BUNDLE_KEY_SUBTYPE"
64         internal const val BUNDLE_KEY_AUTHENTICATION_RESPONSE_JSON =
65             "androidx.credentials.BUNDLE_KEY_AUTHENTICATION_RESPONSE_JSON"
66 
67         @JvmStatic
toBundlenull68         internal fun toBundle(authenticationResponseJson: String): Bundle {
69             val bundle = Bundle()
70             bundle.putString(BUNDLE_KEY_AUTHENTICATION_RESPONSE_JSON, authenticationResponseJson)
71             return bundle
72         }
73 
74         @JvmStatic
createFromnull75         internal fun createFrom(data: Bundle): PublicKeyCredential {
76             try {
77                 val authenticationResponseJson =
78                     data.getString(BUNDLE_KEY_AUTHENTICATION_RESPONSE_JSON)
79                 return PublicKeyCredential(authenticationResponseJson!!, data)
80             } catch (e: Exception) {
81                 throw FrameworkClassParsingException()
82             }
83         }
84     }
85 }
86