Skip to content

Commit 65a1fb8

Browse files
committed
feat(auth): TOTP macOS support
1 parent 2aaf5af commit 65a1fb8

File tree

5 files changed

+12
-30
lines changed

5 files changed

+12
-30
lines changed

packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/FLTFirebaseAuthPlugin.m

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ @interface FLTFirebaseAuthPlugin ()
101101
@end
102102

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

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

117-
#endif
118-
119116
NSObject<FlutterBinaryMessenger> *_binaryMessenger;
120117
NSMutableDictionary<NSString *, FlutterEventChannel *> *_eventChannels;
121118
NSMutableDictionary<NSString *, NSObject<FlutterStreamHandler> *> *_streamHandlers;
@@ -133,13 +130,10 @@ - (instancetype)init:(NSObject<FlutterBinaryMessenger> *)messenger {
133130
_eventChannels = [NSMutableDictionary dictionary];
134131
_streamHandlers = [NSMutableDictionary dictionary];
135132

136-
#if TARGET_OS_IPHONE
137133
_multiFactorSessionMap = [NSMutableDictionary dictionary];
138134
_multiFactorResolverMap = [NSMutableDictionary dictionary];
139135
_multiFactorAssertionMap = [NSMutableDictionary dictionary];
140136
_multiFactorTotpSecretMap = [NSMutableDictionary dictionary];
141-
142-
#endif
143137
}
144138
return self;
145139
}
@@ -156,13 +150,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
156150
[registrar addApplicationDelegate:instance];
157151
SetUpFirebaseAuthHostApi(registrar.messenger, instance);
158152
SetUpFirebaseAuthUserHostApi(registrar.messenger, instance);
159-
160-
#if TARGET_OS_IPHONE
161153
SetUpMultiFactorUserHostApi(registrar.messenger, instance);
162154
SetUpMultiFactoResolverHostApi(registrar.messenger, instance);
163155
SetUpMultiFactorTotpHostApi(registrar.messenger, instance);
164156
SetUpMultiFactorTotpSecretHostApi(registrar.messenger, instance);
165-
#endif
166157
}
167158

168159
+ (FlutterError *)convertToFlutterError:(NSError *)error {
@@ -509,11 +500,6 @@ - (void)handleMultiFactorError:(AuthPigeonFirebaseApp *)app
509500
completion:(nonnull void (^)(PigeonUserCredential *_Nullable,
510501
FlutterError *_Nullable))completion
511502
withError:(NSError *_Nullable)error {
512-
#if TARGET_OS_OSX
513-
completion(nil, [FlutterError errorWithCode:@"second-factor-required"
514-
message:error.description
515-
details:nil]);
516-
#else
517503
FIRMultiFactorResolver *resolver =
518504
(FIRMultiFactorResolver *)error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
519505

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

560545
static void launchAppleSignInRequest(FLTFirebaseAuthPlugin *object, AuthPigeonFirebaseApp *app,
@@ -835,13 +820,11 @@ - (void)ensureAPNSTokenSetting {
835820
#endif
836821
}
837822

838-
#if !TARGET_OS_OSX
839823
- (FIRMultiFactor *)getAppMultiFactorFromPigeon:(nonnull AuthPigeonFirebaseApp *)app {
840824
FIRAuth *auth = [self getFIRAuthFromAppNameFromPigeon:app];
841825
FIRUser *currentUser = auth.currentUser;
842826
return currentUser.multiFactor;
843827
}
844-
#endif
845828

846829
- (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:
847830
(nonnull ASAuthorizationController *)controller API_AVAILABLE(macos(10.15), ios(13.0)) {
@@ -852,12 +835,16 @@ - (nonnull ASPresentationAnchor)presentationAnchorForAuthorizationController:
852835
#endif
853836
}
854837

855-
#if TARGET_OS_IPHONE
856-
857838
- (void)enrollPhoneApp:(nonnull AuthPigeonFirebaseApp *)app
858839
assertion:(nonnull PigeonPhoneMultiFactorAssertion *)assertion
859840
displayName:(nullable NSString *)displayName
860841
completion:(nonnull void (^)(FlutterError *_Nullable))completion {
842+
#if TARGET_OS_OSX
843+
completion([FlutterError errorWithCode:@"unsupported-platform"
844+
message:@"Phone authentication is not supported on macOS"
845+
details:nil]);
846+
#else
847+
861848
FIRMultiFactor *multiFactor = [self getAppMultiFactorFromPigeon:app];
862849

863850
FIRPhoneAuthCredential *credential =
@@ -879,6 +866,7 @@ - (void)enrollPhoneApp:(nonnull AuthPigeonFirebaseApp *)app
879866
details:nil]);
880867
}
881868
}];
869+
#endif
882870
}
883871

884872
- (void)getEnrolledFactorsApp:(nonnull AuthPigeonFirebaseApp *)app
@@ -969,10 +957,12 @@ - (void)resolveSignInResolverId:(nonnull NSString *)resolverId
969957
FIRMultiFactorAssertion *multiFactorAssertion;
970958

971959
if (assertion != nil) {
960+
#if TARGET_OS_IPHONE
972961
FIRPhoneAuthCredential *credential =
973962
[[FIRPhoneAuthProvider provider] credentialWithVerificationID:[assertion verificationId]
974963
verificationCode:[assertion verificationCode]];
975964
multiFactorAssertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
965+
#endif
976966
} else if (totpAssertionId != nil) {
977967
multiFactorAssertion = _multiFactorAssertionMap[totpAssertionId];
978968
} else {
@@ -1065,8 +1055,6 @@ - (void)openInOtpAppSecretKey:(nonnull NSString *)secretKey
10651055
completion(nil);
10661056
}
10671057

1068-
#endif
1069-
10701058
- (void)applyActionCodeApp:(nonnull AuthPigeonFirebaseApp *)app
10711059
code:(nonnull NSString *)code
10721060
completion:(nonnull void (^)(FlutterError *_Nullable))completion {

packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/PigeonParser.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,13 @@ + (PigeonAdditionalUserInfo *)getPigeonAdditionalUserInfo:(nonnull FIRAdditional
8282
profile:userInfo.profile];
8383
}
8484

85-
#if TARGET_OS_IPHONE
8685
+ (PigeonTotpSecret *)getPigeonTotpSecret:(FIRTOTPSecret *)secret {
8786
return [PigeonTotpSecret makeWithCodeIntervalSeconds:nil
8887
codeLength:nil
8988
enrollmentCompletionDeadline:nil
9089
hashingAlgorithm:nil
9190
secretKey:secret.sharedSecretKey];
9291
}
93-
#endif
9492

9593
+ (PigeonAuthCredential *)getPigeonAuthCredential:(FIRAuthCredential *)authCredential
9694
token:(NSNumber *_Nullable)token {

packages/firebase_auth/firebase_auth/ios/firebase_auth/Sources/firebase_auth/include/Private/PigeonParser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
(nullable PigeonActionCodeSettings *)settings;
2626
+ (PigeonUserCredential *_Nullable)getPigeonUserCredentialFromFIRUser:(nonnull FIRUser *)user;
2727
+ (PigeonIdTokenResult *_Nonnull)parseIdTokenResult:(nonnull FIRAuthTokenResult *)tokenResult;
28-
#if TARGET_OS_IPHONE
2928
+ (PigeonTotpSecret *_Nonnull)getPigeonTotpSecret:(nonnull FIRTOTPSecret *)secret;
30-
#endif
3129
+ (PigeonAuthCredential *_Nullable)getPigeonAuthCredential:
3230
(FIRAuthCredential *_Nullable)authCredentialToken
3331
token:(NSNumber *_Nullable)token;

packages/firebase_auth/firebase_auth/lib/src/user.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,11 +673,9 @@ class User {
673673
}
674674

675675
MultiFactor get multiFactor {
676-
if (!kIsWeb &&
677-
(defaultTargetPlatform == TargetPlatform.macOS ||
678-
defaultTargetPlatform == TargetPlatform.windows)) {
676+
if (!kIsWeb && (defaultTargetPlatform == TargetPlatform.windows)) {
679677
throw UnimplementedError(
680-
'MultiFactor Authentication is only supported on web, Android and iOS.',
678+
'MultiFactor Authentication is only supported on web, Android, iOS and macOS.',
681679
);
682680
}
683681
return _multiFactor ??= MultiFactor._(_delegate.multiFactor);

packages/firebase_auth/firebase_auth/macos/firebase_auth/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ let package = Package(
9393
.library(name: "firebase-auth", targets: ["firebase_auth"]),
9494
],
9595
dependencies: [
96-
.package(url: "https://github.com/firebase/firebase-ios-sdk", from: firebase_sdk_version),
96+
.package(url: "https://github.com/cbenhagen/firebase-ios-sdk", branch: "11.15.0-totp"),
9797
.package(url: "https://github.com/firebase/flutterfire", exact: shared_spm_version),
9898
],
9999
targets: [

0 commit comments

Comments
 (0)