|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.firebase.ui.auth; |
| 16 | + |
| 17 | +import android.app.Activity; |
| 18 | +import android.content.Context; |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.support.annotation.NonNull; |
| 22 | +import android.support.annotation.Nullable; |
| 23 | +import android.support.annotation.StyleRes; |
| 24 | + |
| 25 | +import com.firebase.ui.auth.api.CredentialsApiHelper; |
| 26 | +import com.firebase.ui.auth.choreographer.idp.provider.FacebookProvider; |
| 27 | +import com.firebase.ui.auth.choreographer.idp.provider.GoogleProvider; |
| 28 | +import com.firebase.ui.auth.choreographer.idp.provider.IDPProviderParcel; |
| 29 | +import com.firebase.ui.auth.ui.credentials.ChooseAccountActivity; |
| 30 | +import com.google.android.gms.common.api.Status; |
| 31 | +import com.google.android.gms.tasks.Continuation; |
| 32 | +import com.google.android.gms.tasks.Task; |
| 33 | +import com.google.firebase.FirebaseApp; |
| 34 | +import com.google.firebase.auth.EmailAuthProvider; |
| 35 | +import com.google.firebase.auth.FirebaseAuth; |
| 36 | + |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.HashSet; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Set; |
| 43 | + |
| 44 | +/** |
| 45 | + * The entry point to the user authentication flow. |
| 46 | + * |
| 47 | + * <h1>IDP Provider configuration instructions</h1> |
| 48 | + * |
| 49 | + * <ul> |
| 50 | + * |
| 51 | + * <li>Enabling Google Sign In: If you're using the |
| 52 | + * <a href="https://developers.google.com/android/guides/google-services-plugin">Google |
| 53 | + * Services Gradle Plugin</a>, no additional configuration is required. If not, please override |
| 54 | + * {@code R.string.default_web_client_id} to provide your |
| 55 | + * <a href="https://developers.google.com/identity/sign-in/web/devconsole-project">Google OAuth |
| 56 | + * web client id.</a> |
| 57 | + * </li> |
| 58 | + * |
| 59 | + * <li>Enabling Facebook Sign In: Please override the |
| 60 | + * {@code R.string.facebook_application_id} to provide the |
| 61 | + * <a href="https://developers.facebook.com/docs/apps/register">App Id</a> from |
| 62 | + * <a href="https://developers.facebook.com/apps">Facebook Developer Dashboard</a>. |
| 63 | + * </li> |
| 64 | + * |
| 65 | + * </ul> |
| 66 | + */ |
| 67 | +public class AuthUI { |
| 68 | + |
| 69 | + /** |
| 70 | + * Provider identifier for email and password credentials, for use with |
| 71 | + * {@link SignInIntentBuilder#setProviders}. |
| 72 | + */ |
| 73 | + public static final String EMAIL_PROVIDER = "email"; |
| 74 | + |
| 75 | + /** |
| 76 | + * Provider identifier for Google, for use with {@link SignInIntentBuilder#setProviders}. |
| 77 | + */ |
| 78 | + public static final String GOOGLE_PROVIDER = "google"; |
| 79 | + |
| 80 | + /** |
| 81 | + * Provider identifier for Facebook, for use with {@link SignInIntentBuilder#setProviders}. |
| 82 | + */ |
| 83 | + public static final String FACEBOOK_PROVIDER = "facebook"; |
| 84 | + |
| 85 | + /** |
| 86 | + * The set of authentication providers supported in Firebase Auth UI. |
| 87 | + */ |
| 88 | + public static final Set<String> SUPPORTED_PROVIDERS = |
| 89 | + Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( |
| 90 | + EMAIL_PROVIDER, |
| 91 | + GOOGLE_PROVIDER, |
| 92 | + FACEBOOK_PROVIDER |
| 93 | + ))); |
| 94 | + |
| 95 | + /** |
| 96 | + * The theme identifier to use in {@link SignInIntentBuilder#setTheme(int)} if no theme |
| 97 | + * customization is required. |
| 98 | + */ |
| 99 | + public static final int DEFAULT_THEME = 0; |
| 100 | + |
| 101 | + /** |
| 102 | + * Signs the current user out, if one is signed in. The |
| 103 | + * {@link FirebaseAuth} instance associated with the default {@link FirebaseApp} instance |
| 104 | + * (as returned by {@code FirebaseApp.getInstance()}) will be used for this operation. |
| 105 | + * |
| 106 | + * @param activity The activity requesting the user be signed out. |
| 107 | + * @return a task which, upon completion, signals that the user has been signed out |
| 108 | + * ({@code result.isSuccess()}, or that the sign-out attempt failed unexpectedly |
| 109 | + * ({@code !result.isSuccess()}). |
| 110 | + */ |
| 111 | + public static Task<Void> signOut(@NonNull Activity activity) { |
| 112 | + FirebaseApp firebaseApp = FirebaseApp.getInstance(); |
| 113 | + if (firebaseApp == null) { |
| 114 | + throw new IllegalStateException("No FirebaseApp instance available"); |
| 115 | + } |
| 116 | + |
| 117 | + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); |
| 118 | + if (firebaseAuth == null) { |
| 119 | + throw new IllegalStateException( |
| 120 | + "No FirebaseAuth instance available for app" + firebaseApp.getName()); |
| 121 | + } |
| 122 | + |
| 123 | + return signOut(activity, firebaseAuth); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Signs the current user out, if one is signed in to the context of the provided |
| 128 | + * {@link FirebaseAuth} instance. |
| 129 | + * |
| 130 | + * @param activity The activity requesting the user be signed out. |
| 131 | + * @param auth The auth instance from which the current user should be signed out. |
| 132 | + * @return a task which, upon completion, signals that the user has been signed out |
| 133 | + * ({@code result.isSuccess()}, or that the sign-out attempt failed unexpectedly |
| 134 | + * ({@code !result.isSuccess()}). |
| 135 | + */ |
| 136 | + public static Task<Void> signOut(@NonNull Activity activity, @NonNull FirebaseAuth auth) { |
| 137 | + auth.signOut(); |
| 138 | + return CredentialsApiHelper.getInstance(activity).disableAutoSignIn() |
| 139 | + .continueWith(new Continuation<Status, Void>() { |
| 140 | + @Override |
| 141 | + public Void then(@NonNull Task<Status> task) throws Exception { |
| 142 | + return null; |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Builder for the intent to start the user authentication flow. |
| 149 | + */ |
| 150 | + public static final class SignInIntentBuilder { |
| 151 | + private Context mContext; |
| 152 | + private FirebaseApp mFirebaseApp; |
| 153 | + private int mTheme = DEFAULT_THEME; |
| 154 | + private List<String> mProviders = Collections.singletonList(EMAIL_PROVIDER); |
| 155 | + private String mTosUrl; |
| 156 | + |
| 157 | + /** |
| 158 | + * Starts the process of creating a sign in intent, with the mandatory application |
| 159 | + * context parameter. |
| 160 | + */ |
| 161 | + public SignInIntentBuilder(@NonNull Context context) { |
| 162 | + setContext(context); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Specifies the context to use in creating the sign-in intent. |
| 167 | + */ |
| 168 | + public @NonNull SignInIntentBuilder setContext(@NonNull Context context) { |
| 169 | + if (context == null) { |
| 170 | + throw new IllegalArgumentException("context must not be null"); |
| 171 | + } |
| 172 | + mContext = context; |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Specifies the firebase app to use and update during the authentication flow. |
| 178 | + * If {@code null} is provided, or this method is not called, the value returned by |
| 179 | + * {@link FirebaseApp#getInstance()} will be used. |
| 180 | + */ |
| 181 | + public SignInIntentBuilder setFirebaseApp(@Nullable FirebaseApp firebaseApp) { |
| 182 | + if (firebaseApp == null) { |
| 183 | + firebaseApp = getDefaultFirebaseApp(); |
| 184 | + } |
| 185 | + mFirebaseApp = firebaseApp; |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Specifies the theme to use for the application flow. If no theme is specified, |
| 191 | + * {@link #DEFAULT_THEME} will be used. |
| 192 | + */ |
| 193 | + public SignInIntentBuilder setTheme(@StyleRes int theme) { |
| 194 | + mTheme = theme; |
| 195 | + return this; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Specifies the terms-of-service URL for the application. |
| 200 | + */ |
| 201 | + public SignInIntentBuilder setTosUrl(@Nullable String tosUrl) { |
| 202 | + mTosUrl = tosUrl; |
| 203 | + return this; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Specifies the set of supported authentication providers. At least one provider |
| 208 | + * must be specified, and the set of providers must be a subset of |
| 209 | + * {@link #SUPPORTED_PROVIDERS}. |
| 210 | + * |
| 211 | + * <p>If no providers are explicitly specified by calling this method, then |
| 212 | + * {@link #EMAIL_PROVIDER email} is the default supported provider. |
| 213 | + * |
| 214 | + * @see #EMAIL_PROVIDER |
| 215 | + * @see #FACEBOOK_PROVIDER |
| 216 | + * @see #GOOGLE_PROVIDER |
| 217 | + */ |
| 218 | + public SignInIntentBuilder setProviders(@NonNull String... providers) { |
| 219 | + mProviders = Arrays.asList(providers); |
| 220 | + for (String provider : mProviders) { |
| 221 | + if (!SUPPORTED_PROVIDERS.contains(provider)) { |
| 222 | + throw new IllegalArgumentException("Unknown provider: " + provider); |
| 223 | + } |
| 224 | + } |
| 225 | + return this; |
| 226 | + } |
| 227 | + |
| 228 | + public Intent build() { |
| 229 | + if (mFirebaseApp == null) { |
| 230 | + mFirebaseApp = getDefaultFirebaseApp(); |
| 231 | + } |
| 232 | + |
| 233 | + ArrayList<IDPProviderParcel> providerParcels = new ArrayList<>(); |
| 234 | + for (String provider : mProviders) { |
| 235 | + if (provider.equalsIgnoreCase(FACEBOOK_PROVIDER)) { |
| 236 | + providerParcels.add(FacebookProvider.createFacebookParcel( |
| 237 | + mContext.getString(R.string.facebook_application_id))); |
| 238 | + } else if (provider.equalsIgnoreCase(GOOGLE_PROVIDER)) { |
| 239 | + providerParcels.add(GoogleProvider.createParcel( |
| 240 | + mContext.getString(R.string.default_web_client_id))); |
| 241 | + } else if (provider.equalsIgnoreCase(EMAIL_PROVIDER)) { |
| 242 | + providerParcels.add( |
| 243 | + new IDPProviderParcel(EmailAuthProvider.PROVIDER_ID, new Bundle()) |
| 244 | + ); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + return ChooseAccountActivity.createIntent( |
| 249 | + mContext, |
| 250 | + mFirebaseApp.getName(), |
| 251 | + mFirebaseApp.getOptions().getApiKey(), |
| 252 | + mFirebaseApp.getOptions().getApplicationId(), |
| 253 | + providerParcels, |
| 254 | + mTosUrl, |
| 255 | + mTheme); |
| 256 | + } |
| 257 | + |
| 258 | + private @NonNull FirebaseApp getDefaultFirebaseApp() { |
| 259 | + FirebaseApp app = FirebaseApp.getInstance(); |
| 260 | + if (app == null) { |
| 261 | + throw new IllegalStateException( |
| 262 | + "no FirebaseApp instance specified or available through " |
| 263 | + + "FirebaseApp.getInstance()"); |
| 264 | + } |
| 265 | + return app; |
| 266 | + } |
| 267 | + } |
| 268 | +} |
0 commit comments