Skip to content
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

[Passkey #5] Auth startPasskeySignInWithCompletion: and finalizePasskeySignInWithPlatformCredential:completion #11952

Merged
merged 9 commits into from
Oct 23, 2023
90 changes: 90 additions & 0 deletions FirebaseAuth/Sources/Auth/FIRAuth.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeySignInRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeySignInResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.h"
Expand All @@ -57,6 +59,8 @@
#import "FirebaseAuth/Sources/Backend/RPC/FIRSignInWithGameCenterResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRSignUpNewUserResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeySignInRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeySignInResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionRequest.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyAssertionResponse.h"
#import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyCustomTokenRequest.h"
Expand All @@ -72,6 +76,7 @@
#import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
#import "FirebaseAuth/Sources/Utilities/FIRAuthExceptionUtils.h"
#import "FirebaseAuth/Sources/Utilities/FIRAuthWebUtils.h"
#import "FirebaseAuth/Sources/Utilities/NSData+FIRBase64.h"

#if TARGET_OS_IOS
#import "FirebaseAuth/Sources/AuthProvider/Phone/FIRPhoneAuthCredential_Internal.h"
Expand All @@ -83,6 +88,10 @@
#import "FirebaseAuth/Sources/Utilities/FIRAuthURLPresenter.h"
#endif

#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
#import <AuthenticationServices/AuthenticationServices.h>
#endif

NS_ASSUME_NONNULL_BEGIN

#pragma mark-- Logger Service String.
Expand Down Expand Up @@ -1222,6 +1231,87 @@ - (void)signInWithCustomToken:(NSString *)token
});
}

#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
- (void)startPasskeySignInWithCompletion:
(nullable void (^)(
ASAuthorizationPlatformPublicKeyCredentialAssertionRequest *_Nullable request,
NSError *_Nullable error))completion {
FIRStartPasskeySignInRequest *request =
[[FIRStartPasskeySignInRequest alloc] initWithRequestConfiguration:self.requestConfiguration];
[FIRAuthBackend
startPasskeySignIn:request
callback:^(FIRStartPasskeySignInResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
completion(nil, error);
return;
}
if (response) {
NSData *challengeInData =
[[NSData alloc] initWithBase64EncodedString:response.challenge options:0];
ASAuthorizationPlatformPublicKeyCredentialProvider *provider =
[[ASAuthorizationPlatformPublicKeyCredentialProvider alloc]
initWithRelyingPartyIdentifier:response.rpID];
ASAuthorizationPlatformPublicKeyCredentialAssertionRequest *request =
[provider createCredentialAssertionRequestWithChallenge:challengeInData];

completion(request, nil);
}
}];
}

- (void)finalizePasskeySignInWithPlatformCredential:
(ASAuthorizationPlatformPublicKeyCredentialAssertion *)platformCredential
completion:(nullable void (^)(
FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error))completion {
dispatch_async(FIRAuthGlobalWorkQueue(), ^{
FIRAuthDataResultCallback decoratedCallback =
[self signInFlowAuthDataResultCallbackByDecoratingCallback:completion];
NSString *credentialID = [platformCredential.credentialID base64EncodedStringWithOptions:0];
NSString *clientDataJson =
[platformCredential.rawClientDataJSON base64EncodedStringWithOptions:0];
NSString *authenticatorData =
[platformCredential.rawAuthenticatorData base64EncodedStringWithOptions:0];
NSString *signature = [platformCredential.signature base64EncodedStringWithOptions:0];
NSString *userID = [platformCredential.userID base64EncodedStringWithOptions:0];
FIRFinalizePasskeySignInRequest *request =
[[FIRFinalizePasskeySignInRequest alloc] initWithCredentialID:credentialID
clientDataJson:clientDataJson
authenticatorData:authenticatorData
signature:signature
userID:userID
requestConfiguration:self.requestConfiguration];
[FIRAuthBackend
finalizePasskeySignIn:request
callback:^(FIRFinalizePasskeySignInResponse *_Nullable response,
NSError *_Nullable error) {
if (error) {
decoratedCallback(nil, error);
}
[self completeSignInWithAccessToken:response.idToken
accessTokenExpirationDate:nil
refreshToken:response.refreshToken
anonymous:NO
callback:^(FIRUser *_Nullable user,
NSError *_Nullable error) {
if (error) {
completion(nil, error);
return;
}

FIRAuthDataResult *authDataResult =
user ? [[FIRAuthDataResult alloc]
initWithUser:user
additionalUserInfo:nil]
: nil;
decoratedCallback(authDataResult, error);
}];
}];
});
}
#endif // #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST

- (void)createUserWithEmail:(NSString *)email
password:(NSString *)password
completion:(nullable FIRAuthDataResultCallback)completion {
Expand Down
40 changes: 40 additions & 0 deletions FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
@class FIRAuthDataResult;
@class FIRAuthSettings;
@class FIRUser;

#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
@class ASAuthorizationPlatformPublicKeyCredentialAssertion;
@class ASAuthorizationPlatformPublicKeyCredentialAssertionRequest;
#endif

@protocol FIRAuthUIDelegate;
@protocol FIRFederatedAuthProvider;

Expand Down Expand Up @@ -578,6 +584,40 @@ NS_SWIFT_NAME(Auth)
completion:(nullable void (^)(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error))completion;

#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
/**
@fn startPasskeySignInWithCompletion:
@brief start sign in with passkey retrieving challenge from GCIP and create an assertion request.
@param completion Optionally; a block which creates a assertation request.

@remarks //TODO add possible error codes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? Also add your ldap after TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ldap.

The reason for this is to follow the current public API reference format which includes a list of possible error codes. Please see an example here. I will fill this after error code bug (b/302700496) for backend is fixed.


*/
- (void)startPasskeySignInWithCompletion:
(nullable void (^)(
ASAuthorizationPlatformPublicKeyCredentialAssertionRequest *_Nullable request,
NSError *_Nullable error))completion NS_SWIFT_NAME(startPasskeySignIn(completion:))
API_AVAILABLE(macos(12.0), ios(15.0), tvos(16.0));

/**
@fn finalizePasskeySignInWithPlatformCredential:completion:
@brief finalize sign in with passkey with existing credential assertion.
@param platformCredential The existing credential assertion created by device.
@param completion Optionally; a block which is invoked when the sign in with passkey flow finishes,
or is canceled. Invoked asynchronously on the main thread in the future.

@remarks //TODO add possible error codes

*/
- (void)finalizePasskeySignInWithPlatformCredential:
(ASAuthorizationPlatformPublicKeyCredentialAssertion *)platformCredential
completion:(nullable void (^)(
FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error))completion
NS_SWIFT_NAME(finalizePasskeySignIn(with:completion:))
API_AVAILABLE(macos(12.0), ios(15.0), tvos(16.0));
#endif

/** @fn createUserWithEmail:password:completion:
@brief Creates and, on success, signs in a user with the given email address and password.

Expand Down
Loading
Loading