Skip to content

Commit 07a2a07

Browse files
cartlandgithub-actions[bot]
authored andcommitted
Apply Spotless
1 parent 6537d54 commit 07a2a07

File tree

26 files changed

+984
-773
lines changed

26 files changed

+984
-773
lines changed
Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
* https://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+
117
package com.example.identity.credentialmanager
218

319
import android.app.Activity
@@ -13,39 +29,39 @@ import androidx.credentials.exceptions.GetCredentialException
1329

1430
// This class is mostly copied from https://github.com/android/identity-samples/blob/main/WebView/CredentialManagerWebView/CredentialManagerHandler.kt.
1531
class CredentialManagerHandler(private val activity: Activity) {
16-
private val mCredMan = CredentialManager.create(activity.applicationContext)
17-
private val TAG = "CredentialManagerHandler"
18-
/**
19-
* Encapsulates the create passkey API for credential manager in a less error-prone manner.
20-
*
21-
* @param request a create public key credential request JSON required by [CreatePublicKeyCredentialRequest].
22-
* @return [CreatePublicKeyCredentialResponse] containing the result of the credential creation.
23-
*/
24-
suspend fun createPasskey(request: String): CreatePublicKeyCredentialResponse {
25-
val createRequest = CreatePublicKeyCredentialRequest(request)
26-
try {
27-
return mCredMan.createCredential(activity, createRequest) as CreatePublicKeyCredentialResponse
28-
} catch (e: CreateCredentialException) {
29-
// For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
30-
Log.i(TAG, "Error creating credential: ErrMessage: ${e.errorMessage}, ErrType: ${e.type}")
31-
throw e
32+
private val mCredMan = CredentialManager.create(activity.applicationContext)
33+
private val TAG = "CredentialManagerHandler"
34+
/**
35+
* Encapsulates the create passkey API for credential manager in a less error-prone manner.
36+
*
37+
* @param request a create public key credential request JSON required by [CreatePublicKeyCredentialRequest].
38+
* @return [CreatePublicKeyCredentialResponse] containing the result of the credential creation.
39+
*/
40+
suspend fun createPasskey(request: String): CreatePublicKeyCredentialResponse {
41+
val createRequest = CreatePublicKeyCredentialRequest(request)
42+
try {
43+
return mCredMan.createCredential(activity, createRequest) as CreatePublicKeyCredentialResponse
44+
} catch (e: CreateCredentialException) {
45+
// For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
46+
Log.i(TAG, "Error creating credential: ErrMessage: ${e.errorMessage}, ErrType: ${e.type}")
47+
throw e
48+
}
3249
}
33-
}
3450

35-
/**
36-
* Encapsulates the get passkey API for credential manager in a less error-prone manner.
37-
*
38-
* @param request a get public key credential request JSON required by [GetCredentialRequest].
39-
* @return [GetCredentialResponse] containing the result of the credential retrieval.
40-
*/
41-
suspend fun getPasskey(request: String): GetCredentialResponse {
42-
val getRequest = GetCredentialRequest(listOf(GetPublicKeyCredentialOption(request, null)))
43-
try {
44-
return mCredMan.getCredential(activity, getRequest)
45-
} catch (e: GetCredentialException) {
46-
// For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
47-
Log.i(TAG, "Error retrieving credential: ${e.message}")
48-
throw e
51+
/**
52+
* Encapsulates the get passkey API for credential manager in a less error-prone manner.
53+
*
54+
* @param request a get public key credential request JSON required by [GetCredentialRequest].
55+
* @return [GetCredentialResponse] containing the result of the credential retrieval.
56+
*/
57+
suspend fun getPasskey(request: String): GetCredentialResponse {
58+
val getRequest = GetCredentialRequest(listOf(GetPublicKeyCredentialOption(request, null)))
59+
try {
60+
return mCredMan.getCredential(activity, getRequest)
61+
} catch (e: GetCredentialException) {
62+
// For error handling use guidance from https://developer.android.com/training/sign-in/passkeys
63+
Log.i(TAG, "Error retrieving credential: ${e.message}")
64+
throw e
65+
}
4966
}
50-
}
5167
}

identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/CredentialProviderDummyActivity.kt

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
* https://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+
117
package com.example.identity.credentialmanager
218

319
import android.annotation.SuppressLint
@@ -46,7 +62,7 @@ import java.security.spec.ECParameterSpec
4662
import java.security.spec.ECPoint
4763
import java.security.spec.EllipticCurve
4864

49-
class CredentialProviderDummyActivity: FragmentActivity() {
65+
class CredentialProviderDummyActivity : FragmentActivity() {
5066

5167
private val PERSONAL_ACCOUNT_ID: String = ""
5268
private val FAMILY_ACCOUNT_ID: String = ""
@@ -85,7 +101,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
85101

86102
val biometricPrompt = BiometricPrompt(
87103
this,
88-
{ }, // Pass in your own executor
104+
{ }, // Pass in your own executor
89105
object : AuthenticationCallback() {
90106
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
91107
super.onAuthenticationError(errorCode, errString)
@@ -109,7 +125,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
109125

110126
// Generate a credential key pair
111127
val spec = ECGenParameterSpec("secp256r1")
112-
val keyPairGen = KeyPairGenerator.getInstance("EC");
128+
val keyPairGen = KeyPairGenerator.getInstance("EC")
113129
keyPairGen.initialize(spec)
114130
val keyPair = keyPairGen.genKeyPair()
115131

@@ -165,7 +181,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
165181
@RequiresApi(VERSION_CODES.P)
166182
fun appInfoToOrigin(info: CallingAppInfo): String {
167183
val cert = info.signingInfo.apkContentsSigners[0].toByteArray()
168-
val md = MessageDigest.getInstance("SHA-256");
184+
val md = MessageDigest.getInstance("SHA-256")
169185
val certHash = md.digest(cert)
170186
// This is the format for origin
171187
return "android:apk-key-hash:${b64Encode(certHash)}"
@@ -240,7 +256,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
240256
)
241257
)
242258

243-
//Set the final response back
259+
// Set the final response back
244260
val result = Intent()
245261
val response = CreatePasswordResponse()
246262
PendingIntentHandler.setCreateCredentialResponse(result, response)
@@ -300,10 +316,11 @@ class CredentialProviderDummyActivity: FragmentActivity() {
300316

301317
val biometricPrompt = BiometricPrompt(
302318
this,
303-
{ }, // Pass in your own executor
319+
{ }, // Pass in your own executor
304320
object : BiometricPrompt.AuthenticationCallback() {
305321
override fun onAuthenticationError(
306-
errorCode: Int, errString: CharSequence
322+
errorCode: Int,
323+
errString: CharSequence
307324
) {
308325
super.onAuthenticationError(errorCode, errString)
309326
finish()
@@ -330,7 +347,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
330347
packageName = packageName
331348
)
332349

333-
val sig = Signature.getInstance("SHA256withECDSA");
350+
val sig = Signature.getInstance("SHA256withECDSA")
334351
sig.initSign(privateKey)
335352
sig.update(response.dataToSign())
336353
response.signature = sig.sign()
@@ -401,9 +418,7 @@ class CredentialProviderDummyActivity: FragmentActivity() {
401418
}
402419

403420
// [START android_identity_credential_pending_intent]
404-
fun createSettingsPendingIntent(): PendingIntent
405-
// [END android_identity_credential_pending_intent]
406-
{
421+
fun createSettingsPendingIntent(): PendingIntent { // [END android_identity_credential_pending_intent]
407422
return PendingIntent.getBroadcast(this, 0, Intent(), PendingIntent.FLAG_IMMUTABLE)
408423
}
409424

@@ -468,7 +483,7 @@ data class CredentialsInfo(
468483
val passwords: List<PasswordInfo> = listOf()
469484
)
470485

471-
class ECPrivateKeyImpl: ECPrivateKey {
486+
class ECPrivateKeyImpl : ECPrivateKey {
472487
override fun getAlgorithm(): String = ""
473488
override fun getFormat(): String = ""
474489
override fun getEncoded(): ByteArray = byteArrayOf()

identity/credentialmanager/src/main/java/com/example/identity/credentialmanager/Fido2ToCredmanMigration.kt

Lines changed: 71 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2025 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+
* https://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+
117
package com.example.identity.credentialmanager
218

319
import android.app.Activity
@@ -16,6 +32,7 @@ import androidx.credentials.GetPublicKeyCredentialOption
1632
import androidx.credentials.PublicKeyCredential
1733
import androidx.credentials.exceptions.CreateCredentialException
1834
import com.example.identity.credentialmanager.ApiResult.Success
35+
import java.io.StringWriter
1936
import okhttp3.MediaType.Companion.toMediaTypeOrNull
2037
import okhttp3.OkHttpClient
2138
import okhttp3.Request.Builder
@@ -24,7 +41,6 @@ import okhttp3.RequestBody.Companion.toRequestBody
2441
import okhttp3.Response
2542
import okhttp3.ResponseBody
2643
import org.json.JSONObject
27-
import java.io.StringWriter
2844
import ru.gildor.coroutines.okhttp.await
2945

3046
class Fido2ToCredmanMigration(
@@ -44,12 +60,15 @@ class Fido2ToCredmanMigration(
4460
// ...
4561
val call = client.newCall(
4662
Builder()
47-
.method("POST", jsonRequestBody {
48-
name("attestation").value("none")
49-
name("authenticatorSelection").objectValue {
50-
name("residentKey").value("required")
63+
.method(
64+
"POST",
65+
jsonRequestBody {
66+
name("attestation").value("none")
67+
name("authenticatorSelection").objectValue {
68+
name("residentKey").value("required")
69+
}
5170
}
52-
}).build()
71+
).build()
5372
)
5473
// ...
5574
}
@@ -61,14 +80,17 @@ class Fido2ToCredmanMigration(
6180
Builder()
6281
.url("$BASE_URL/<your api url>")
6382
.addHeader("Cookie", formatCookie(sessionId))
64-
.method("POST", jsonRequestBody {
65-
name("attestation").value("none")
66-
name("authenticatorSelection").objectValue {
67-
name("authenticatorAttachment").value("platform")
68-
name("userVerification").value("required")
69-
name("residentKey").value("required")
83+
.method(
84+
"POST",
85+
jsonRequestBody {
86+
name("attestation").value("none")
87+
name("authenticatorSelection").objectValue {
88+
name("authenticatorAttachment").value("platform")
89+
name("userVerification").value("required")
90+
name("residentKey").value("required")
91+
}
7092
}
71-
}).build()
93+
).build()
7294
)
7395
val response = call.await()
7496
return response.result("Error calling the api") {
@@ -108,10 +130,13 @@ class Fido2ToCredmanMigration(
108130
* @return a JSON object.
109131
*/
110132
suspend fun signinRequest(): ApiResult<JSONObject> {
111-
val call = client.newCall(Builder().url(buildString {
112-
append("$BASE_URL/signinRequest")
113-
}).method("POST", jsonRequestBody {})
114-
.build()
133+
val call = client.newCall(
134+
Builder().url(
135+
buildString {
136+
append("$BASE_URL/signinRequest")
137+
}
138+
).method("POST", jsonRequestBody {})
139+
.build()
115140
)
116141
val response = call.await()
117142
return response.result("Error calling /signinRequest") {
@@ -129,31 +154,36 @@ class Fido2ToCredmanMigration(
129154
* including the newly-registered one.
130155
*/
131156
suspend fun signinResponse(
132-
sessionId: String, response: JSONObject, credentialId: String
157+
sessionId: String,
158+
response: JSONObject,
159+
credentialId: String
133160
): ApiResult<Unit> {
134161

135162
val call = client.newCall(
136163
Builder().url("$BASE_URL/signinResponse")
137-
.addHeader("Cookie",formatCookie(sessionId))
138-
.method("POST", jsonRequestBody {
139-
name("id").value(credentialId)
140-
name("type").value(PUBLIC_KEY.toString())
141-
name("rawId").value(credentialId)
142-
name("response").objectValue {
143-
name("clientDataJSON").value(
144-
response.getString("clientDataJSON")
145-
)
146-
name("authenticatorData").value(
147-
response.getString("authenticatorData")
148-
)
149-
name("signature").value(
150-
response.getString("signature")
151-
)
152-
name("userHandle").value(
153-
response.getString("userHandle")
154-
)
164+
.addHeader("Cookie", formatCookie(sessionId))
165+
.method(
166+
"POST",
167+
jsonRequestBody {
168+
name("id").value(credentialId)
169+
name("type").value(PUBLIC_KEY.toString())
170+
name("rawId").value(credentialId)
171+
name("response").objectValue {
172+
name("clientDataJSON").value(
173+
response.getString("clientDataJSON")
174+
)
175+
name("authenticatorData").value(
176+
response.getString("authenticatorData")
177+
)
178+
name("signature").value(
179+
response.getString("signature")
180+
)
181+
name("userHandle").value(
182+
response.getString("userHandle")
183+
)
184+
}
155185
}
156-
}).build()
186+
).build()
157187
)
158188
val apiResponse = call.await()
159189
return apiResponse.result("Error calling /signingResponse") {
@@ -169,11 +199,12 @@ class Fido2ToCredmanMigration(
169199
Toast.makeText(
170200
activity,
171201
"Fetching previously stored credentials",
172-
Toast.LENGTH_SHORT)
202+
Toast.LENGTH_SHORT
203+
)
173204
.show()
174205
var result: GetCredentialResponse? = null
175206
try {
176-
val request= GetCredentialRequest(
207+
val request = GetCredentialRequest(
177208
listOf(
178209
GetPublicKeyCredentialOption(
179210
creationResult.toString(),
@@ -234,7 +265,7 @@ class Fido2ToCredmanMigration(
234265
}
235266

236267
sealed class ApiResult<out R> {
237-
class Success<T>: ApiResult<T>()
268+
class Success<T> : ApiResult<T>()
238269
}
239270

240271
class ApiException(message: String) : RuntimeException(message)

0 commit comments

Comments
 (0)