Trên Android 15, Trình quản lý thông tin xác thực hỗ trợ quy trình một lần nhấn để tạo và truy xuất thông tin xác thực. Trong quy trình này, thông tin về thông tin đăng nhập đang được tạo hoặc đang được sử dụng sẽ xuất hiện trực tiếp trong Lời nhắc sinh trắc học, cùng với một điểm truy cập vào các lựa chọn khác. Quy trình đơn giản này giúp quy trình tạo và truy xuất thông tin xác thực hiệu quả và tinh gọn hơn.
Yêu cầu:
- Người dùng đã thiết lập dữ liệu sinh trắc học trên thiết bị của họ và cho phép dùng dữ liệu đó để xác thực vào các ứng dụng.
- Đối với quy trình đăng nhập, tính năng này chỉ được bật cho các trường hợp sử dụng một tài khoản, ngay cả khi có nhiều thông tin đăng nhập (chẳng hạn như khoá truy cập và mật khẩu) cho tài khoản đó.
Bật tính năng nhấn một lần trong quy trình tạo khoá truy cập
Các bước tạo của phương thức này khớp với quy trình tạo thông tin xác thực hiện có. Trong BeginCreatePublicKeyCredentialRequest
, hãy dùng handleCreatePasskeyQuery()
để xử lý yêu cầu nếu đó là yêu cầu về khoá truy cập.
is BeginCreatePublicKeyCredentialRequest -> {
Log.i(TAG, "Request is passkey type")
return handleCreatePasskeyQuery(request, passwordCount, passkeyCount)
}
Trong handleCreatePasskeyQuery()
, hãy thêm BiometricPromptData
bằng lớp CreateEntry
:
val createEntry = CreateEntry(
// Additional properties...
biometricPromptData = BiometricPromptData(
allowedAuthenticators = allowedAuthenticator
),
)
Nhà cung cấp thông tin đăng nhập phải đặt thuộc tính allowedAuthenticator
một cách rõ ràng trong phiên bản BiometricPromptData
. Nếu bạn không đặt thuộc tính này, giá trị mặc định sẽ là DEVICE_WEAK
. Đặt thuộc tính cryptoObject
không bắt buộc nếu cần cho trường hợp sử dụng của bạn.
Bật tính năng nhấn một lần trong quy trình đăng nhập bằng khoá truy cập
Tương tự như quy trình tạo khoá truy cập, quy trình này sẽ tuân theo chế độ thiết lập hiện có để xử lý hoạt động đăng nhập của người dùng. Trong BeginGetPublicKeyCredentialOption
, hãy dùng populatePasskeyData()
để thu thập thông tin liên quan về yêu cầu xác thực:
is BeginGetPublicKeyCredentialOption -> {
// ... other logic
populatePasskeyData(
origin,
option,
responseBuilder,
autoSelectEnabled,
allowedAuthenticator
)
// ... other logic as needed
}
Tương tự như CreateEntry
, một thực thể BiometricPromptData
được đặt thành thực thể PublicKeyCredentialEntry
. Nếu không được đặt rõ ràng, allowedAuthenticator
sẽ mặc định là BIOMETRIC_WEAK
.
PublicKeyCredentialEntry(
// other properties...
biometricPromptData = BiometricPromptData(
allowedAuthenticators = allowedAuthenticator
)
)
Xử lý lựa chọn mục nhập thông tin đăng nhập
Trong khi xử lý lựa chọn mục nhập thông tin xác thực cho việc tạo khoá truy cập hoặc lựa chọn khoá truy cập trong quá trình đăng nhập, hãy gọi PendingIntentHandler's
retrieveProviderCreateCredentialRequest
hoặc retrieveProviderGetCredentialRequest
, tuỳ theo trường hợp. Các đối tượng này trả về các đối tượng chứa siêu dữ liệu cần thiết cho nhà cung cấp. Ví dụ: khi xử lý lựa chọn mục nhập tạo khoá truy cập, hãy cập nhật mã của bạn như sau:
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...
}
Ví dụ này chứa thông tin về việc quy trình sinh trắc học có thành công hay không. Ngoài ra, thông tin này còn chứa các thông tin khác về thông tin đăng nhập. Nếu quy trình không thành công, hãy sử dụng mã lỗi trong biometricPromptResult.authenticationError
để đưa ra quyết định.
Mã lỗi được trả về trong biometricPromptResult.authenticationError.errorCode
cũng là mã lỗi được xác định trong thư viện androidx.biometric, chẳng hạn như androidx.biometric.BiometricPrompt.NO_SPACE, androidx.biometric.BiometricPrompt.UNABLE_TO_PROCESS, androidx.biometric.BiometricPrompt.ERROR_TIMEOUT, v.v. authenticationError
cũng sẽ chứa thông báo lỗi liên kết với errorCode
có thể hiển thị trên giao diện người dùng.
Tương tự, hãy trích xuất siêu dữ liệu trong retrieveProviderGetCredentialRequest
.
Kiểm tra xem quy trình sinh trắc học của bạn có phải là null
hay không. Nếu có, hãy thiết lập dữ liệu sinh trắc học của riêng bạn để xác thực. Điều này tương tự như cách hoạt động của thao tác 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...