1 /* 2 * Copyright 2023 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.webauthn 18 19 import android.util.Log 20 import androidx.annotation.RestrictTo 21 import org.json.JSONObject 22 23 @RestrictTo(RestrictTo.Scope.LIBRARY) 24 class PublicKeyCredentialCreationOptions(requestJson: String) { 25 val json: JSONObject 26 27 val rp: PublicKeyCredentialRpEntity 28 val user: PublicKeyCredentialUserEntity 29 val challenge: ByteArray 30 val pubKeyCredParams: List<PublicKeyCredentialParameters> 31 32 var timeout: Long 33 var excludeCredentials: List<PublicKeyCredentialDescriptor> 34 var authenticatorSelection: AuthenticatorSelectionCriteria 35 var attestation: String 36 37 init { 38 json = JSONObject(requestJson) 39 val challengeString = json.getString("challenge") 40 challenge = WebAuthnUtils.b64Decode(challengeString) 41 val rpJson = json.getJSONObject("rp") 42 rp = PublicKeyCredentialRpEntity(rpJson.getString("name"), rpJson.getString("id")) 43 val rpUser = json.getJSONObject("user") 44 val userId = WebAuthnUtils.b64Decode(rpUser.getString("id")) 45 user = 46 PublicKeyCredentialUserEntity( 47 rpUser.getString("name"), 48 userId, 49 rpUser.getString("displayName") 50 ) 51 val pubKeyCredParamsJson = json.getJSONArray("pubKeyCredParams") 52 val pubKeyCredParamsTmp: MutableList<PublicKeyCredentialParameters> = mutableListOf() 53 for (i in 0 until pubKeyCredParamsJson.length()) { 54 val e = pubKeyCredParamsJson.getJSONObject(i) 55 pubKeyCredParamsTmp.add( 56 PublicKeyCredentialParameters(e.getString("type"), e.getLong("alg")) 57 ) 58 } 59 pubKeyCredParams = pubKeyCredParamsTmp.toList() 60 61 timeout = json.optLong("timeout", 0) 62 // TODO: Fix excludeCredentials and authenticatorSelection 63 excludeCredentials = emptyList() 64 authenticatorSelection = AuthenticatorSelectionCriteria("platform", "required") 65 attestation = json.optString("attestation", "none") 66 67 Log.i("WebAuthn", "Challenge $challenge()") 68 Log.i("WebAuthn", "rp $rp") 69 Log.i("WebAuthn", "user $user") 70 Log.i("WebAuthn", "pubKeyCredParams $pubKeyCredParams") 71 Log.i("WebAuthn", "timeout $timeout") 72 Log.i("WebAuthn", "excludeCredentials $excludeCredentials") 73 Log.i("WebAuthn", "authenticatorSelection $authenticatorSelection") 74 Log.i("WebAuthn", "attestation $attestation") 75 } 76 } 77