-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[34253] - Add AuthorizationControllerWrapper & Unit Test for TwilioPasskey iOS. #21
Open
AlejandroOrozco
wants to merge
2
commits into
dev
Choose a base branch
from
aorozcobuiles/ACCSEC-34253/PasskeysWrappersForUnitTests
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
shared/src/iosMain/kotlin/com/twilio/passkeys/AuthorizationControllerWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package com.twilio.passkeys | ||
|
||
import com.twilio.passkeys.exception.TwilioException | ||
import com.twilio.passkeys.exception.UNKNOWN_ERROR | ||
import com.twilio.passkeys.models.AuthenticatePasskeyResponse | ||
import com.twilio.passkeys.models.CreatePasskeyResponse | ||
import com.twilio.passkeys.utils.DeviceUtils | ||
import platform.AuthenticationServices.ASAuthorization | ||
import platform.AuthenticationServices.ASAuthorizationController | ||
import platform.AuthenticationServices.ASAuthorizationControllerDelegateProtocol | ||
import platform.AuthenticationServices.ASAuthorizationPlatformPublicKeyCredentialAssertion | ||
import platform.AuthenticationServices.ASAuthorizationPlatformPublicKeyCredentialRegistration | ||
import platform.AuthenticationServices.ASAuthorizationPublicKeyCredentialAttachment | ||
import platform.Foundation.NSError | ||
import platform.darwin.NSObject | ||
|
||
interface IAuthorizationControllerWrapper { | ||
fun createPasskey( | ||
authController: ASAuthorizationController, | ||
completion: (CreatePasskeyResult) -> Unit, | ||
) | ||
|
||
fun authenticatePasskey( | ||
authController: ASAuthorizationController, | ||
completion: (AuthenticatePasskeyResult) -> Unit, | ||
) | ||
} | ||
|
||
/** | ||
* Wraps the ASAuthorizationController functionality in order to improve testability & maintainability. | ||
* | ||
* @property deviceUtils The utility class for device-related operations. | ||
*/ | ||
class AuthorizationControllerWrapper : IAuthorizationControllerWrapper { | ||
private lateinit var authController: ASAuthorizationController | ||
private var createPasskeyCompletion: ((CreatePasskeyResult) -> Unit)? = null | ||
private var authenticatePasskeyCompletion: ((AuthenticatePasskeyResult) -> Unit)? = null | ||
private val deviceUtils: DeviceUtils = DeviceUtils() | ||
|
||
override fun createPasskey( | ||
authController: ASAuthorizationController, | ||
completion: (CreatePasskeyResult) -> Unit, | ||
) { | ||
this.createPasskeyCompletion = completion | ||
this.authController = authController | ||
this.authController.delegate = createPasskeyDelegate | ||
this.authController.performRequests() | ||
} | ||
|
||
override fun authenticatePasskey( | ||
authController: ASAuthorizationController, | ||
completion: (AuthenticatePasskeyResult) -> Unit, | ||
) { | ||
this.authenticatePasskeyCompletion = completion | ||
this.authController = authController | ||
this.authController.delegate = authenticatePasskeyDelegate | ||
this.authController.performRequests() | ||
} | ||
|
||
private val createPasskeyDelegate = | ||
object : NSObject(), ASAuthorizationControllerDelegateProtocol { | ||
override fun authorizationController( | ||
controller: ASAuthorizationController, | ||
didCompleteWithAuthorization: ASAuthorization, | ||
) { | ||
val credentialRegistration = didCompleteWithAuthorization.credential as ASAuthorizationPlatformPublicKeyCredentialRegistration | ||
|
||
val createPasskeyResponse: CreatePasskeyResponse = | ||
credentialRegistration.rawAttestationObject?.toUrlSafeString() | ||
?.let { attestationObject -> | ||
CreatePasskeyResponse( | ||
id = credentialRegistration.credentialID.toUrlSafeString(), | ||
rawId = credentialRegistration.credentialID.toUrlSafeString(), | ||
authenticatorAttachment = | ||
if (deviceUtils.isOSVersionSupported(ATTACHMENT_SUPPORT_MIN_OS_VERSION)) { | ||
getAuthenticatorAttachment(credentialRegistration.attachment) | ||
} else { | ||
getAuthenticatorAttachment(ASAuthorizationPublicKeyCredentialAttachment.ASAuthorizationPublicKeyCredentialAttachmentPlatform) | ||
}, | ||
type = PASSKEY_TYPE, | ||
attestationObject = attestationObject, | ||
clientDataJSON = credentialRegistration.rawClientDataJSON.toUrlSafeString(), | ||
transports = listOf("internal"), | ||
) | ||
} ?: kotlin.run { | ||
createPasskeyCompletion?.invoke( | ||
CreatePasskeyResult.Error( | ||
TwilioException("Null attestation object", MISSING_ATTESTATION_OBJECT_ERROR), | ||
), | ||
) | ||
return | ||
} | ||
|
||
createPasskeyCompletion?.invoke( | ||
CreatePasskeyResult.Success( | ||
createPasskeyResponse, | ||
), | ||
) | ||
} | ||
|
||
override fun authorizationController( | ||
controller: ASAuthorizationController, | ||
didCompleteWithError: NSError, | ||
) { | ||
val exception = mapToTwilioException(didCompleteWithError) | ||
createPasskeyCompletion?.invoke(CreatePasskeyResult.Error(exception)) | ||
} | ||
} | ||
|
||
private val authenticatePasskeyDelegate = | ||
object : NSObject(), ASAuthorizationControllerDelegateProtocol { | ||
override fun authorizationController( | ||
controller: ASAuthorizationController, | ||
didCompleteWithAuthorization: ASAuthorization, | ||
) { | ||
val credentialAuthentication = didCompleteWithAuthorization.credential as ASAuthorizationPlatformPublicKeyCredentialAssertion | ||
|
||
val authenticatePasskeyResponse = | ||
AuthenticatePasskeyResponse( | ||
id = credentialAuthentication.credentialID.toUrlSafeString(), | ||
rawId = credentialAuthentication.credentialID.toUrlSafeString(), | ||
authenticatorAttachment = | ||
if (deviceUtils.isOSVersionSupported(ATTACHMENT_SUPPORT_MIN_OS_VERSION)) { | ||
getAuthenticatorAttachment(credentialAuthentication.attachment) | ||
} else { | ||
getAuthenticatorAttachment(ASAuthorizationPublicKeyCredentialAttachment.ASAuthorizationPublicKeyCredentialAttachmentPlatform) | ||
}, | ||
type = PASSKEY_TYPE, | ||
clientDataJSON = credentialAuthentication.rawClientDataJSON.toUrlSafeString(), | ||
authenticatorData = credentialAuthentication.rawAuthenticatorData?.toUrlSafeString(), | ||
signature = credentialAuthentication.signature?.toUrlSafeString(), | ||
userHandle = credentialAuthentication.userID?.toUrlSafeString(), | ||
) | ||
|
||
authenticatePasskeyCompletion?.invoke( | ||
AuthenticatePasskeyResult.Success( | ||
authenticatePasskeyResponse, | ||
), | ||
) | ||
} | ||
|
||
override fun authorizationController( | ||
controller: ASAuthorizationController, | ||
didCompleteWithError: NSError, | ||
) { | ||
authenticatePasskeyCompletion?.invoke( | ||
AuthenticatePasskeyResult.Error( | ||
mapToTwilioException(didCompleteWithError), | ||
), | ||
) | ||
} | ||
} | ||
|
||
private fun getAuthenticatorAttachment(attachment: ASAuthorizationPublicKeyCredentialAttachment): String { | ||
return when (attachment) { | ||
ASAuthorizationPublicKeyCredentialAttachment.ASAuthorizationPublicKeyCredentialAttachmentCrossPlatform -> Attachment.CROSS_PLATFORM.value | ||
else -> Attachment.PLATFORM.value | ||
} | ||
} | ||
|
||
private fun mapToTwilioException(error: NSError): TwilioException { | ||
val type = | ||
when (error.code) { | ||
PASSKEY_CANCELED_ERROR_CODE -> USER_CANCELED_ERROR | ||
PASSKEY_INVALID_RESPONSE_ERROR_CODE -> INVALID_RESPONSE_ERROR | ||
PASSKEY_NOT_HANDLED_ERROR_CODE -> NOT_HANDLED_ERROR | ||
PASSKEY_FAILED_ERROR_CODE -> FAILED_ERROR | ||
PASSKEY_NOT_INTERACTIVE_ERROR_CODE -> NOT_INTERACTIVE_ERROR | ||
else -> UNKNOWN_ERROR | ||
} | ||
return TwilioException(type, error.localizedDescription) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to pass the
credentialRegistration
to TwilioPasskey and applying the logic below inTwilioPasskey
?AuthorizationControllerWrapper
could be in charge of completing the interaction / needed code for iOS authorization, but some logic can be kept inTwilioPasskey
, so it can be testedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No unfortunately that
ASAuthorizationPlatformPublicKeyCredentialRegistration
contains a couple of Objects that can't be initialized outside of Apple private logic. So It won't be possible to create that object from Scratch to use it as a mock.