|
| 1 | +package com.google.firebase.quickstart.auth; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.appcompat.app.AppCompatActivity; |
| 5 | + |
| 6 | +import com.google.android.gms.tasks.OnFailureListener; |
| 7 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 8 | +import com.google.android.gms.tasks.Task; |
| 9 | +import com.google.firebase.auth.AuthResult; |
| 10 | +import com.google.firebase.auth.FirebaseAuth; |
| 11 | +import com.google.firebase.auth.FirebaseUser; |
| 12 | +import com.google.firebase.auth.OAuthProvider; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +public class GenericIdpActivity extends AppCompatActivity { |
| 18 | + |
| 19 | + private FirebaseAuth firebaseAuth; |
| 20 | + |
| 21 | + public void twitter() { |
| 22 | + // [START auth_twitter_provider_create] |
| 23 | + OAuthProvider.Builder provider = OAuthProvider.newBuilder("twitter.com"); |
| 24 | + // [END auth_twitter_provider_create] |
| 25 | + |
| 26 | + // [START auth_twitter_provider_params] |
| 27 | + // Localize to French. |
| 28 | + provider.addCustomParameter("lang", "fr"); |
| 29 | + // [END auth_twitter_provider_params] |
| 30 | + } |
| 31 | + |
| 32 | + public void github() { |
| 33 | + // [START auth_github_provider_create] |
| 34 | + OAuthProvider.Builder provider = OAuthProvider.newBuilder("github.com"); |
| 35 | + // [END auth_github_provider_create] |
| 36 | + |
| 37 | + // [START auth_github_provider_params] |
| 38 | + // Target specific email with login hint. |
| 39 | + provider. addCustomParameter( "login", "[email protected]"); |
| 40 | + // [END auth_github_provider_params] |
| 41 | + |
| 42 | + // [START auth_github_provider_scopes] |
| 43 | + // Request read access to a user's email addresses. |
| 44 | + // This must be preconfigured in the app's API permissions. |
| 45 | + List<String> scopes = |
| 46 | + new ArrayList<String>() { |
| 47 | + { |
| 48 | + add("user:email"); |
| 49 | + } |
| 50 | + }; |
| 51 | + provider.setScopes(scopes); |
| 52 | + // [END auth_github_provider_scopes] |
| 53 | + } |
| 54 | + |
| 55 | + public void microsoft() { |
| 56 | + // [START auth_microsoft_provider_create] |
| 57 | + OAuthProvider.Builder provider = OAuthProvider.newBuilder("microsoft.com"); |
| 58 | + // [END auth_microsoft_provider_create] |
| 59 | + |
| 60 | + // [START auth_microsoft_provider_params] |
| 61 | + // Target specific email with login hint. |
| 62 | + // Force re-consent. |
| 63 | + provider.addCustomParameter("prompt", "consent"); |
| 64 | + |
| 65 | + // Target specific email with login hint. |
| 66 | + provider. addCustomParameter( "login_hint", "[email protected]"); |
| 67 | + // [END auth_microsoft_provider_params] |
| 68 | + |
| 69 | + // [START auth_microsoft_provider_tenant] |
| 70 | + // Optional "tenant" parameter in case you are using an Azure AD tenant. |
| 71 | + // eg. '8eaef023-2b34-4da1-9baa-8bc8c9d6a490' or 'contoso.onmicrosoft.com' |
| 72 | + // or "common" for tenant-independent tokens. |
| 73 | + // The default value is "common". |
| 74 | + provider.addCustomParameter("tenant", "TENANT_ID"); |
| 75 | + // [END auth_microsoft_provider_tenant] |
| 76 | + |
| 77 | + // [START auth_microsoft_provider_scopes] |
| 78 | + // Request read access to a user's email addresses. |
| 79 | + // This must be preconfigured in the app's API permissions. |
| 80 | + List<String> scopes = |
| 81 | + new ArrayList<String>() { |
| 82 | + { |
| 83 | + add("mail.read"); |
| 84 | + add("calendars.read"); |
| 85 | + } |
| 86 | + }; |
| 87 | + provider.setScopes(scopes); |
| 88 | + // [END auth_microsoft_provider_scopes] |
| 89 | + } |
| 90 | + |
| 91 | + public void yahoo() { |
| 92 | + // [START auth_yahoo_provider_create] |
| 93 | + OAuthProvider.Builder provider = OAuthProvider.newBuilder("yahoo.com"); |
| 94 | + // [END auth_yahoo_provider_create] |
| 95 | + |
| 96 | + |
| 97 | + // [START auth_yahoo_provider_params] |
| 98 | + // Prompt user to re-authenticate to Yahoo. |
| 99 | + provider.addCustomParameter("prompt", "login"); |
| 100 | + |
| 101 | + // Localize to French. |
| 102 | + provider.addCustomParameter("language", "fr"); |
| 103 | + // [END auth_yahoo_provider_params] |
| 104 | + |
| 105 | + |
| 106 | + // [START auth_yahoo_provider_scopes] |
| 107 | + // Request read access to a user's email addresses. |
| 108 | + // This must be preconfigured in the app's API permissions. |
| 109 | + List<String> scopes = |
| 110 | + new ArrayList<String>() { |
| 111 | + { |
| 112 | + // Request access to Yahoo Mail API. |
| 113 | + add("mail-r"); |
| 114 | + // This must be preconfigured in the app's API permissions. |
| 115 | + add("sdct-w"); |
| 116 | + } |
| 117 | + }; |
| 118 | + provider.setScopes(scopes); |
| 119 | + // [END auth_yahoo_provider_scopes] |
| 120 | + } |
| 121 | + |
| 122 | + public void oidc() { |
| 123 | + // [START auth_oidc_provider_create] |
| 124 | + OAuthProvider.Builder providerBuilder = OAuthProvider.newBuilder("oidc.example-provider"); |
| 125 | + // [END auth_oidc_provider_create] |
| 126 | + |
| 127 | + |
| 128 | + // [START auth_oidc_provider_params] |
| 129 | + // Target specific email with login hint. |
| 130 | + providerBuilder. addCustomParameter( "login_hint", "[email protected]"); |
| 131 | + // [END auth_oidc_provider_params] |
| 132 | + |
| 133 | + |
| 134 | + // [START auth_oidc_provider_scopes] |
| 135 | + // Request read access to a user's email addresses. |
| 136 | + // This must be preconfigured in the app's API permissions. |
| 137 | + List<String> scopes = |
| 138 | + new ArrayList<String>() { |
| 139 | + { |
| 140 | + add("mail.read"); |
| 141 | + add("calendars.read"); |
| 142 | + } |
| 143 | + }; |
| 144 | + providerBuilder.setScopes(scopes); |
| 145 | + // [END auth_oidc_provider_scopes] |
| 146 | + } |
| 147 | + |
| 148 | + public void getPendingAuthResult() { |
| 149 | + // [START auth_oidc_pending_result] |
| 150 | + Task<AuthResult> pendingResultTask = firebaseAuth.getPendingAuthResult(); |
| 151 | + if (pendingResultTask != null) { |
| 152 | + // There's something already here! Finish the sign-in for your user. |
| 153 | + pendingResultTask |
| 154 | + .addOnSuccessListener( |
| 155 | + new OnSuccessListener<AuthResult>() { |
| 156 | + @Override |
| 157 | + public void onSuccess(AuthResult authResult) { |
| 158 | + // User is signed in. |
| 159 | + // IdP data available in |
| 160 | + // authResult.getAdditionalUserInfo().getProfile(). |
| 161 | + // The OAuth access token can also be retrieved: |
| 162 | + // ((OAuthCredential)authResult.getCredential()).getAccessToken(). |
| 163 | + // The OAuth secret can be retrieved by calling: |
| 164 | + // ((OAuthCredential)authResult.getCredential()).getSecret(). |
| 165 | + } |
| 166 | + }) |
| 167 | + .addOnFailureListener( |
| 168 | + new OnFailureListener() { |
| 169 | + @Override |
| 170 | + public void onFailure(@NonNull Exception e) { |
| 171 | + // Handle failure. |
| 172 | + } |
| 173 | + }); |
| 174 | + } else { |
| 175 | + // There's no pending result so you need to start the sign-in flow. |
| 176 | + // See below. |
| 177 | + } |
| 178 | + // [END auth_oidc_pending_result] |
| 179 | + } |
| 180 | + |
| 181 | + public void signInWithProvider(OAuthProvider.Builder provider) { |
| 182 | + // [START auth_oidc_provider_signin] |
| 183 | + firebaseAuth |
| 184 | + .startActivityForSignInWithProvider(/* activity= */ this, provider.build()) |
| 185 | + .addOnSuccessListener( |
| 186 | + new OnSuccessListener<AuthResult>() { |
| 187 | + @Override |
| 188 | + public void onSuccess(AuthResult authResult) { |
| 189 | + // User is signed in. |
| 190 | + // IdP data available in |
| 191 | + // authResult.getAdditionalUserInfo().getProfile(). |
| 192 | + // The OAuth access token can also be retrieved: |
| 193 | + // ((OAuthCredential)authResult.getCredential()).getAccessToken(). |
| 194 | + // The OAuth secret can be retrieved by calling: |
| 195 | + // ((OAuthCredential)authResult.getCredential()).getSecret(). |
| 196 | + } |
| 197 | + }) |
| 198 | + .addOnFailureListener( |
| 199 | + new OnFailureListener() { |
| 200 | + @Override |
| 201 | + public void onFailure(@NonNull Exception e) { |
| 202 | + // Handle failure. |
| 203 | + } |
| 204 | + }); |
| 205 | + // [END auth_oidc_provider_signin] |
| 206 | + } |
| 207 | + |
| 208 | + public void linkWithProvider(OAuthProvider.Builder provider) { |
| 209 | + // [START auth_oidc_provider_link] |
| 210 | + // The user is already signed-in. |
| 211 | + FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); |
| 212 | + |
| 213 | + firebaseUser |
| 214 | + .startActivityForLinkWithProvider(/* activity= */ this, provider.build()) |
| 215 | + .addOnSuccessListener( |
| 216 | + new OnSuccessListener<AuthResult>() { |
| 217 | + @Override |
| 218 | + public void onSuccess(AuthResult authResult) { |
| 219 | + // Provider credential is linked to the current user. |
| 220 | + // IdP data available in |
| 221 | + // authResult.getAdditionalUserInfo().getProfile(). |
| 222 | + // The OAuth access token can also be retrieved: |
| 223 | + // authResult.getCredential().getAccessToken(). |
| 224 | + // The OAuth secret can be retrieved by calling: |
| 225 | + // authResult.getCredential().getSecret(). |
| 226 | + } |
| 227 | + }) |
| 228 | + .addOnFailureListener( |
| 229 | + new OnFailureListener() { |
| 230 | + @Override |
| 231 | + public void onFailure(@NonNull Exception e) { |
| 232 | + // Handle failure. |
| 233 | + } |
| 234 | + }); |
| 235 | + // [END auth_oidc_provider_link] |
| 236 | + } |
| 237 | + |
| 238 | + public void reauthenticateWithProvider(OAuthProvider.Builder provider) { |
| 239 | + // The user is already signed-in. |
| 240 | + FirebaseUser firebaseUser = firebaseAuth.getCurrentUser(); |
| 241 | + |
| 242 | + firebaseUser |
| 243 | + .startActivityForReauthenticateWithProvider(/* activity= */ this, provider.build()) |
| 244 | + .addOnSuccessListener( |
| 245 | + new OnSuccessListener<AuthResult>() { |
| 246 | + @Override |
| 247 | + public void onSuccess(AuthResult authResult) { |
| 248 | + // User is re-authenticated with fresh tokens and |
| 249 | + // should be able to perform sensitive operations |
| 250 | + // like account deletion and email or password |
| 251 | + // update. |
| 252 | + } |
| 253 | + }) |
| 254 | + .addOnFailureListener( |
| 255 | + new OnFailureListener() { |
| 256 | + @Override |
| 257 | + public void onFailure(@NonNull Exception e) { |
| 258 | + // Handle failure. |
| 259 | + } |
| 260 | + }); |
| 261 | + } |
| 262 | +} |
0 commit comments