Skip to content

Commit

Permalink
Fix some mappings in index.ts, some obj-c updates
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattMufson committed Aug 26, 2024
1 parent 517231f commit 42b7372
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ class Passkey {
}
(options.publicKey as PublicKeyCredentialRequestOptions).rpId = this.domain;

options.publicKey.challenge = arrayBufferToBase64(
options.publicKey.challenge as ArrayBuffer,
);

(options.publicKey as PublicKeyCredentialRequestOptions).allowCredentials =
(
options.publicKey as PublicKeyCredentialRequestOptions
).allowCredentials?.filter((cred) => {
return (
cred && cred.id && typeof cred.id === 'string' && cred.id.length > 0
);
});
return this.handler.HandlePasskeyGet(JSON.stringify(options));
}

Expand Down
25 changes: 15 additions & 10 deletions src/lib/passkey.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

typedef void (^PasskeyCompletionHandler)(NSString *resultMessage, NSString *errorMessage);

NSData* ConvertBufferToNSData(Napi::Buffer<uint8_t> buffer) {
return [NSData dataWithBytes:buffer.Data() length:buffer.Length()];
}

@interface PasskeyHandlerObjC : NSObject <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>

@property (nonatomic, strong) PasskeyCompletionHandler completionHandler;
Expand All @@ -28,7 +24,7 @@ - (instancetype)init {
- (void)PerformCreateRequest:(NSDictionary *)options withCompletionHandler:(PasskeyCompletionHandler)completionHandler {
self.completionHandler = completionHandler;

if (@available(macOS 12.0, *)) {
if (@available(macOS 13.5, *)) {
NSDictionary *publicKeyOptions = options[@"publicKey"];
NSString *rpId = publicKeyOptions[@"rp"][@"id"];
NSString *userName = publicKeyOptions[@"user"][@"name"];
Expand Down Expand Up @@ -63,6 +59,10 @@ - (void)PerformCreateRequest:(NSDictionary *)options withCompletionHandler:(Pass
request.userVerificationPreference = ASAuthorizationPublicKeyCredentialUserVerificationPreferenceDiscouraged;
}
}
// NSString *attestationPreference = publicKeyOptions[@"attestation"];
// if (attestationPreference) {
// request.attestationPreference = attestationPreference;
// }

ASAuthorizationController *controller =
[[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
Expand All @@ -84,10 +84,11 @@ - (void)PerformCreateRequest:(NSDictionary *)options withCompletionHandler:(Pass
- (void)PerformGetRequest:(NSDictionary *)options withCompletionHandler:(PasskeyCompletionHandler)completionHandler {
self.completionHandler = completionHandler;

if (@available(macOS 12.0, *)) {
if (@available(macOS 13.5, *)) {
NSDictionary *publicKeyOptions = options[@"publicKey"];
NSString *rpId = publicKeyOptions[@"rpId"];
NSData *challenge = publicKeyOptions[@"challenge"];
NSString *challengeString = publicKeyOptions[@"challenge"];
NSData *challenge = [[NSData alloc] initWithBase64EncodedString:challengeString options:0];

ASAuthorizationPlatformPublicKeyCredentialProvider *provider =
[[ASAuthorizationPlatformPublicKeyCredentialProvider alloc] initWithRelyingPartyIdentifier:rpId];
Expand Down Expand Up @@ -143,7 +144,8 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
NSData *clientDataJSON = credential.rawClientDataJSON;
NSData *attestationObject = credential.rawAttestationObject;
NSString *credentialId = [credential.credentialID base64EncodedStringWithOptions:0];

ASAuthorizationPublicKeyCredentialAttachment attachment = credential.attachment;

NSDictionary *responseDict = @{
@"clientDataJSON": [clientDataJSON base64EncodedStringWithOptions:0],
@"attestationObject": [attestationObject base64EncodedStringWithOptions:0]
Expand All @@ -156,7 +158,8 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
@"rawId": credentialId, // rawId is the raw NSData representing the credential ID
@"response": responseDict, // The response object
@"clientExtensionResults": @{}, // An empty dictionary, as no extensions are used in this example
@"transports": @[] // Transports are not directly available in ASAuthorizationPlatformPublicKeyCredentialRegistration
@"transports": @[], // Transports are not directly available in ASAuthorizationPlatformPublicKeyCredentialRegistration
@"authenticatorAttachment": attachment == ASAuthorizationPublicKeyCredentialAttachmentPlatform ? @'platform' : @'cross-platform',
};

if (![NSJSONSerialization isValidJSONObject:publicKeyCredentialDict]) {
Expand Down Expand Up @@ -185,6 +188,7 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
ASAuthorizationPlatformPublicKeyCredentialAssertion *credential = (ASAuthorizationPlatformPublicKeyCredentialAssertion *)authorization.credential;

NSString *credentialId = [credential.credentialID base64EncodedStringWithOptions:0];
ASAuthorizationPublicKeyCredentialAttachment attachment = credential.attachment;

// Create the "response" dictionary, simulating the AuthenticatorAssertionResponse
NSDictionary *responseDict = @{
Expand All @@ -201,7 +205,8 @@ - (void)authorizationController:(ASAuthorizationController *)controller didCompl
@"rawId": credentialId, // rawId is the base64-encoded credential ID
@"response": responseDict, // The response object
@"clientExtensionResults": @{}, // An empty dictionary, as no extensions are used in this example
@"transports": @[] // Transports are not directly available in ASAuthorizationPlatformPublicKeyCredentialAssertion
@"transports": @[], // Transports are not directly available in ASAuthorizationPlatformPublicKeyCredentialAssertion
@"authenticatorAttachment": attachment == ASAuthorizationPublicKeyCredentialAttachmentPlatform ? @'platform' : @'cross-platform',
};

if (![NSJSONSerialization isValidJSONObject:publicKeyCredentialDict]) {
Expand Down

0 comments on commit 42b7372

Please sign in to comment.