-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added passkey authenticator to project
- Loading branch information
1 parent
0dbf126
commit ce0f245
Showing
4 changed files
with
109 additions
and
0 deletions.
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
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,23 @@ | ||
import Foundation | ||
|
||
extension Data { | ||
/// Certain base 64 data values are encoded to be url safe. For the webauthn authentication we will need to decode the url safe data so that we can read it locally. | ||
/// | ||
static func decodeUrlSafeBase64(_ value: String) throws -> Data { | ||
var stringtoDecode: String = value.replacingOccurrences(of: "-", with: "+") | ||
stringtoDecode = stringtoDecode.replacingOccurrences(of: "_", with: "/") | ||
switch stringtoDecode.utf8.count % 4 { | ||
case 2: | ||
stringtoDecode += "==" | ||
case 3: | ||
stringtoDecode += "=" | ||
default: | ||
break | ||
} | ||
guard let data = Data(base64Encoded: stringtoDecode, options: [.ignoreUnknownCharacters]) else { | ||
throw NSError(domain: "decodeUrlSafeBase64", code: 1, | ||
userInfo: [NSLocalizedDescriptionKey: "Can't decode base64 string"]) | ||
} | ||
return data | ||
} | ||
} |
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,77 @@ | ||
import Foundation | ||
import AuthenticationServices | ||
|
||
class PasskeyAuthControllerDelegate: NSObject, ASAuthorizationControllerDelegate { | ||
|
||
var onCompletion: ((Result<PasskeyAuthResponse, Error>) -> Void)? | ||
|
||
public func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: any Error) { | ||
onCompletion?(.failure(error)) | ||
} | ||
|
||
public func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) { | ||
guard let credential = authorization.credential as? ASAuthorizationPlatformPublicKeyCredentialAssertion else { | ||
onCompletion?(.failure(PasskeyError.authFailed)) | ||
return | ||
} | ||
|
||
let response = PasskeyAuthResponse(from: credential) | ||
onCompletion?(.success(response)) | ||
} | ||
} | ||
|
||
class PasskeyAuthenticator: NSObject { | ||
private let passkeyRemote: PasskeyRemote | ||
private let internalAuthControllerDelegate: PasskeyAuthControllerDelegate | ||
|
||
init(passkeyRemote: PasskeyRemote = PasskeyRemote(), authControllerDelegate: PasskeyAuthControllerDelegate = .init()) { | ||
self.passkeyRemote = passkeyRemote | ||
self.internalAuthControllerDelegate = authControllerDelegate | ||
} | ||
|
||
func attemptPasskeyAuth(for email: String, in presentationContext: PresentationContext) async throws -> PasskeyVerifyResponse { | ||
let challenge = try await passkeyRemote.passkeyAuthChallenge(for: email) | ||
|
||
let challengeData = try Data.decodeUrlSafeBase64(challenge.challenge) | ||
let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: challenge.relayingParty) | ||
let request = provider.createCredentialAssertionRequest(challenge: challengeData) | ||
|
||
let controller = ASAuthorizationController(authorizationRequests: [request]) | ||
controller.delegate = internalAuthControllerDelegate | ||
controller.presentationContextProvider = presentationContext | ||
|
||
return try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<PasskeyVerifyResponse, any Error>) in | ||
internalAuthControllerDelegate.onCompletion = { [weak self] result in | ||
guard let self else { | ||
continuation.resume(throwing: PasskeyError.authFailed) | ||
return | ||
} | ||
|
||
switch result { | ||
case .success(let response): | ||
Task { | ||
do { | ||
let verify = try await self.performPasskeyAuthentication(with: response) | ||
continuation.resume(returning: verify) | ||
} catch { | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
|
||
case .failure(let error): | ||
continuation.resume(throwing: error) | ||
} | ||
} | ||
|
||
controller.performRequests() | ||
} | ||
} | ||
|
||
private func performPasskeyAuthentication(with response: PasskeyAuthResponse) async throws -> PasskeyVerifyResponse { | ||
guard let response = try? await passkeyRemote.verifyPasskeyLogin(with: response) else { | ||
throw PasskeyError.authFailed | ||
} | ||
|
||
return response | ||
} | ||
} |
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