Skip to content

Commit cbdc803

Browse files
authored
fix(authentication): reject calls when provider is not configured (#954)
* fix(authentication): reject calls when provider is not configured Add null/nil checks for auth provider handlers so that calling a sign-in or link method for a provider not listed in the Capacitor config rejects with a clear error message instead of crashing (Android) or silently hanging (iOS). * refactor(authentication): remove @objc from throwing phone methods These methods are only called from Swift, so @objc is unnecessary. * docs: add changeset
1 parent 1acd66a commit cbdc803

File tree

4 files changed

+155
-17
lines changed

4 files changed

+155
-17
lines changed

.changeset/good-hats-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@capacitor-firebase/authentication': patch
3+
---
4+
5+
fix: reject calls when provider is not configured

packages/authentication/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/authentication/FirebaseAuthentication.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,23 @@ public class FirebaseAuthentication {
6060
private FirebaseAuthenticationConfig config;
6161
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
6262
private FirebaseAuth.IdTokenListener firebaseIdTokenChangeListener;
63+
64+
@Nullable
6365
private AppleAuthProviderHandler appleAuthProviderHandler;
66+
67+
@Nullable
6468
private FacebookAuthProviderHandler facebookAuthProviderHandler;
69+
70+
@Nullable
6571
private GoogleAuthProviderHandler googleAuthProviderHandler;
72+
6673
public ActivityResultLauncher<IntentSenderRequest> googleAuthorizationResultLauncher;
6774
private OAuthProviderHandler oAuthProviderHandler;
75+
76+
@Nullable
6877
private PhoneAuthProviderHandler phoneAuthProviderHandler;
78+
79+
@Nullable
6980
private PlayGamesAuthProviderHandler playGamesAuthProviderHandler;
7081

7182
public FirebaseAuthentication(FirebaseAuthenticationPlugin plugin, FirebaseAuthenticationConfig config) {
@@ -136,6 +147,10 @@ public void confirmPasswordReset(@NonNull String oobCode, @NonNull String newPas
136147
}
137148

138149
public void confirmVerificationCode(@NonNull ConfirmVerificationCodeOptions options, @NonNull NonEmptyResultCallback callback) {
150+
if (phoneAuthProviderHandler == null) {
151+
callback.error(new Exception(createProviderNotEnabledErrorMessage("Phone")));
152+
return;
153+
}
139154
phoneAuthProviderHandler.confirmVerificationCode(options, callback);
140155
}
141156

@@ -221,6 +236,10 @@ public boolean isSignInWithEmailLink(@NonNull String emailLink) {
221236
}
222237

223238
public void linkWithApple(final PluginCall call) {
239+
if (appleAuthProviderHandler == null) {
240+
call.reject(createProviderNotEnabledErrorMessage("Apple"));
241+
return;
242+
}
224243
appleAuthProviderHandler.link(call);
225244
}
226245

@@ -289,6 +308,10 @@ public void linkWithEmailLink(final PluginCall call) {
289308
}
290309

291310
public void linkWithFacebook(final PluginCall call) {
311+
if (facebookAuthProviderHandler == null) {
312+
call.reject(createProviderNotEnabledErrorMessage("Facebook"));
313+
return;
314+
}
292315
facebookAuthProviderHandler.link(call);
293316
}
294317

@@ -297,6 +320,10 @@ public void linkWithGithub(final PluginCall call) {
297320
}
298321

299322
public void linkWithGoogle(final PluginCall call) {
323+
if (googleAuthProviderHandler == null) {
324+
call.reject(createProviderNotEnabledErrorMessage("Google"));
325+
return;
326+
}
300327
googleAuthProviderHandler.link(call);
301328
}
302329

@@ -309,10 +336,17 @@ public void linkWithOpenIdConnect(final PluginCall call, final String providerId
309336
}
310337

311338
public void linkWithPhoneNumber(@NonNull final LinkWithPhoneNumberOptions options) throws Exception {
339+
if (phoneAuthProviderHandler == null) {
340+
throw new Exception(createProviderNotEnabledErrorMessage("Phone"));
341+
}
312342
phoneAuthProviderHandler.link(options);
313343
}
314344

315345
public void linkWithPlayGames(final PluginCall call) {
346+
if (playGamesAuthProviderHandler == null) {
347+
call.reject(createProviderNotEnabledErrorMessage("Play Games"));
348+
return;
349+
}
316350
playGamesAuthProviderHandler.link(call);
317351
}
318352

@@ -416,6 +450,10 @@ public void signInAnonymously(final PluginCall call) {
416450
}
417451

418452
public void signInWithApple(final PluginCall call) {
453+
if (appleAuthProviderHandler == null) {
454+
call.reject(createProviderNotEnabledErrorMessage("Apple"));
455+
return;
456+
}
419457
appleAuthProviderHandler.signIn(call);
420458
}
421459

@@ -479,6 +517,10 @@ public void signInWithEmailLink(final PluginCall call) {
479517
}
480518

481519
public void signInWithFacebook(final PluginCall call) {
520+
if (facebookAuthProviderHandler == null) {
521+
call.reject(createProviderNotEnabledErrorMessage("Facebook"));
522+
return;
523+
}
482524
facebookAuthProviderHandler.signIn(call);
483525
}
484526

@@ -487,6 +529,10 @@ public void signInWithGithub(final PluginCall call) {
487529
}
488530

489531
public void signInWithGoogle(final PluginCall call) {
532+
if (googleAuthProviderHandler == null) {
533+
call.reject(createProviderNotEnabledErrorMessage("Google"));
534+
return;
535+
}
490536
googleAuthProviderHandler.signIn(call);
491537
}
492538

@@ -499,10 +545,17 @@ public void signInWithOpenIdConnect(final PluginCall call, final String provider
499545
}
500546

501547
public void signInWithPhoneNumber(final SignInWithPhoneNumberOptions options) throws Exception {
548+
if (phoneAuthProviderHandler == null) {
549+
throw new Exception(createProviderNotEnabledErrorMessage("Phone"));
550+
}
502551
phoneAuthProviderHandler.signIn(options);
503552
}
504553

505554
public void signInWithPlayGames(final PluginCall call) {
555+
if (playGamesAuthProviderHandler == null) {
556+
call.reject(createProviderNotEnabledErrorMessage("Play Games"));
557+
return;
558+
}
506559
playGamesAuthProviderHandler.signIn(call);
507560
}
508561

@@ -633,18 +686,34 @@ public void startActivityForResult(final PluginCall call, Intent intent, String
633686
}
634687

635688
public void handleGoogleAuthProviderSignInActivityResult(@NonNull final PluginCall call, @NonNull ActivityResult result) {
689+
if (googleAuthProviderHandler == null) {
690+
call.reject(createProviderNotEnabledErrorMessage("Google"));
691+
return;
692+
}
636693
googleAuthProviderHandler.handleOnActivityResult(call, result, false);
637694
}
638695

639696
public void handleGoogleAuthProviderLinkActivityResult(@NonNull final PluginCall call, @NonNull ActivityResult result) {
697+
if (googleAuthProviderHandler == null) {
698+
call.reject(createProviderNotEnabledErrorMessage("Google"));
699+
return;
700+
}
640701
googleAuthProviderHandler.handleOnActivityResult(call, result, true);
641702
}
642703

643704
public void handlePlayGamesAuthProviderSignInActivityResult(@NonNull final PluginCall call, @NonNull ActivityResult result) {
705+
if (playGamesAuthProviderHandler == null) {
706+
call.reject(createProviderNotEnabledErrorMessage("Play Games"));
707+
return;
708+
}
644709
playGamesAuthProviderHandler.handleOnActivityResult(call, result, false);
645710
}
646711

647712
public void handlePlayGamesAuthProviderLinkActivityResult(@NonNull final PluginCall call, @NonNull ActivityResult result) {
713+
if (playGamesAuthProviderHandler == null) {
714+
call.reject(createProviderNotEnabledErrorMessage("Play Games"));
715+
return;
716+
}
648717
playGamesAuthProviderHandler.handleOnActivityResult(call, result, true);
649718
}
650719

@@ -897,6 +966,14 @@ public FirebaseAuthenticationConfig getConfig() {
897966
return config;
898967
}
899968

969+
@NonNull
970+
private static String createProviderNotEnabledErrorMessage(@NonNull String providerName) {
971+
return (
972+
providerName +
973+
" sign-in provider is not enabled. Make sure to add the provider to the 'providers' list in the Capacitor configuration."
974+
);
975+
}
976+
900977
private void initAuthProviderHandlers(FirebaseAuthenticationConfig config) {
901978
List<String> providerList = Arrays.asList(config.getProviders());
902979
if (providerList.contains(ProviderId.APPLE)) {

packages/authentication/ios/Plugin/FirebaseAuthentication.swift

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public typealias AuthStateChangedObserver = () -> Void
8282
}
8383

8484
@objc func confirmVerificationCode(_ options: ConfirmVerificationCodeOptions, completion: @escaping (Result?, Error?) -> Void) {
85-
self.phoneAuthProviderHandler?.confirmVerificationCode(options, completion: completion)
85+
guard let phoneAuthProviderHandler = self.phoneAuthProviderHandler else {
86+
completion(nil, RuntimeError(createProviderNotEnabledErrorMessage("Phone")))
87+
return
88+
}
89+
phoneAuthProviderHandler.confirmVerificationCode(options, completion: completion)
8690
}
8791

8892
@objc func deleteUser(user: User, completion: @escaping (Error?) -> Void) {
@@ -151,8 +155,12 @@ public typealias AuthStateChangedObserver = () -> Void
151155
}
152156

153157
@objc func linkWithApple(_ call: CAPPluginCall) {
158+
guard let appleAuthProviderHandler = self.appleAuthProviderHandler else {
159+
call.reject(createProviderNotEnabledErrorMessage("Apple"))
160+
return
161+
}
154162
self.savedCall = call
155-
self.appleAuthProviderHandler?.link(call: call)
163+
appleAuthProviderHandler.link(call: call)
156164
}
157165

158166
@objc func linkWithEmailAndPassword(_ call: CAPPluginCall) {
@@ -215,13 +223,21 @@ public typealias AuthStateChangedObserver = () -> Void
215223
}
216224

217225
@objc func linkWithFacebook(_ call: CAPPluginCall) {
226+
guard let facebookAuthProviderHandler = self.facebookAuthProviderHandler else {
227+
call.reject(createProviderNotEnabledErrorMessage("Facebook"))
228+
return
229+
}
218230
self.savedCall = call
219-
self.facebookAuthProviderHandler?.link(call: call)
231+
facebookAuthProviderHandler.link(call: call)
220232
}
221233

222234
@objc func linkWithGameCenter(_ call: CAPPluginCall) {
235+
guard let gameCenterAuthProviderHandler = self.gameCenterAuthProviderHandler else {
236+
call.reject(createProviderNotEnabledErrorMessage("Game Center"))
237+
return
238+
}
223239
self.savedCall = call
224-
self.gameCenterAuthProviderHandler?.link(call: call)
240+
gameCenterAuthProviderHandler.link(call: call)
225241
}
226242

227243
@objc func linkWithGithub(_ call: CAPPluginCall) {
@@ -230,8 +246,12 @@ public typealias AuthStateChangedObserver = () -> Void
230246
}
231247

232248
@objc func linkWithGoogle(_ call: CAPPluginCall) {
249+
guard let googleAuthProviderHandler = self.googleAuthProviderHandler else {
250+
call.reject(createProviderNotEnabledErrorMessage("Google"))
251+
return
252+
}
233253
self.savedCall = call
234-
self.googleAuthProviderHandler?.link(call: call)
254+
googleAuthProviderHandler.link(call: call)
235255
}
236256

237257
@objc func linkWithMicrosoft(_ call: CAPPluginCall) {
@@ -244,8 +264,11 @@ public typealias AuthStateChangedObserver = () -> Void
244264
self.oAuthProviderHandler?.link(call: call, providerId: providerId)
245265
}
246266

247-
@objc func linkWithPhoneNumber(_ options: LinkWithPhoneNumberOptions) {
248-
self.phoneAuthProviderHandler?.link(options)
267+
func linkWithPhoneNumber(_ options: LinkWithPhoneNumberOptions) throws {
268+
guard let phoneAuthProviderHandler = self.phoneAuthProviderHandler else {
269+
throw RuntimeError(createProviderNotEnabledErrorMessage("Phone"))
270+
}
271+
phoneAuthProviderHandler.link(options)
249272
}
250273

251274
@objc func linkWithTwitter(_ call: CAPPluginCall) {
@@ -358,8 +381,12 @@ public typealias AuthStateChangedObserver = () -> Void
358381
}
359382

360383
@objc func signInWithApple(_ call: CAPPluginCall) {
384+
guard let appleAuthProviderHandler = self.appleAuthProviderHandler else {
385+
call.reject(createProviderNotEnabledErrorMessage("Apple"))
386+
return
387+
}
361388
self.savedCall = call
362-
self.appleAuthProviderHandler?.signIn(call: call)
389+
appleAuthProviderHandler.signIn(call: call)
363390
}
364391

365392
@objc func signInWithCustomToken(_ call: CAPPluginCall) {
@@ -440,13 +467,21 @@ public typealias AuthStateChangedObserver = () -> Void
440467
}
441468

442469
@objc func signInWithFacebook(_ call: CAPPluginCall) {
470+
guard let facebookAuthProviderHandler = self.facebookAuthProviderHandler else {
471+
call.reject(createProviderNotEnabledErrorMessage("Facebook"))
472+
return
473+
}
443474
self.savedCall = call
444-
self.facebookAuthProviderHandler?.signIn(call: call)
475+
facebookAuthProviderHandler.signIn(call: call)
445476
}
446477

447478
@objc func signInWithGameCenter(_ call: CAPPluginCall) {
479+
guard let gameCenterAuthProviderHandler = self.gameCenterAuthProviderHandler else {
480+
call.reject(createProviderNotEnabledErrorMessage("Game Center"))
481+
return
482+
}
448483
self.savedCall = call
449-
self.gameCenterAuthProviderHandler?.signIn(call: call)
484+
gameCenterAuthProviderHandler.signIn(call: call)
450485
}
451486

452487
@objc func signInWithGithub(_ call: CAPPluginCall) {
@@ -455,8 +490,12 @@ public typealias AuthStateChangedObserver = () -> Void
455490
}
456491

457492
@objc func signInWithGoogle(_ call: CAPPluginCall) {
493+
guard let googleAuthProviderHandler = self.googleAuthProviderHandler else {
494+
call.reject(createProviderNotEnabledErrorMessage("Google"))
495+
return
496+
}
458497
self.savedCall = call
459-
self.googleAuthProviderHandler?.signIn(call: call)
498+
googleAuthProviderHandler.signIn(call: call)
460499
}
461500

462501
@objc func signInWithMicrosoft(_ call: CAPPluginCall) {
@@ -469,8 +508,11 @@ public typealias AuthStateChangedObserver = () -> Void
469508
self.oAuthProviderHandler?.signIn(call: call, providerId: providerId)
470509
}
471510

472-
@objc func signInWithPhoneNumber(_ options: SignInWithPhoneNumberOptions) {
473-
self.phoneAuthProviderHandler?.signIn(options)
511+
func signInWithPhoneNumber(_ options: SignInWithPhoneNumberOptions) throws {
512+
guard let phoneAuthProviderHandler = self.phoneAuthProviderHandler else {
513+
throw RuntimeError(createProviderNotEnabledErrorMessage("Phone"))
514+
}
515+
phoneAuthProviderHandler.signIn(options)
474516
}
475517

476518
@objc func signInWithTwitter(_ call: CAPPluginCall) {
@@ -676,6 +718,10 @@ public typealias AuthStateChangedObserver = () -> Void
676718
completion(CheckAppTrackingTransparencyPermissionResult(ATTrackingManager.trackingAuthorizationStatus))
677719
}
678720

721+
private func createProviderNotEnabledErrorMessage(_ providerName: String) -> String {
722+
return "\(providerName) sign-in provider is not enabled. Make sure to add the provider to the 'providers' list in the Capacitor configuration."
723+
}
724+
679725
private func initAuthProviderHandlers(config: FirebaseAuthenticationConfig) {
680726
if config.providers.contains(ProviderId.apple) {
681727
self.appleAuthProviderHandler = AppleAuthProviderHandler(self)

packages/authentication/ios/Plugin/FirebaseAuthenticationPlugin.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,13 @@ public class FirebaseAuthenticationPlugin: CAPPlugin, CAPBridgedPlugin {
327327
}
328328
let options = LinkWithPhoneNumberOptions(phoneNumber: phoneNumber)
329329

330-
implementation?.linkWithPhoneNumber(options)
331-
call.resolve()
330+
do {
331+
try implementation?.linkWithPhoneNumber(options)
332+
call.resolve()
333+
} catch {
334+
CAPLog.print("[", self.tag, "] ", error)
335+
call.reject(error.localizedDescription)
336+
}
332337
}
333338

334339
@objc func linkWithPlayGames(_ call: CAPPluginCall) {
@@ -518,8 +523,13 @@ public class FirebaseAuthenticationPlugin: CAPPlugin, CAPBridgedPlugin {
518523
}
519524
let options = SignInWithPhoneNumberOptions(skipNativeAuth: skipNativeAuth, phoneNumber: phoneNumber)
520525

521-
implementation?.signInWithPhoneNumber(options)
522-
call.resolve()
526+
do {
527+
try implementation?.signInWithPhoneNumber(options)
528+
call.resolve()
529+
} catch {
530+
CAPLog.print("[", self.tag, "] ", error)
531+
call.reject(error.localizedDescription)
532+
}
523533
}
524534

525535
@objc func signInWithPlayGames(_ call: CAPPluginCall) {

0 commit comments

Comments
 (0)