Skip to content

feat(auth): TOTP macOS support #17513

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ @interface FLTFirebaseAuthPlugin ()
@end

@implementation FLTFirebaseAuthPlugin {
#if TARGET_OS_IPHONE
// Map an id to a MultiFactorSession object.
NSMutableDictionary<NSString *, FIRMultiFactorSession *> *_multiFactorSessionMap;

Expand All @@ -114,8 +113,6 @@ @implementation FLTFirebaseAuthPlugin {
// Map an id to a MultiFactorResolver object.
NSMutableDictionary<NSString *, FIRTOTPSecret *> *_multiFactorTotpSecretMap;

#endif

NSObject<FlutterBinaryMessenger> *_binaryMessenger;
NSMutableDictionary<NSString *, FlutterEventChannel *> *_eventChannels;
NSMutableDictionary<NSString *, NSObject<FlutterStreamHandler> *> *_streamHandlers;
Expand All @@ -133,13 +130,10 @@ - (instancetype)init:(NSObject<FlutterBinaryMessenger> *)messenger {
_eventChannels = [NSMutableDictionary dictionary];
_streamHandlers = [NSMutableDictionary dictionary];

#if TARGET_OS_IPHONE
_multiFactorSessionMap = [NSMutableDictionary dictionary];
_multiFactorResolverMap = [NSMutableDictionary dictionary];
_multiFactorAssertionMap = [NSMutableDictionary dictionary];
_multiFactorTotpSecretMap = [NSMutableDictionary dictionary];

#endif
}
return self;
}
Expand All @@ -156,13 +150,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
[registrar addApplicationDelegate:instance];
SetUpFirebaseAuthHostApi(registrar.messenger, instance);
SetUpFirebaseAuthUserHostApi(registrar.messenger, instance);

#if TARGET_OS_IPHONE
SetUpMultiFactorUserHostApi(registrar.messenger, instance);
SetUpMultiFactoResolverHostApi(registrar.messenger, instance);
SetUpMultiFactorTotpHostApi(registrar.messenger, instance);
SetUpMultiFactorTotpSecretHostApi(registrar.messenger, instance);
#endif
}

+ (FlutterError *)convertToFlutterError:(NSError *)error {
Expand Down Expand Up @@ -509,11 +500,6 @@ - (void)handleMultiFactorError:(AuthPigeonFirebaseApp *)app
completion:(nonnull void (^)(PigeonUserCredential *_Nullable,
FlutterError *_Nullable))completion
withError:(NSError *_Nullable)error {
#if TARGET_OS_OSX
completion(nil, [FlutterError errorWithCode:@"second-factor-required"
message:error.description
details:nil]);
#else
FIRMultiFactorResolver *resolver =
(FIRMultiFactorResolver *)error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];

Expand Down Expand Up @@ -554,7 +540,6 @@ - (void)handleMultiFactorError:(AuthPigeonFirebaseApp *)app
completion(nil, [FlutterError errorWithCode:@"second-factor-required"
message:error.description
details:output]);
#endif
}

static void launchAppleSignInRequest(FLTFirebaseAuthPlugin *object, AuthPigeonFirebaseApp *app,
Expand Down Expand Up @@ -835,13 +820,11 @@ - (void)ensureAPNSTokenSetting {
#endif
}

#if !TARGET_OS_OSX
- (FIRMultiFactor *)getAppMultiFactorFromPigeon:(nonnull AuthPigeonFirebaseApp *)app {
FIRAuth *auth = [self getFIRAuthFromAppNameFromPigeon:app];
FIRUser *currentUser = auth.currentUser;
return currentUser.multiFactor;
}
#endif

- (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:
(nonnull ASAuthorizationController *)controller API_AVAILABLE(macos(10.15), ios(13.0)) {
Expand All @@ -852,12 +835,16 @@ - (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:
#endif
}

#if TARGET_OS_IPHONE

- (void)enrollPhoneApp:(nonnull AuthPigeonFirebaseApp *)app
assertion:(nonnull PigeonPhoneMultiFactorAssertion *)assertion
displayName:(nullable NSString *)displayName
completion:(nonnull void (^)(FlutterError *_Nullable))completion {
#if TARGET_OS_OSX
completion([FlutterError errorWithCode:@"unsupported-platform"
message:@"Phone authentication is not supported on macOS"
details:nil]);
#else

FIRMultiFactor *multiFactor = [self getAppMultiFactorFromPigeon:app];

FIRPhoneAuthCredential *credential =
Expand All @@ -879,6 +866,7 @@ - (void)enrollPhoneApp:(nonnull AuthPigeonFirebaseApp *)app
details:nil]);
}
}];
#endif
}

- (void)getEnrolledFactorsApp:(nonnull AuthPigeonFirebaseApp *)app
Expand Down Expand Up @@ -969,10 +957,12 @@ - (void)resolveSignInResolverId:(nonnull NSString *)resolverId
FIRMultiFactorAssertion *multiFactorAssertion;

if (assertion != nil) {
#if TARGET_OS_IPHONE
FIRPhoneAuthCredential *credential =
[[FIRPhoneAuthProvider provider] credentialWithVerificationID:[assertion verificationId]
verificationCode:[assertion verificationCode]];
multiFactorAssertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
#endif
} else if (totpAssertionId != nil) {
multiFactorAssertion = _multiFactorAssertionMap[totpAssertionId];
} else {
Expand Down Expand Up @@ -1065,8 +1055,6 @@ - (void)openInOtpAppSecretKey:(nonnull NSString *)secretKey
completion(nil);
}

#endif

- (void)applyActionCodeApp:(nonnull AuthPigeonFirebaseApp *)app
code:(nonnull NSString *)code
completion:(nonnull void (^)(FlutterError *_Nullable))completion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,13 @@ + (PigeonAdditionalUserInfo *)getPigeonAdditionalUserInfo:(nonnull FIRAdditional
profile:userInfo.profile];
}

#if TARGET_OS_IPHONE
+ (PigeonTotpSecret *)getPigeonTotpSecret:(FIRTOTPSecret *)secret {
return [PigeonTotpSecret makeWithCodeIntervalSeconds:nil
codeLength:nil
enrollmentCompletionDeadline:nil
hashingAlgorithm:nil
secretKey:secret.sharedSecretKey];
}
#endif

+ (PigeonAuthCredential *)getPigeonAuthCredential:(FIRAuthCredential *)authCredential
token:(NSNumber *_Nullable)token {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
(nullable PigeonActionCodeSettings *)settings;
+ (PigeonUserCredential *_Nullable)getPigeonUserCredentialFromFIRUser:(nonnull FIRUser *)user;
+ (PigeonIdTokenResult *_Nonnull)parseIdTokenResult:(nonnull FIRAuthTokenResult *)tokenResult;
#if TARGET_OS_IPHONE
+ (PigeonTotpSecret *_Nonnull)getPigeonTotpSecret:(nonnull FIRTOTPSecret *)secret;
#endif
+ (PigeonAuthCredential *_Nullable)getPigeonAuthCredential:
(FIRAuthCredential *_Nullable)authCredentialToken
token:(NSNumber *_Nullable)token;
Expand Down
6 changes: 2 additions & 4 deletions packages/firebase_auth/firebase_auth/lib/src/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,9 @@ class User {
}

MultiFactor get multiFactor {
if (!kIsWeb &&
(defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.windows)) {
if (!kIsWeb && (defaultTargetPlatform == TargetPlatform.windows)) {
throw UnimplementedError(
'MultiFactor Authentication is only supported on web, Android and iOS.',
'MultiFactor Authentication is only supported on web, Android, iOS and macOS.',
);
}
return _multiFactor ??= MultiFactor._(_delegate.multiFactor);
Expand Down