Skip to content

Commit 4facac4

Browse files
author
Aaron Mandle
committed
Specify additonal scopes thorugh code rather than config.xml
1 parent df0eca0 commit 4facac4

File tree

12 files changed

+223
-46
lines changed

12 files changed

+223
-46
lines changed

app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
import android.view.View;
2727
import android.widget.Button;
2828
import android.widget.CheckBox;
29+
import android.widget.CompoundButton;
30+
import android.widget.CompoundButton.OnCheckedChangeListener;
2931
import android.widget.RadioButton;
3032

33+
import android.widget.TextView;
3134
import com.firebase.ui.auth.AuthUI;
3235
import com.firebase.ui.auth.ui.ResultCodes;
3336
import com.firebase.uidemo.R;
37+
import com.google.android.gms.common.Scopes;
3438
import com.google.firebase.auth.FirebaseAuth;
3539

3640
import java.util.ArrayList;
@@ -99,6 +103,24 @@ public class AuthUiActivity extends AppCompatActivity {
99103
@BindView(R.id.smartlock_enabled)
100104
CheckBox mEnableSmartLock;
101105

106+
@BindView(R.id.facebook_scopes_label)
107+
TextView mFacebookScopesLabel;
108+
109+
@BindView(R.id.facebook_scope_friends)
110+
CheckBox mFacebookScopeFriends;
111+
112+
@BindView(R.id.facebook_scope_photos)
113+
CheckBox mFacebookScopePhotos;
114+
115+
@BindView(R.id.google_scopes_label)
116+
TextView mGoogleScopesLabel;
117+
118+
@BindView(R.id.google_scope_drive_file)
119+
CheckBox mGoogleScopeDriveFile;
120+
121+
@BindView(R.id.google_scope_games)
122+
CheckBox mGoogleScopeGames;
123+
102124
@Override
103125
public void onCreate(Bundle savedInstanceState) {
104126
super.onCreate(savedInstanceState);
@@ -116,12 +138,30 @@ public void onCreate(Bundle savedInstanceState) {
116138
mUseGoogleProvider.setChecked(false);
117139
mUseGoogleProvider.setEnabled(false);
118140
mUseGoogleProvider.setText(R.string.google_label_missing_config);
141+
setGoogleScopesEnabled(false);
142+
} else {
143+
setGoogleScopesEnabled(mUseGoogleProvider.isChecked());
144+
mUseGoogleProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
145+
@Override
146+
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
147+
setGoogleScopesEnabled(checked);
148+
}
149+
});
119150
}
120151

121152
if (!isFacebookConfigured()) {
122153
mUseFacebookProvider.setChecked(false);
123154
mUseFacebookProvider.setEnabled(false);
124155
mUseFacebookProvider.setText(R.string.facebook_label_missing_config);
156+
setFacebookScopesEnabled(false);
157+
} else {
158+
setFacebookScopesEnabled(mUseFacebookProvider.isChecked());
159+
mUseFacebookProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
160+
@Override
161+
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
162+
setFacebookScopesEnabled(checked);
163+
}
164+
});
125165
}
126166

127167
if (!isTwitterConfigured()) {
@@ -144,6 +184,8 @@ public void signIn(View view) {
144184
.setLogo(getSelectedLogo())
145185
.setProviders(getSelectedProviders())
146186
.setTosUrl(getSelectedTosUrl())
187+
.setAdditonalFacebookPermissions(getFacebookPermissions())
188+
.setAdditonalGooglePermissions(getGooglePermissions())
147189
.setIsSmartLockEnabled(mEnableSmartLock.isChecked())
148190
.build(),
149191
RC_SIGN_IN);
@@ -160,6 +202,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
160202
showSnackbar(R.string.unknown_response);
161203
}
162204

205+
206+
@MainThread
207+
private void setGoogleScopesEnabled(boolean enabled) {
208+
mGoogleScopesLabel.setEnabled(enabled);
209+
mGoogleScopeDriveFile.setEnabled(enabled);
210+
mGoogleScopeGames.setEnabled(enabled);
211+
}
212+
213+
@MainThread
214+
private void setFacebookScopesEnabled(boolean enabled) {
215+
mFacebookScopesLabel.setEnabled(enabled);
216+
mFacebookScopeFriends.setEnabled(enabled);
217+
mFacebookScopePhotos.setEnabled(enabled);
218+
}
219+
163220
@MainThread
164221
private void handleSignInResponse(int resultCode, Intent data) {
165222
if (resultCode == RESULT_OK) {
@@ -265,6 +322,30 @@ private void showSnackbar(@StringRes int errorMessageRes) {
265322
Snackbar.make(mRootView, errorMessageRes, Snackbar.LENGTH_LONG).show();
266323
}
267324

325+
@MainThread
326+
private List<String> getFacebookPermissions() {
327+
List<String> result = new ArrayList<>();
328+
if (mFacebookScopeFriends.isChecked()) {
329+
result.add("user_friends");
330+
}
331+
if (mFacebookScopePhotos.isChecked()) {
332+
result.add("user_photos");
333+
}
334+
return result;
335+
}
336+
337+
@MainThread
338+
private List<String> getGooglePermissions() {
339+
List<String> result = new ArrayList<>();
340+
if (mGoogleScopeGames.isChecked()) {
341+
result.add(Scopes.GAMES);
342+
}
343+
if (mGoogleScopeDriveFile.isChecked()) {
344+
result.add(Scopes.DRIVE_FILE);
345+
}
346+
return result;
347+
}
348+
268349
public static Intent createIntent(Context context) {
269350
Intent in = new Intent();
270351
in.setClass(context, AuthUiActivity.class);

app/src/main/res/layout/auth_ui_layout.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,52 @@
157157
android:text="@string/no_logo_label" />
158158
</RadioGroup>
159159

160+
<TextView
161+
style="@style/Base.TextAppearance.AppCompat.Subhead"
162+
android:id="@+id/facebook_scopes_label"
163+
android:layout_width="wrap_content"
164+
android:layout_height="wrap_content"
165+
android:layout_marginBottom="8dp"
166+
android:layout_marginTop="16dp"
167+
android:text="@string/extra_facebook_scopes" />
168+
169+
<CheckBox
170+
android:id="@+id/facebook_scope_friends"
171+
android:layout_width="wrap_content"
172+
android:layout_height="wrap_content"
173+
android:checked="false"
174+
android:text="@string/friends" />
175+
176+
<CheckBox
177+
android:id="@+id/facebook_scope_photos"
178+
android:layout_width="wrap_content"
179+
android:layout_height="wrap_content"
180+
android:checked="false"
181+
android:text="@string/photos" />
182+
183+
<TextView
184+
style="@style/Base.TextAppearance.AppCompat.Subhead"
185+
android:id="@+id/google_scopes_label"
186+
android:layout_width="wrap_content"
187+
android:layout_height="wrap_content"
188+
android:layout_marginBottom="8dp"
189+
android:layout_marginTop="16dp"
190+
android:text="@string/extra_google_scopes" />
191+
192+
<CheckBox
193+
android:id="@+id/google_scope_games"
194+
android:layout_width="wrap_content"
195+
android:layout_height="wrap_content"
196+
android:checked="false"
197+
android:text="@string/games" />
198+
199+
<CheckBox
200+
android:id="@+id/google_scope_drive_file"
201+
android:layout_width="wrap_content"
202+
android:layout_height="wrap_content"
203+
android:checked="false"
204+
android:text="Drive File" />
205+
160206
<TextView
161207
style="@style/Base.TextAppearance.AppCompat.Subhead"
162208
android:layout_width="wrap_content"

app/src/main/res/values/strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,9 @@
4545
<string name="configuration_required">Configuration is required - see README.md</string>
4646
<string name="other_options_header">Other Options:</string>
4747
<string name="enable_smartlock">Enable SmartLock for Passwords</string>
48+
<string name="extra_google_scopes">Example extra Google scopes</string>
49+
<string name="games">Games</string>
50+
<string name="extra_facebook_scopes">Example extra Facebook scopes</string>
51+
<string name="friends">Friends</string>
52+
<string name="photos">Photos</string>
4853
</resources>

auth/src/main/java/com/firebase/ui/auth/AuthUI.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ public final class SignInIntentBuilder {
412412
private List<String> mProviders = Collections.singletonList(EMAIL_PROVIDER);
413413
private String mTosUrl;
414414
private boolean mIsSmartLockEnabled = true;
415+
private List<String> mAdditonalGooglePermissions;
416+
private List<String> mAdditonalFacebookPermissions;
415417

416418
private SignInIntentBuilder() {}
417419

@@ -477,6 +479,31 @@ public SignInIntentBuilder setIsSmartLockEnabled(boolean enabled) {
477479
return this;
478480
}
479481

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+
480507
public Intent build() {
481508
Context context = mApp.getApplicationContext();
482509
return build(context);
@@ -494,7 +521,9 @@ public Intent build(Context context) {
494521
mTheme,
495522
mLogo,
496523
mTosUrl,
497-
mIsSmartLockEnabled));
524+
mIsSmartLockEnabled,
525+
mAdditonalFacebookPermissions,
526+
mAdditonalGooglePermissions));
498527
}
499528
}
500529
}

auth/src/main/java/com/firebase/ui/auth/provider/FacebookProvider.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.json.JSONObject;
3838

3939
import java.util.ArrayList;
40-
import java.util.Arrays;
4140
import java.util.List;
4241

4342
public class FacebookProvider implements IDPProvider, FacebookCallback<LoginResult> {
@@ -51,12 +50,17 @@ public class FacebookProvider implements IDPProvider, FacebookCallback<LoginResu
5150
private static final String EMAIL = "email";
5251
private static final String PUBLIC_PROFILE = "public_profile";
5352

53+
private final List<String> mScopes;
5454
private CallbackManager mCallbackManager;
5555
private IDPCallback mCallbackObject;
5656

57-
public FacebookProvider (Context appContext, IDPProviderParcel facebookParcel) {
57+
public FacebookProvider (
58+
Context appContext,
59+
IDPProviderParcel facebookParcel,
60+
List<String> scopes) {
5861
mCallbackManager = CallbackManager.Factory.create();
5962
String applicationId = facebookParcel.getProviderExtra().getString(APPLICATION_ID);
63+
mScopes = scopes;
6064
FacebookSdk.sdkInitialize(appContext);
6165
FacebookSdk.setApplicationId(applicationId);
6266
}
@@ -83,8 +87,7 @@ public void startLogin(Activity activity) {
8387
LoginManager loginManager = LoginManager.getInstance();
8488
loginManager.registerCallback(mCallbackManager, this);
8589

86-
String[] permissions = activity.getResources().getStringArray(R.array.facebook_permissions);
87-
List<String> permissionsList = new ArrayList<>(Arrays.asList(permissions));
90+
List<String> permissionsList = new ArrayList<>(mScopes);
8891

8992
// Ensure we have email and public_profile scopes
9093
if (!permissionsList.contains(EMAIL)) {

auth/src/main/java/com/firebase/ui/auth/provider/GoogleProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.android.gms.common.api.Scope;
3737
import com.google.firebase.auth.AuthCredential;
3838
import com.google.firebase.auth.GoogleAuthProvider;
39+
import java.util.List;
3940

4041
public class GoogleProvider implements
4142
IDPProvider, OnClickListener, GoogleApiClient.OnConnectionFailedListener {
@@ -51,7 +52,11 @@ public class GoogleProvider implements
5152
private Activity mActivity;
5253
private IDPCallback mIDPCallback;
5354

54-
public GoogleProvider(FragmentActivity activity, IDPProviderParcel parcel, @Nullable String email) {
55+
public GoogleProvider(
56+
FragmentActivity activity,
57+
IDPProviderParcel parcel,
58+
@Nullable String email,
59+
List<String> scopes) {
5560
mActivity = activity;
5661
String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
5762
GoogleSignInOptions googleSignInOptions;
@@ -62,8 +67,7 @@ public GoogleProvider(FragmentActivity activity, IDPProviderParcel parcel, @Null
6267
.requestIdToken(mClientId);
6368

6469
// Add additional scopes
65-
String[] extraScopes = mActivity.getResources().getStringArray(R.array.google_permissions);
66-
for (String scopeString : extraScopes) {
70+
for (String scopeString : scopes) {
6771
builder.requestScopes(new Scope(scopeString));
6872
}
6973

auth/src/main/java/com/firebase/ui/auth/ui/FlowParameters.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.firebase.ui.auth.provider.IDPProviderParcel;
2424
import com.firebase.ui.auth.util.Preconditions;
2525

26+
import java.util.ArrayList;
2627
import java.util.List;
2728

2829
/**
@@ -48,19 +49,27 @@ public class FlowParameters implements Parcelable {
4849

4950
public final boolean smartLockEnabled;
5051

52+
public final List<String> additionalFacebookPermissions;
53+
54+
public final List<String> additionalGooglePermissions;
55+
5156
public FlowParameters(
5257
@NonNull String appName,
5358
@NonNull List<IDPProviderParcel> providerInfo,
5459
@StyleRes int themeId,
5560
@DrawableRes int logoId,
5661
@Nullable String termsOfServiceUrl,
57-
boolean smartLockEnabled) {
62+
boolean smartLockEnabled,
63+
List<String> additionalFacebookPermissions,
64+
List<String> additionalGooglePermissions) {
5865
this.appName = Preconditions.checkNotNull(appName, "appName cannot be null");
5966
this.providerInfo = Preconditions.checkNotNull(providerInfo, "providerInfo cannot be null");
6067
this.themeId = themeId;
6168
this.logoId = logoId;
6269
this.termsOfServiceUrl = termsOfServiceUrl;
6370
this.smartLockEnabled = smartLockEnabled;
71+
this.additionalFacebookPermissions = additionalFacebookPermissions;
72+
this.additionalGooglePermissions = additionalGooglePermissions;
6473
}
6574

6675
@Override
@@ -71,6 +80,8 @@ public void writeToParcel(Parcel dest, int flags) {
7180
dest.writeInt(logoId);
7281
dest.writeString(termsOfServiceUrl);
7382
dest.writeInt(smartLockEnabled ? 1 : 0);
83+
dest.writeStringList(additionalFacebookPermissions);
84+
dest.writeStringList(additionalGooglePermissions);
7485
}
7586

7687
@Override
@@ -89,9 +100,19 @@ public FlowParameters createFromParcel(Parcel in) {
89100
String termsOfServiceUrl = in.readString();
90101
int smartLockEnabledInt = in.readInt();
91102
boolean smartLockEnabled = (smartLockEnabledInt != 0);
92-
103+
List<String> additionalFacebookPermissions = new ArrayList<>();
104+
in.readStringList(additionalFacebookPermissions);
105+
List<String> additionalGooglePermissions = new ArrayList<>();
106+
in.readStringList(additionalGooglePermissions);
93107
return new FlowParameters(
94-
appName, providerInfo, themeId, logoId, termsOfServiceUrl, smartLockEnabled);
108+
appName,
109+
providerInfo,
110+
themeId,
111+
logoId,
112+
termsOfServiceUrl,
113+
smartLockEnabled,
114+
additionalFacebookPermissions,
115+
additionalGooglePermissions);
95116
}
96117

97118
@Override

0 commit comments

Comments
 (0)