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  * A response of a public key credential (passkey) flow.
25  *
26  * @property registrationResponseJson the public key credential registration response in JSON format
27  */
28 class CreatePublicKeyCredentialResponse
29 private constructor(
30     val registrationResponseJson: String,
31     data: Bundle,
32 ) :
33     CreateCredentialResponse(
34         PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL,
35         data,
36     ) {
37 
38     /**
39      * Constructs a [CreatePublicKeyCredentialResponse].
40      *
41      * @param registrationResponseJson the public key credential registration response in JSON
42      *   format
43      * @throws NullPointerException If [registrationResponseJson] is null
44      * @throws IllegalArgumentException If [registrationResponseJson] is empty, or an invalid JSON
45      */
46     constructor(
47         registrationResponseJson: String
48     ) : this(registrationResponseJson, toBundle(registrationResponseJson))
49 
<lambda>null50     init {
51         require(RequestValidationHelper.isValidJSON(registrationResponseJson)) {
52             "registrationResponseJson must not be empty, and must be a valid JSON"
53         }
54     }
55 
56     internal companion object {
57         internal const val BUNDLE_KEY_REGISTRATION_RESPONSE_JSON =
58             "androidx.credentials.BUNDLE_KEY_REGISTRATION_RESPONSE_JSON"
59 
60         @JvmStatic
toBundlenull61         internal fun toBundle(registrationResponseJson: String): Bundle {
62             val bundle = Bundle()
63             bundle.putString(BUNDLE_KEY_REGISTRATION_RESPONSE_JSON, registrationResponseJson)
64             return bundle
65         }
66 
67         @JvmStatic
createFromnull68         internal fun createFrom(data: Bundle): CreatePublicKeyCredentialResponse {
69             try {
70                 val registrationResponseJson = data.getString(BUNDLE_KEY_REGISTRATION_RESPONSE_JSON)
71                 return CreatePublicKeyCredentialResponse(registrationResponseJson!!, data)
72             } catch (e: Exception) {
73                 throw FrameworkClassParsingException()
74             }
75         }
76     }
77 }
78