Skip to content

Specify additonal scopes through code rather than config.xml #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 93 additions & 7 deletions app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

import android.widget.TextView;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.AuthUI.IdpConfig;
import com.firebase.ui.auth.ui.ResultCodes;
import com.firebase.uidemo.R;
import com.google.android.gms.common.Scopes;
import com.google.firebase.auth.FirebaseAuth;

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

@BindView(R.id.facebook_scopes_label)
TextView mFacebookScopesLabel;

@BindView(R.id.facebook_scope_friends)
CheckBox mFacebookScopeFriends;

@BindView(R.id.facebook_scope_photos)
CheckBox mFacebookScopePhotos;

@BindView(R.id.google_scopes_label)
TextView mGoogleScopesLabel;

@BindView(R.id.google_scope_drive_file)
CheckBox mGoogleScopeDriveFile;

@BindView(R.id.google_scope_games)
CheckBox mGoogleScopeGames;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -116,12 +139,30 @@ public void onCreate(Bundle savedInstanceState) {
mUseGoogleProvider.setChecked(false);
mUseGoogleProvider.setEnabled(false);
mUseGoogleProvider.setText(R.string.google_label_missing_config);
setGoogleScopesEnabled(false);
} else {
setGoogleScopesEnabled(mUseGoogleProvider.isChecked());
mUseGoogleProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
setGoogleScopesEnabled(checked);
}
});
}

if (!isFacebookConfigured()) {
mUseFacebookProvider.setChecked(false);
mUseFacebookProvider.setEnabled(false);
mUseFacebookProvider.setText(R.string.facebook_label_missing_config);
setFacebookScopesEnabled(false);
} else {
setFacebookScopesEnabled(mUseFacebookProvider.isChecked());
mUseFacebookProvider.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
setFacebookScopesEnabled(checked);
}
});
}

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


@MainThread
private void setGoogleScopesEnabled(boolean enabled) {
mGoogleScopesLabel.setEnabled(enabled);
mGoogleScopeDriveFile.setEnabled(enabled);
mGoogleScopeGames.setEnabled(enabled);
}

@MainThread
private void setFacebookScopesEnabled(boolean enabled) {
mFacebookScopesLabel.setEnabled(enabled);
mFacebookScopeFriends.setEnabled(enabled);
mFacebookScopePhotos.setEnabled(enabled);
}

@MainThread
private void handleSignInResponse(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Expand Down Expand Up @@ -207,26 +263,32 @@ private int getSelectedLogo() {
}

@MainThread
private String[] getSelectedProviders() {
ArrayList<String> selectedProviders = new ArrayList<>();
private List<IdpConfig> getSelectedProviders() {
List<IdpConfig> selectedProviders = new ArrayList<>();

if (mUseEmailProvider.isChecked()) {
selectedProviders.add(AuthUI.EMAIL_PROVIDER);
selectedProviders.add(new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build());
}

if (mUseFacebookProvider.isChecked()) {
selectedProviders.add(AuthUI.FACEBOOK_PROVIDER);
selectedProviders.add(
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER)
.setPermissions(getFacebookPermissions())
.build());
}

if (mUseGoogleProvider.isChecked()) {
selectedProviders.add(AuthUI.GOOGLE_PROVIDER);
selectedProviders.add(
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
.setPermissions(getGooglePermissions())
.build());
}

if (mUseTwitterProvider.isChecked()) {
selectedProviders.add(AuthUI.TWITTER_PROVIDER);
selectedProviders.add(new IdpConfig.Builder(AuthUI.TWITTER_PROVIDER).build());
}

return selectedProviders.toArray(new String[selectedProviders.size()]);
return selectedProviders;
}

@MainThread
Expand Down Expand Up @@ -265,6 +327,30 @@ private void showSnackbar(@StringRes int errorMessageRes) {
Snackbar.make(mRootView, errorMessageRes, Snackbar.LENGTH_LONG).show();
}

@MainThread
private List<String> getFacebookPermissions() {
List<String> result = new ArrayList<>();
if (mFacebookScopeFriends.isChecked()) {
result.add("user_friends");
}
if (mFacebookScopePhotos.isChecked()) {
result.add("user_photos");
}
return result;
}

@MainThread
private List<String> getGooglePermissions() {
List<String> result = new ArrayList<>();
if (mGoogleScopeGames.isChecked()) {
result.add(Scopes.GAMES);
}
if (mGoogleScopeDriveFile.isChecked()) {
result.add(Scopes.DRIVE_FILE);
}
return result;
}

public static Intent createIntent(Context context) {
Intent in = new Intent();
in.setClass(context, AuthUiActivity.class);
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/res/layout/auth_ui_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,52 @@
android:text="@string/no_logo_label" />
</RadioGroup>

<TextView
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:id="@+id/facebook_scopes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:text="@string/extra_facebook_scopes" />

<CheckBox
android:id="@+id/facebook_scope_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/friends" />

<CheckBox
android:id="@+id/facebook_scope_photos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/photos" />

<TextView
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:id="@+id/google_scopes_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:text="@string/extra_google_scopes" />

<CheckBox
android:id="@+id/google_scope_games"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="@string/games" />

<CheckBox
android:id="@+id/google_scope_drive_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Drive File" />

<TextView
style="@style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
<string name="other_options_header">Other Options:</string>
<string name="enable_smartlock">Enable SmartLock for Passwords</string>
<string name="rational_image_perm">This sample will read an image from local storage to upload to Firebase Storage.</string>

<string name="anonymous_auth_failed_toast">Authentication failed, uploads and downloads will not work.</string>
<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>
<string name="extra_google_scopes">Example extra Google scopes</string>
<string name="games">Games</string>
<string name="extra_facebook_scopes">Example extra Facebook scopes</string>
<string name="friends">Friends</string>
<string name="photos">Photos</string>
</resources>
72 changes: 46 additions & 26 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(
AuthUI.EMAIL_PROVIDER,
AuthUI.GOOGLE_PROVIDER,
AuthUI.FACEBOOK_PROVIDER)
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build())
.build(),
RC_SIGN_IN);
```
Expand Down Expand Up @@ -367,35 +367,55 @@ redefine a string to change it, for example:

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

```xml
<!--
For a list of all scopes, see:
https://developers.google.com/identity/protocols/googlescopes
-->
<string-array name="google_permissions">
<!-- Request permission to read the user's Google Drive files -->
<item>https://www.googleapis.com/auth/drive.readonly</item>
</string-array>

```java
// For a list of all scopes, see:
// https://developers.google.com/identity/protocols/googlescopes
AuthUI.IdpConfig googleIdp = new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER)
.setPermissions(Arrays.asList(Scopes.GAMES))
.build();

startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
googleIdp,
new IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER).build())
.build(),
RC_SIGN_IN);
```


#### Facebook

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

```xml
<!--
See:
https://developers.facebook.com/docs/facebook-login/android
https://developers.facebook.com/docs/facebook-login/permissions
-->
<string-array name="facebook_permissions">
<!-- Request permission to know the user's birthday -->
<item>user_birthday</item>
</string-array>
```java
// For a list of permissions see:
// https://developers.facebook.com/docs/facebook-login/android
// https://developers.facebook.com/docs/facebook-login/permissions

AuthUI.IdpConfig facebookIdp = new AuthUI.IdpConfig.Builder(AuthUI.FACEBOOK_PROVIDER)
.setPermissions(Arrays.asList("user_friends"))
.build();

startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setProviders(
new IdpConfig.Builder(AuthUI.EMAIL_PROVIDER).build(),
new IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build(),
facebookIdp)
.build(),
RC_SIGN_IN);
```

#### Twitter

Twitter permissions can only be configured through Twitter's developer console.
4 changes: 4 additions & 0 deletions auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ javadoc.exclude([
'com/firebase/ui/auth/ui/email/field_validators/**',
'com/firebase/ui/auth/util/**',
])

javadoc.include([
'com/google/firebase/auth/**'
])
Loading