Skip to content

Commit 897b455

Browse files
authored
Specify additonal scopes thorugh code rather than config.xml (#342)
Specify additonal scopes through code rather than config.xml
1 parent f0d4d15 commit 897b455

28 files changed

+504
-310
lines changed

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

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@
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;
35+
import com.firebase.ui.auth.AuthUI.IdpConfig;
3236
import com.firebase.ui.auth.ui.ResultCodes;
3337
import com.firebase.uidemo.R;
38+
import com.google.android.gms.common.Scopes;
3439
import com.google.firebase.auth.FirebaseAuth;
3540

3641
import java.util.ArrayList;
@@ -99,6 +104,24 @@ public class AuthUiActivity extends AppCompatActivity {
99104
@BindView(R.id.smartlock_enabled)
100105
CheckBox mEnableSmartLock;
101106

107+
@BindView(R.id.facebook_scopes_label)
108+
TextView mFacebookScopesLabel;
109+
110+
@BindView(R.id.facebook_scope_friends)
111+
CheckBox mFacebookScopeFriends;
112+
113+
@BindView(R.id.facebook_scope_photos)
114+
CheckBox mFacebookScopePhotos;
115+
116+
@BindView(R.id.google_scopes_label)
117+
TextView mGoogleScopesLabel;
118+
119+
@BindView(R.id.google_scope_drive_file)
120+
CheckBox mGoogleScopeDriveFile;
121+
122+
@BindView(R.id.google_scope_games)
123+
CheckBox mGoogleScopeGames;
124+
102125
@Override
103126
public void onCreate(Bundle savedInstanceState) {
104127
super.onCreate(savedInstanceState);
@@ -116,12 +139,30 @@ public void onCreate(Bundle savedInstanceState) {
116139
mUseGoogleProvider.setChecked(false);
117140
mUseGoogleProvider.setEnabled(false);
118141
mUseGoogleProvider.setText(R.string.google_label_missing_config);
142+
setGoogleScopesEnabled(false);
143+
} else {
144+
setGoogleScopesEnabled(mUseGoogleProvider.isChecked());
145+
mUseGoogleProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
146+
@Override
147+
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
148+
setGoogleScopesEnabled(checked);
149+
}
150+
});
119151
}
120152

121153
if (!isFacebookConfigured()) {
122154
mUseFacebookProvider.setChecked(false);
123155
mUseFacebookProvider.setEnabled(false);
124156
mUseFacebookProvider.setText(R.string.facebook_label_missing_config);
157+
setFacebookScopesEnabled(false);
158+
} else {
159+
setFacebookScopesEnabled(mUseFacebookProvider.isChecked());
160+
mUseFacebookProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
161+
@Override
162+
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
163+
setFacebookScopesEnabled(checked);
164+
}
165+
});
125166
}
126167

127168
if (!isTwitterConfigured()) {
@@ -160,6 +201,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
160201
showSnackbar(R.string.unknown_response);
161202
}
162203

204+
205+
@MainThread
206+
private void setGoogleScopesEnabled(boolean enabled) {
207+
mGoogleScopesLabel.setEnabled(enabled);
208+
mGoogleScopeDriveFile.setEnabled(enabled);
209+
mGoogleScopeGames.setEnabled(enabled);
210+
}
211+
212+
@MainThread
213+
private void setFacebookScopesEnabled(boolean enabled) {
214+
mFacebookScopesLabel.setEnabled(enabled);
215+
mFacebookScopeFriends.setEnabled(enabled);
216+
mFacebookScopePhotos.setEnabled(enabled);
217+
}
218+
163219
@MainThread
164220
private void handleSignInResponse(int resultCode, Intent data) {
165221
if (resultCode == RESULT_OK) {
@@ -207,26 +263,32 @@ private int getSelectedLogo() {
207263
}
208264

209265
@MainThread
210-
private String[] getSelectedProviders() {
211-
ArrayList<String> selectedProviders = new ArrayList<>();
266+
private List<IdpConfig> getSelectedProviders() {
267+
List<IdpConfig> selectedProviders = new ArrayList<>();
212268

213269
if (mUseEmailProvider.isChecked()) {
214-
selectedProviders.add(AuthUI.EMAIL_PROVIDER);
270+
selectedProviders.add(new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
215271
}
216272

217273
if (mUseFacebookProvider.isChecked()) {
218-
selectedProviders.add(AuthUI.FACEBOOK_PROVIDER);
274+
selectedProviders.add(
275+
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER)
276+
.setPermissions(getFacebookPermissions())
277+
.build());
219278
}
220279

221280
if (mUseGoogleProvider.isChecked()) {
222-
selectedProviders.add(AuthUI.GOOGLE_PROVIDER);
281+
selectedProviders.add(
282+
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
283+
.setPermissions(getGooglePermissions())
284+
.build());
223285
}
224286

225287
if (mUseTwitterProvider.isChecked()) {
226-
selectedProviders.add(AuthUI.TWITTER_PROVIDER);
288+
selectedProviders.add(new IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build());
227289
}
228290

229-
return selectedProviders.toArray(new String[selectedProviders.size()]);
291+
return selectedProviders;
230292
}
231293

232294
@MainThread
@@ -265,6 +327,30 @@ private void showSnackbar(@StringRes int errorMessageRes) {
265327
Snackbar.make(mRootView, errorMessageRes, Snackbar.LENGTH_LONG).show();
266328
}
267329

330+
@MainThread
331+
private List<String> getFacebookPermissions() {
332+
List<String> result = new ArrayList<>();
333+
if (mFacebookScopeFriends.isChecked()) {
334+
result.add("user_friends");
335+
}
336+
if (mFacebookScopePhotos.isChecked()) {
337+
result.add("user_photos");
338+
}
339+
return result;
340+
}
341+
342+
@MainThread
343+
private List<String> getGooglePermissions() {
344+
List<String> result = new ArrayList<>();
345+
if (mGoogleScopeGames.isChecked()) {
346+
result.add(Scopes.GAMES);
347+
}
348+
if (mGoogleScopeDriveFile.isChecked()) {
349+
result.add(Scopes.DRIVE_FILE);
350+
}
351+
return result;
352+
}
353+
268354
public static Intent createIntent(Context context) {
269355
Intent in = new Intent();
270356
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
<string name="other_options_header">Other Options:</string>
4949
<string name="enable_smartlock">Enable SmartLock for Passwords</string>
5050
<string name="rational_image_perm">This sample will read an image from local storage to upload to Firebase Storage.</string>
51-
5251
<string name="anonymous_auth_failed_toast">Authentication failed, uploads and downloads will not work.</string>
5352
<string name="anonymous_auth_failed_msg">Anonymous authentication failed. Make sure your device is online and that Anonymous Auth is configured in your Firebase project(https://console.firebase.google.com/project/_/authentication/providers)</string>
53+
<string name="extra_google_scopes">Example extra Google scopes</string>
54+
<string name="games">Games</string>
55+
<string name="extra_facebook_scopes">Example extra Facebook scopes</string>
56+
<string name="friends">Friends</string>
57+
<string name="photos">Photos</string>
5458
</resources>

auth/README.md

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ startActivityForResult(
150150
AuthUI.getInstance()
151151
.createSignInIntentBuilder()
152152
.setProviders(
153-
AuthUI.EMAIL_PROVIDER,
154-
AuthUI.GOOGLE_PROVIDER,
155-
AuthUI.FACEBOOK_PROVIDER)
153+
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
154+
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
155+
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build())
156156
.build(),
157157
RC_SIGN_IN);
158158
```
@@ -367,35 +367,55 @@ redefine a string to change it, for example:
367367

368368
#### Google
369369
By default, FirebaseUI requests the `email` and `profile` scopes when using Google Sign In. If you
370-
would like to request additional scopes from the user, add a string array resource named
371-
`google_permissions` to your `strings.xml` file like this:
370+
would like to request additional scopes from the user, call `setPermissions` on the
371+
`AuthUI.IdpConfig.Builder` when initializing FirebaseUI.
372372

373-
```xml
374-
<!--
375-
For a list of all scopes, see:
376-
https://developers.google.com/identity/protocols/googlescopes
377-
-->
378-
<string-array name="google_permissions">
379-
<!-- Request permission to read the user's Google Drive files -->
380-
<item>https://www.googleapis.com/auth/drive.readonly</item>
381-
</string-array>
373+
374+
```java
375+
// For a list of all scopes, see:
376+
// https://developers.google.com/identity/protocols/googlescopes
377+
AuthUI.IdpConfig googleIdp = new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
378+
.setPermissions(Arrays.asList(Scopes.GAMES))
379+
.build();
380+
381+
startActivityForResult(
382+
AuthUI.getInstance()
383+
.createSignInIntentBuilder()
384+
.setProviders(
385+
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
386+
googleIdp,
387+
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build())
388+
.build(),
389+
RC_SIGN_IN);
382390
```
383391

384392

385393
#### Facebook
386394

387395
By default, FirebaseUI requests the `email` and `public_profile` permissions when initiating
388-
Facebook Login. If you would like to override these scopes, a string array resource named
389-
`facebook_permissions` to your `strings.xml` file like this:
396+
Facebook Login. If you would like to request additional permissions from the user, call
397+
`setPermissions` on the `AuthUI.IdpConfig.Builder` when initializing FirebaseUI.
390398

391-
```xml
392-
<!--
393-
See:
394-
https://developers.facebook.com/docs/facebook-login/android
395-
https://developers.facebook.com/docs/facebook-login/permissions
396-
-->
397-
<string-array name="facebook_permissions">
398-
<!-- Request permission to know the user's birthday -->
399-
<item>user_birthday</item>
400-
</string-array>
399+
```java
400+
// For a list of permissions see:
401+
// https://developers.facebook.com/docs/facebook-login/android
402+
// https://developers.facebook.com/docs/facebook-login/permissions
403+
404+
AuthUI.IdpConfig facebookIdp = new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER)
405+
.setPermissions(Arrays.asList("user_friends"))
406+
.build();
407+
408+
startActivityForResult(
409+
AuthUI.getInstance()
410+
.createSignInIntentBuilder()
411+
.setProviders(
412+
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
413+
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
414+
facebookIdp)
415+
.build(),
416+
RC_SIGN_IN);
401417
```
418+
419+
#### Twitter
420+
421+
Twitter permissions can only be configured through Twitter's developer console.

auth/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,7 @@ javadoc.exclude([
6565
'com/firebase/ui/auth/ui/email/field_validators/**',
6666
'com/firebase/ui/auth/util/**',
6767
])
68+
69+
javadoc.include([
70+
'com/google/firebase/auth/**'
71+
])

0 commit comments

Comments
 (0)