ใน Android 15 Credential Manager รองรับขั้นตอนการแตะเพียงครั้งเดียวสำหรับการสร้างและการดึงข้อมูลเข้าสู่ระบบ ในขั้นตอนการทำงานนี้ ระบบจะแสดงข้อมูลของข้อมูลเข้าสู่ระบบที่กำลังสร้างหรือกำลังใช้งานในพรอมต์ไบโอเมตริกโดยตรง พร้อมกับจุดเริ่มต้นไปยังตัวเลือกเพิ่มเติม กระบวนการที่ง่ายขึ้นนี้จะช่วยให้กระบวนการสร้างและดึงข้อมูลเข้าสู่ระบบมีประสิทธิภาพและคล่องตัวมากขึ้น
ข้อกำหนด:
- ตั้งค่าไบโอเมตริกในอุปกรณ์ของผู้ใช้แล้ว และผู้ใช้ได้อนุญาตให้ใช้ไบโอเมตริกเพื่อตรวจสอบสิทธิ์เข้าสู่แอปพลิเคชัน
- สำหรับขั้นตอนการลงชื่อเข้าใช้ ฟีเจอร์นี้จะเปิดใช้สำหรับสถานการณ์ที่มีบัญชีเดียวเท่านั้น แม้ว่าบัญชีนั้นจะมีข้อมูลเข้าสู่ระบบหลายรายการ (เช่น พาสคีย์และรหัสผ่าน)
เปิดใช้การแตะเพียงครั้งเดียวในขั้นตอนการสร้างพาสคีย์
ขั้นตอนการสร้างของเมธอดนี้จะตรงกับกระบวนการสร้างข้อมูลเข้าสู่ระบบที่มีอยู่ ใช้ handleCreatePasskeyQuery() ภายใน BeginCreatePublicKeyCredentialRequest เพื่อประมวลผลคำขอหากคำขอนั้นเป็นคำขอสำหรับพาสคีย์
is BeginCreatePublicKeyCredentialRequest -> {
Log.i(TAG, "Request is passkey type")
return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
}
รวม BiometricPromptData กับ
คลาส CreateEntry ใน handleCreatePasskeyQuery() ดังนี้
val createEntry = CreateEntry(
// Additional properties...
biometricPromptData = BiometricPromptData(
allowedAuthenticators = allowedAuthenticator
),
)
ผู้ให้บริการข้อมูลเข้าสู่ระบบควรตั้งค่าพร็อพเพอร์ตี้ allowedAuthenticator ในอินสแตนซ์ BiometricPromptData อย่างชัดเจน หากไม่ได้ตั้งค่าพร็อพเพอร์ตี้นี้ ค่าเริ่มต้นจะเป็น DEVICE_WEAK ตั้งค่าพร็อพเพอร์ตี้ cryptoObject ที่ไม่บังคับหากจำเป็นสำหรับกรณีการใช้งานของคุณ
เปิดใช้การแตะเพียงครั้งเดียวในขั้นตอนการลงชื่อเข้าใช้ด้วยพาสคีย์
การทำงานนี้จะคล้ายกับขั้นตอนการสร้างพาสคีย์ โดยจะทำตามการตั้งค่าที่มีอยู่สำหรับ
การจัดการการลงชื่อเข้าใช้ของผู้ใช้ ใช้ populatePasskeyData() ภายใต้ BeginGetPublicKeyCredentialOption เพื่อรวบรวมข้อมูลที่เกี่ยวข้องเกี่ยวกับคำขอการตรวจสอบสิทธิ์
is BeginGetPublicKeyCredentialOption -> {
// ... other logic
populatePasskeyData(
origin,
option,
responseBuilder,
autoSelectEnabled,
allowedAuthenticator
)
// ... other logic as needed
}
อินสแตนซ์ BiometricPromptData จะตั้งค่าเป็นอินสแตนซ์ PublicKeyCredentialEntry ซึ่งคล้ายกับ CreateEntry 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 operation
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...