ใน Android 15 Credential Manager จะรองรับขั้นตอนการแตะเพียงครั้งเดียวสำหรับการสร้างและการดึงข้อมูลเข้าสู่ระบบ ในขั้นตอนนี้ ข้อมูลของข้อมูลเข้าสู่ระบบที่สร้างหรือใช้จะแสดงในข้อความแจ้งข้อมูลไบโอเมตริกโดยตรง พร้อมกับจุดแรกเข้าสำหรับตัวเลือกเพิ่มเติม กระบวนการที่เรียบง่ายนี้ช่วยให้การสร้างและการดึงข้อมูลเข้าสู่ระบบมีประสิทธิภาพและราบรื่นยิ่งขึ้น
ข้อกำหนด
- มีการตั้งค่าข้อมูลไบโอเมตริกในอุปกรณ์ของผู้ใช้และผู้ใช้อนุญาตให้ใช้ข้อมูลดังกล่าวสำหรับการตรวจสอบสิทธิ์ในแอปพลิเคชัน
- สำหรับขั้นตอนการลงชื่อเข้าใช้ ฟีเจอร์นี้จะเปิดใช้สำหรับสถานการณ์ที่มีบัญชีเดียวเท่านั้น แม้ว่าจะมีข้อมูลเข้าสู่ระบบหลายรายการ (เช่น พาสคีย์และรหัสผ่าน) สำหรับบัญชีนั้นก็ตาม
เปิดใช้การแตะครั้งเดียวในขั้นตอนการสร้างพาสคีย์
ขั้นตอนการสร้างของวิธีนี้จะตรงกับกระบวนการสร้างข้อมูลเข้าสู่ระบบที่มีอยู่ ภายใน BeginCreatePublicKeyCredentialRequest ให้ใช้ handleCreatePasskeyQuery() เพื่อประมวลผลคำขอหากเป็นคำขอพาสคีย์
is BeginCreatePublicKeyCredentialRequest -> {
Log.i(TAG, "Request is passkey type")
return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
}
ใน handleCreatePasskeyQuery() ให้ใส่ BiometricPromptData พร้อมคลาส CreateEntry ดังนี้
val createEntry = CreateEntry(
// Additional properties...
biometricPromptData = BiometricPromptData(
allowedAuthenticators = allowedAuthenticator
),
)
ผู้ให้บริการข้อมูลเข้าสู่ระบบควรตั้งค่าพร็อพเพอร์ตี้ allowedAuthenticator อย่างชัดเจนในอินสแตนซ์ BiometricPromptData หากไม่ได้ตั้งค่าพร็อพเพอร์ตี้นี้ ค่าเริ่มต้นจะเป็น DEVICE_WEAK ตั้งค่าพร็อพเพอร์ตี้ cryptoObject ที่ไม่บังคับหากจำเป็นสำหรับกรณีการใช้งานของคุณ
เปิดใช้การแตะครั้งเดียวในขั้นตอนการลงชื่อเข้าใช้ด้วยพาสคีย์
ซึ่งจะทำตามการตั้งค่าที่มีอยู่สำหรับการจัดการการลงชื่อเข้าใช้ของผู้ใช้ เช่นเดียวกับขั้นตอนการสร้างพาสคีย์ ในส่วน BeginGetPublicKeyCredentialOption ให้ใช้ populatePasskeyData() เพื่อรวบรวมข้อมูลที่เกี่ยวข้องเกี่ยวกับคำขอตรวจสอบสิทธิ์ ดังนี้
is BeginGetPublicKeyCredentialOption -> {
// ... other logic
populatePasskeyData(
origin,
option,
responseBuilder,
autoSelectEnabled,
allowedAuthenticator
)
// ... other logic as needed
}
เช่นเดียวกับ CreateEntry อินสแตนซ์ BiometricPromptData จะตั้งค่าเป็นอินสแตนซ์ PublicKeyCredentialEntry หากไม่ได้ตั้งไว้อย่างชัดแจ้ง allowedAuthenticator จะมีค่าเริ่มต้นเป็น BIOMETRIC_WEAK
PublicKeyCredentialEntry(
// other properties...
biometricPromptData = BiometricPromptData(
allowedAuthenticators = allowedAuthenticator
)
)
จัดการการเลือกรายการข้อมูลเข้าสู่ระบบ
ขณะจัดการการเลือกรายการข้อมูลเข้าสู่ระบบสำหรับการสร้างพาสคีย์หรือการเลือกพาสคีย์ระหว่างการลงชื่อเข้าใช้ ให้เรียกใช้ PendingIntentHandler's
retrieveProviderCreateCredentialRequest หรือ retrieveProviderGetCredentialRequest ตามความเหมาะสม ซึ่งจะแสดงผลออบเจ็กต์ที่มีข้อมูลเมตาที่จำเป็นสำหรับผู้ให้บริการ ตัวอย่างเช่น เมื่อจัดการการเลือกรายการการสร้างพาสคีย์ ให้อัปเดตโค้ดดังที่แสดงต่อไปนี้
val createRequest = PendingIntentHandler.retrieveProviderCreateCredentialRequest(intent)
if (createRequest == null) {
Log.i(TAG, "request is null")
setUpFailureResponseAndFinish("Unable to extract request from intent")
return
}
// Other logic...
val biometricPromptResult = createRequest.biometricPromptResult
// Add your logic based on what needs to be done
// after getting biometrics
if (createRequest.callingRequest is CreatePublicKeyCredentialRequest) {
val publicKeyRequest: CreatePublicKeyCredentialRequest =
createRequest.callingRequest as CreatePublicKeyCredentialRequest
if (biometricPromptResult == null) {
// Do your own authentication flow, if needed
} else if (biometricPromptResult.isSuccessful) {
createPasskey(
publicKeyRequest.requestJson,
createRequest.callingAppInfo,
publicKeyRequest.clientDataHash,
accountId
)
} else {
val error = biometricPromptResult.authenticationError
// Process the error
}
// Other logic...
}
ตัวอย่างนี้มีข้อมูลเกี่ยวกับความสำเร็จของขั้นตอนการตรวจสอบข้อมูลไบโอเมตริก นอกจากนี้ยังมีข้อมูลอื่นๆ เกี่ยวกับเอกสารรับรองด้วย หากโฟลว์ล้มเหลว ให้ใช้รหัสข้อผิดพลาดในส่วน biometricPromptResult.authenticationError เพื่อตัดสินใจ
รหัสข้อผิดพลาดที่แสดงผลเป็นส่วนหนึ่งของ biometricPromptResult.authenticationError.errorCode เป็นรหัสข้อผิดพลาดเดียวกับที่กำหนดไว้ในไลบรารี androidx.biometric เช่น androidx.biometric.BiometricPrompt.ERROR_NO_SPACE, androidx.biometric.BiometricPrompt.ERROR_UNABLE_TO_PROCESS, androidx.biometric.BiometricPrompt.ERROR_TIMEOUT และอื่นๆ ที่คล้ายกัน authenticationError จะมีข้อความแสดงข้อผิดพลาดที่เชื่อมโยงกับ errorCode ซึ่งแสดงใน UI ได้ด้วย
ในทำนองเดียวกัน ให้ดึงข้อมูลเมตาในระหว่างretrieveProviderGetCredentialRequest
ตรวจสอบว่าขั้นตอนการใช้ข้อมูลไบโอเมตริกเป็น null หรือไม่ หากใช่ ให้กำหนดค่าข้อมูลไบโอเมตริกของคุณเองเพื่อตรวจสอบสิทธิ์ ซึ่งคล้ายกับวิธีใช้เครื่องมือการดำเนินการ get ดังนี้
val getRequest =
PendingIntentHandler.retrieveProviderGetCredentialRequest(intent)
if (getRequest == null) {
Log.i(TAG, "request is null")
setUpFailureResponseAndFinish("Unable to extract request from intent")
return
}
// Other logic...
val biometricPromptResult = getRequest.biometricPromptResult
// Add your logic based on what needs to be done
// after getting biometrics
if (biometricPromptResult == null) {
// Do your own authentication flow, if necessary
} else if (biometricPromptResult.isSuccessful) {
Log.i(TAG, "The response from the biometricPromptResult was ${biometricPromptResult.authenticationResult?.authenticationType}")
validatePasskey(
publicKeyRequest.requestJson,
origin,
packageName,
uid,
passkey.username,
credId,
privateKey
)
} else {
val error = biometricPromptResult.authenticationError
// Process the error
}
// Other logic...