17
17
import android .app .Activity ;
18
18
import android .content .Context ;
19
19
import android .content .Intent ;
20
+ import android .os .Parcel ;
21
+ import android .os .Parcelable ;
20
22
import android .support .annotation .DrawableRes ;
21
23
import android .support .annotation .NonNull ;
22
24
import android .support .annotation .Nullable ;
23
25
import android .support .annotation .StyleRes ;
24
26
import android .support .annotation .VisibleForTesting ;
25
-
26
27
import com .facebook .FacebookSdk ;
27
28
import com .facebook .login .LoginManager ;
28
- import com .firebase .ui .auth .provider .IDPProviderParcel ;
29
29
import com .firebase .ui .auth .ui .ChooseAccountActivity ;
30
30
import com .firebase .ui .auth .ui .FlowParameters ;
31
31
import com .firebase .ui .auth .ui .idp .AuthMethodPickerActivity ;
32
32
import com .firebase .ui .auth .util .CredentialsApiHelper ;
33
33
import com .firebase .ui .auth .util .GoogleApiClientTaskHelper ;
34
34
import com .firebase .ui .auth .util .Preconditions ;
35
- import com .firebase .ui .auth .util .ProviderHelper ;
36
35
import com .firebase .ui .auth .util .SmartlockUtil ;
37
36
import com .google .android .gms .auth .api .Auth ;
38
37
import com .google .android .gms .auth .api .credentials .Credential ;
45
44
import com .google .firebase .FirebaseApp ;
46
45
import com .google .firebase .auth .FirebaseAuth ;
47
46
import com .google .firebase .auth .FirebaseUser ;
48
-
49
47
import java .util .ArrayList ;
50
48
import java .util .Arrays ;
51
49
import java .util .Collections ;
105
103
* startActivityForResult(
106
104
* AuthUI.getInstance()
107
105
* .createSignInIntentBuilder()
108
- * .setProviders(AuthUI.EMAIL_PROVIDER, AuthUI.GOOGLE_PROVIDER, AuthUI.FACEBOOK_PROVIDER)
106
+ * .setProviders(
107
+ * Arrays.asList(
108
+ * new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
109
+ * new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
110
+ * new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build()
111
+ * )
112
+ * )
109
113
* .build(),
110
114
* RC_SIGN_IN);
111
115
* }
@@ -403,19 +407,111 @@ public static AuthUI getInstance(FirebaseApp app) {
403
407
return R .style .FirebaseUI ;
404
408
}
405
409
410
+
411
+ /**
412
+ * Configuration for an identity provider.
413
+ *
414
+ * In the simplest case, you can supply the provider ID and build the config like this:
415
+ * {@code new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()}
416
+ */
417
+ public static class IdpConfig implements Parcelable {
418
+ private final String mProviderId ;
419
+ private final List <String > mScopes ;
420
+
421
+ private IdpConfig (@ NonNull String providerId , List <String > scopes ) {
422
+ mScopes = scopes ;
423
+ mProviderId = providerId ;
424
+ }
425
+
426
+ public String getProviderId () {
427
+ return mProviderId ;
428
+ }
429
+
430
+ public List <String > getScopes () {
431
+ return mScopes ;
432
+ }
433
+
434
+ protected IdpConfig (Parcel in ) {
435
+ mProviderId = in .readString ();
436
+ mScopes = in .createStringArrayList ();
437
+ }
438
+
439
+ public static final Creator <IdpConfig > CREATOR = new Creator <IdpConfig >() {
440
+ @ Override
441
+ public IdpConfig createFromParcel (Parcel in ) {
442
+ return new IdpConfig (in );
443
+ }
444
+
445
+ @ Override
446
+ public IdpConfig [] newArray (int size ) {
447
+ return new IdpConfig [size ];
448
+ }
449
+ };
450
+
451
+ @ Override
452
+ public int describeContents () {
453
+ return 0 ;
454
+ }
455
+
456
+ @ Override
457
+ public void writeToParcel (Parcel parcel , int i ) {
458
+ parcel .writeString (mProviderId );
459
+ parcel .writeStringList (mScopes );
460
+ }
461
+
462
+ public static class Builder {
463
+ private String mProviderId ;
464
+ private List <String > mScopes ;
465
+
466
+
467
+ /**
468
+ * Builds the configuration parameters for an identity provider.
469
+ * @param providerId An ID of one of the supported identity providers. e.g.
470
+ * {@link AuthUI#GOOGLE_PROVIDER}. See {@link AuthUI#SUPPORTED_PROVIDERS} for the
471
+ * complete list of supported Identity providers
472
+ */
473
+ public Builder (@ NonNull String providerId ) {
474
+ if (!SUPPORTED_PROVIDERS .contains (providerId )) {
475
+ throw new IllegalArgumentException ("Unkown provider: " + providerId );
476
+ }
477
+ mProviderId = providerId ;
478
+ }
479
+
480
+ /**
481
+ * Specifies the additional permissions that the application will request for this
482
+ * identity provider.
483
+ *
484
+ * For Facebook permissions see:
485
+ * https://developers.facebook.com/docs/facebook-login/android
486
+ * https://developers.facebook.com/docs/facebook-login/permissions
487
+ *
488
+ * For Google permissions see:
489
+ * https://developers.google.com/identity/protocols/googlescopes
490
+ */
491
+ public Builder setPermissions (List <String > permissions ) {
492
+ mScopes = permissions ;
493
+ return this ;
494
+ }
495
+
496
+ public IdpConfig build () {
497
+ return new IdpConfig (mProviderId , mScopes );
498
+ }
499
+ }
500
+ }
501
+
406
502
/**
407
503
* Builder for the intent to start the user authentication flow.
408
504
*/
409
505
public final class SignInIntentBuilder {
410
506
private int mLogo = NO_LOGO ;
411
507
private int mTheme = getDefaultTheme ();
412
- private List <String > mProviders = Collections . singletonList ( EMAIL_PROVIDER );
508
+ private List <IdpConfig > mProviders = new ArrayList <>( );
413
509
private String mTosUrl ;
414
510
private boolean mIsSmartLockEnabled = true ;
415
- private List <String > mAdditonalGooglePermissions ;
416
- private List <String > mAdditonalFacebookPermissions ;
417
511
418
- private SignInIntentBuilder () {}
512
+ private SignInIntentBuilder () {
513
+ mProviders .add (new IdpConfig .Builder (EMAIL_PROVIDER ).build ());
514
+ }
419
515
420
516
/**
421
517
* Specifies the theme to use for the application flow. If no theme is specified,
@@ -447,10 +543,34 @@ public SignInIntentBuilder setTosUrl(@Nullable String tosUrl) {
447
543
return this ;
448
544
}
449
545
546
+ /**
547
+ * Specified the set of supported authentication providers. At least one provider must
548
+ * be specified. There may only be one instance of each provider.
549
+ *
550
+ * <p>If no providers are explicitly specified by calling this method, then the email
551
+ * provider is the default supported provider.
552
+ *
553
+ * @param idpConfigs a list of {@link IdpConfig}s, where each {@link IdpConfig} contains
554
+ * the configuration parameters for the IDP.
555
+ *
556
+ * @see IdpConfig
557
+ */
558
+ public SignInIntentBuilder setProviders (@ NonNull List <IdpConfig > idpConfigs ) {
559
+ Set <String > configuredProviders = new HashSet <>();
560
+ for (IdpConfig idpConfig : idpConfigs ) {
561
+ if (!configuredProviders .add (idpConfig .getProviderId ())) {
562
+ throw new IllegalArgumentException ("Each provider can only be set once. "
563
+ + idpConfig .getProviderId () + " was set twice." );
564
+ }
565
+ }
566
+ mProviders = idpConfigs ;
567
+ return this ;
568
+ }
569
+
450
570
/**
451
571
* Specifies the set of supported authentication providers. At least one provider
452
572
* must be specified, and the set of providers must be a subset of
453
- * {@link #SUPPORTED_PROVIDERS}.
573
+ * {@link #SUPPORTED_PROVIDERS}. There may only be one instance of each provider.
454
574
*
455
575
* <p>If no providers are explicitly specified by calling this method, then
456
576
* {@link #EMAIL_PROVIDER email} is the default supported provider.
@@ -459,12 +579,14 @@ public SignInIntentBuilder setTosUrl(@Nullable String tosUrl) {
459
579
* @see #FACEBOOK_PROVIDER
460
580
* @see #GOOGLE_PROVIDER
461
581
*/
582
+ @ Deprecated
462
583
public SignInIntentBuilder setProviders (@ NonNull String ... providers ) {
463
- mProviders = Arrays . asList ( providers );
464
- for (String provider : mProviders ) {
465
- if (! SUPPORTED_PROVIDERS . contains (provider )) {
466
- throw new IllegalArgumentException ("Unknown provider : " + provider );
584
+ mProviders . clear (); // clear the default email provider
585
+ for (String provider : providers ) {
586
+ if (isIdpAlreadyConfigured (provider )) {
587
+ throw new IllegalArgumentException ("Provider already configured : " + provider );
467
588
}
589
+ mProviders .add (new IdpConfig .Builder (provider ).build ());
468
590
}
469
591
return this ;
470
592
}
@@ -479,51 +601,31 @@ public SignInIntentBuilder setIsSmartLockEnabled(boolean enabled) {
479
601
return this ;
480
602
}
481
603
482
- /**
483
- * Specifies additional Google scopes that this application will request from the user.
484
- * Note that {@link com.google.android.gms.common.Scopes.EMAIL} and
485
- * {@link com.google.android.gms.common.Scopes.PROFILE} are alawys requested.
486
- *
487
- * For a list of all scopes, see:
488
- * https://developers.google.com/identity/protocols/googlescopes
489
- */
490
- public SignInIntentBuilder setAdditonalGooglePermissions (List <String > permissions ) {
491
- mAdditonalGooglePermissions = permissions ;
492
- return this ;
493
- }
494
-
495
- /**
496
- * Specifies the Facebook permissions that this application will request from the user.
497
- *
498
- * See:
499
- * https://developers.facebook.com/docs/facebook-login/android
500
- * https://developers.facebook.com/docs/facebook-login/permissions
501
- */
502
- public SignInIntentBuilder setAdditonalFacebookPermissions (List <String > permissions ) {
503
- mAdditonalFacebookPermissions = permissions ;
504
- return this ;
505
- }
506
-
507
604
public Intent build () {
508
605
Context context = mApp .getApplicationContext ();
509
606
return build (context );
510
607
}
511
608
512
609
@ VisibleForTesting
513
610
public Intent build (Context context ) {
514
- List <IDPProviderParcel > providerInfo =
515
- ProviderHelper .getProviderParcels (context , mProviders );
516
611
return ChooseAccountActivity .createIntent (
517
612
context ,
518
613
new FlowParameters (
519
614
mApp .getName (),
520
- providerInfo ,
615
+ mProviders ,
521
616
mTheme ,
522
617
mLogo ,
523
618
mTosUrl ,
524
- mIsSmartLockEnabled ,
525
- mAdditonalFacebookPermissions ,
526
- mAdditonalGooglePermissions ));
619
+ mIsSmartLockEnabled ));
620
+ }
621
+
622
+ private boolean isIdpAlreadyConfigured (@ NonNull String providerId ) {
623
+ for (IdpConfig config : mProviders ) {
624
+ if (config .getProviderId ().equals (providerId )) {
625
+ return true ;
626
+ }
627
+ }
628
+ return false ;
527
629
}
528
630
}
529
631
}
0 commit comments