@@ -17,6 +17,7 @@ package com.amplifyframework.auth.cognito.helpers
17
17
18
18
import android.app.Activity
19
19
import android.content.Context
20
+ import android.os.Build
20
21
import androidx.credentials.CreateCredentialResponse
21
22
import androidx.credentials.CreatePublicKeyCredentialRequest
22
23
import androidx.credentials.CreatePublicKeyCredentialResponse
@@ -75,22 +76,26 @@ internal class WebAuthnHelper(
75
76
}
76
77
77
78
suspend fun createCredential (requestJson : String , callingActivity : Activity ): String {
78
- try {
79
- // Create the request for CredentialManager
80
- val request = CreatePublicKeyCredentialRequest (requestJson)
81
-
82
- // Create the credential
83
- logger.verbose(" Prompting user to create a PassKey" )
84
- val result: CreateCredentialResponse = credentialManager.createCredential(callingActivity, request)
85
-
86
- // Extract the Public Key registration response. This is what we send to Cognito.
87
- val publicKeyResult = result as ? CreatePublicKeyCredentialResponse ? : throw WebAuthnFailedException (
88
- " Android created wrong credential type" ,
89
- AmplifyException .REPORT_BUG_TO_AWS_SUGGESTION
90
- )
91
- return publicKeyResult.registrationResponseJson
92
- } catch (e: CreateCredentialException ) {
93
- throw e.toAuthException()
79
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
80
+ try {
81
+ // Create the request for CredentialManager
82
+ val request = CreatePublicKeyCredentialRequest (requestJson)
83
+
84
+ // Create the credential
85
+ logger.verbose(" Prompting user to create a PassKey" )
86
+ val result: CreateCredentialResponse = credentialManager.createCredential(callingActivity, request)
87
+
88
+ // Extract the Public Key registration response. This is what we send to Cognito.
89
+ val publicKeyResult = result as ? CreatePublicKeyCredentialResponse ? : throw WebAuthnFailedException (
90
+ " Android created wrong credential type" ,
91
+ AmplifyException .REPORT_BUG_TO_AWS_SUGGESTION
92
+ )
93
+ return publicKeyResult.registrationResponseJson
94
+ } catch (e: CreateCredentialException ) {
95
+ throw e.toAuthException()
96
+ }
97
+ } else {
98
+ throw WebAuthnNotSupportedException (" Passkeys are only supported on API 28 and above" )
94
99
}
95
100
}
96
101
0 commit comments