private void handleTwitterSession(TwitterSession session) { Utils.d("Twitter:HandleSession:" + session); AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential) .addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Utils.d("signInWithCredential:success"); } else { // If sign in fails, display a message to the user. Utils.w("signInWithCredential:failure: " + task.getException()); } // ... } }); }
public static AuthCredential getCredential(final JSONObject credential) throws Exception { final String providerId = credential.getString("provider"); AuthCredential authCredential; if (providerId.equals(EmailAuthProvider.PROVIDER_ID)) { authCredential = getEmailAuthCredential(credential); } else if (providerId.equals(FacebookAuthProvider.PROVIDER_ID)) { authCredential = getFacebookAuthCredential(credential); } else if (providerId.equals(GithubAuthProvider.PROVIDER_ID)) { authCredential = getGithubAuthCredential(credential); } else if (providerId.equals(GoogleAuthProvider.PROVIDER_ID)) { authCredential = getGoogleAuthCredential(credential); } else if (providerId.equals(TwitterAuthProvider.PROVIDER_ID)) { authCredential = getTwitterAuthCredential(credential); } else { throw new Exception("Unknown provider ID: " + providerId); } return authCredential; }
private void handleTwitterSession(TwitterSession session) { Log.d(TAG, "handleTwitterSession:" + session); AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // ... } }); }
@Override protected Void doInBackground(Void... voids) { final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if(user != null) { for(UserInfo info : user.getProviderData()) { switch(info.getProviderId()) { case GoogleAuthProvider.PROVIDER_ID: logoutGoogle(); break; case FacebookAuthProvider.PROVIDER_ID: logoutFacebook(); break; case TwitterAuthProvider.PROVIDER_ID: logoutTwitter(); break; } } FirebaseAuth.getInstance().signOut(); } return null; }
@ReactMethod public void twitterLogin(String Token, String Secret, final Callback callback) { mAuth = FirebaseAuth.getInstance(); AuthCredential credential = TwitterAuthProvider.getCredential(Token, Secret); mAuth.signInWithCredential(credential) .addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { FirestackAuthModule.this.user = task.getResult().getUser(); userCallback(FirestackAuthModule.this.user, callback); }else{ // userErrorCallback(task, callback); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception ex) { userExceptionCallback(ex, callback); } }); }
void handleTwitterSession(TwitterSession session) { Timber.d("handleTwitterSession:" + session); AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential) .addOnCompleteListener((Activity) mContext, task -> { Timber.d("signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Timber.w("signInWithTwitterCredential", task.getException()); mEventBus.post(new LoginEvent(false, "Twitter Auth Failed : " + task.getException().getMessage())); } else { Timber.w("signInWithTwitterCredential", "success with email " + task.getResult().getUser().getEmail()); mEventBus.post(new LoginEvent(true, "Twitter Auth Success")); } }); }
public IdpResponse build() { String providerId = mUser.getProviderId(); if (!AuthUI.SUPPORTED_PROVIDERS.contains(providerId)) { throw new IllegalStateException("Unknown provider: " + providerId); } if (AuthUI.SOCIAL_PROVIDERS.contains(providerId) && TextUtils.isEmpty(mToken)) { throw new IllegalStateException( "Token cannot be null when using a non-email provider."); } if (providerId.equals(TwitterAuthProvider.PROVIDER_ID) && TextUtils.isEmpty(mSecret)) { throw new IllegalStateException( "Secret cannot be null when using the Twitter provider."); } return new IdpResponse(mUser, mToken, mSecret); }
/** * Translate a Firebase Auth provider ID (such as {@link GoogleAuthProvider#PROVIDER_ID}) to a * Credentials API account type (such as {@link IdentityProviders#GOOGLE}). */ public static String providerIdToAccountType(@AuthUI.SupportedProvider @NonNull String providerId) { switch (providerId) { case GoogleAuthProvider.PROVIDER_ID: return IdentityProviders.GOOGLE; case FacebookAuthProvider.PROVIDER_ID: return IdentityProviders.FACEBOOK; case TwitterAuthProvider.PROVIDER_ID: return IdentityProviders.TWITTER; case PhoneAuthProvider.PROVIDER_ID: return PHONE_IDENTITY; // The account type for email/password creds is null case EmailAuthProvider.PROVIDER_ID: default: return null; } }
@Test @Config(shadows = {AuthHelperShadow.class}) public void testTwitterLoginFlowStarts() { List<String> providers = Arrays.asList(AuthUI.TWITTER_PROVIDER); AuthMethodPickerActivity authMethodPickerActivity = createActivity(providers); when(AuthHelperShadow.getCurrentUser().getProviders()) .thenReturn(Arrays.asList(TwitterAuthProvider.PROVIDER_ID)); when(AuthHelperShadow.getFirebaseAuth().signInWithCredential(any(AuthCredential.class))) .thenReturn(new AutoCompleteTask<>(FakeAuthResult.INSTANCE, true, null)); Button twitterButton = authMethodPickerActivity.findViewById(R.id.twitter_button); assertNotNull(twitterButton); twitterButton.performClick(); ShadowActivity.IntentForResult nextIntent = Shadows.shadowOf(authMethodPickerActivity).getNextStartedActivityForResult(); assertTrue(nextIntent.intent.getComponent().getClassName().contains("com.twitter.sdk")); }
private void handleTwitterSession(TwitterSession session) { AuthCredential credential = TwitterAuthProvider.getCredential(session.getAuthToken().token, session.getAuthToken().secret); if (mAuth.getCurrentUser() == null) { mAuth.signInWithCredential(credential) .addOnCompleteListener(LoginActivity.this, new LoginCallback(LoginActivity.this, false, "connect_to_twitter")); } else { mAuth.getCurrentUser() .linkWithCredential(credential) .addOnCompleteListener(LoginActivity.this, new LoginCallback(LoginActivity.this, true, "connect_to_twitter")); } }
private void handleTwitterSession(TwitterSession session) { AuthCredential credential = TwitterAuthProvider.getCredential(session.getAuthToken().token, session.getAuthToken().secret); if (mAuth.getCurrentUser() == null) { mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new LoginCallback(this, false, "connect_to_twitter")); } else { mAuth.getCurrentUser() .linkWithCredential(credential) .addOnCompleteListener(this, new LoginCallback(this, true, "connect_to_twitter")); } }
private void handleTwitterSession(TwitterSession session) { Log.d(TAG, "handleTwitterSession:" + session); // [START_EXCLUDE silent] showProgressDialog(); // [END_EXCLUDE] AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success"); FirebaseUser user = mAuth.getCurrentUser(); updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.getException()); Toast.makeText(TwitterLoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); updateUI(null); } // [START_EXCLUDE] hideProgressDialog(); // [END_EXCLUDE] } }); }
private void handleTwitterSession(TwitterSession session) { Log.d(TAG, "handleTwitterSession:" + session); // [START_EXCLUDE silent] showProgressDialog(); // [END_EXCLUDE] AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w(TAG, "signInWithCredential", task.getException()); Toast.makeText(TwitterLoginActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } // [START_EXCLUDE] hideProgressDialog(); // [END_EXCLUDE] } }); }
private void signInTwitter(String token, String secret) { AuthCredential credential = TwitterAuthProvider.getCredential(token, secret); firebaseAuth.signInWithCredential(credential).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d("TAG", "signInWithCredential:onComplete:" + task.isSuccessful()); // If sign in fails, display a message to the user. If sign in succeeds // the auth state listener will be notified and logic to handle the // signed in user can be handled in the listener. if (!task.isSuccessful()) { Log.w("TAG", "signInWithCredential", task.getException()); Toast.makeText(getActivity(), "Authentication failed.", Toast.LENGTH_SHORT).show(); getActivity().finish(); } else { user = task.getResult().getUser(); registerUserInDatabase(); if (listener != null) { Bundle bundle = new Bundle(); listener.onFragmentInteraction(Constants.SIGN_UP_FRAGMENT, bundle); } else { getActivity().finish(); } } } }); }
/** * Authenticate with Firebase using a Twitter token. * * @param session The TwitterSession */ private void firebaseAuthWithTwitter(@NonNull TwitterSession session) { final AuthCredential credential = TwitterAuthProvider.getCredential( session.getAuthToken().token, session.getAuthToken().secret); mAuth.signInWithCredential(credential); }
@ReactMethod public void reauthenticateWithCredentialForProvider(final String provider, final String authToken, final String authSecret, final Callback callback) { AuthCredential credential; if (provider.equals("facebook")) { credential = FacebookAuthProvider.getCredential(authToken); } else if (provider.equals("google")) { credential = GoogleAuthProvider.getCredential(authToken, null); } else if (provider.equals("twitter")) { credential = TwitterAuthProvider.getCredential(authToken, authSecret); } else { // TODO: FirestackUtils.todoNote(TAG, "reauthenticateWithCredentialForProvider", callback); // AuthCredential credential; // Log.d(TAG, "reauthenticateWithCredentialForProvider called with: " + provider); return; } FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null) { user.reauthenticate(credential) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "User re-authenticated with " + provider); FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser(); userCallback(u, callback); } else { // userErrorCallback(task, callback); } } }); } else { WritableMap err = Arguments.createMap(); err.putInt("errorCode", NO_CURRENT_USER); err.putString("errorMessage", "No current user"); callback.invoke(err); } }
private IdpResponse createIdpResponse(TwitterSession session, String email, String name, Uri photoUri) { return new IdpResponse.Builder( new com.firebase.ui.auth.data.model.User.Builder(TwitterAuthProvider.PROVIDER_ID, email) .setName(name) .setPhotoUri(photoUri) .build()) .setToken(session.getAuthToken().token) .setSecret(session.getAuthToken().secret) .build(); }
@Nullable public static AuthCredential getAuthCredential(IdpResponse response) { switch (response.getProviderType()) { case GoogleAuthProvider.PROVIDER_ID: return GoogleAuthProvider.getCredential(response.getIdpToken(), null); case FacebookAuthProvider.PROVIDER_ID: return FacebookAuthProvider.getCredential(response.getIdpToken()); case TwitterAuthProvider.PROVIDER_ID: return TwitterAuthProvider.getCredential(response.getIdpToken(), response.getIdpSecret()); default: return null; } }
@AuthUI.SupportedProvider public static String accountTypeToProviderId(@NonNull String accountType) { switch (accountType) { case IdentityProviders.GOOGLE: return GoogleAuthProvider.PROVIDER_ID; case IdentityProviders.FACEBOOK: return FacebookAuthProvider.PROVIDER_ID; case IdentityProviders.TWITTER: return TwitterAuthProvider.PROVIDER_ID; case PHONE_IDENTITY: return PhoneAuthProvider.PROVIDER_ID; default: return null; } }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSaveSmartLock = getAuthHelper().getSaveSmartLockInstance(mActivity); User user = User.getUser(getArguments()); String provider = user.getProviderId(); AuthUI.IdpConfig providerConfig = null; for (AuthUI.IdpConfig config : getFlowParams().providerInfo) { if (config.getProviderId().equalsIgnoreCase(provider)) { providerConfig = config; break; } } if (providerConfig == null) { // we don't have a provider to handle this finish(Activity.RESULT_CANCELED, IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR)); return; } if (provider.equalsIgnoreCase(GoogleAuthProvider.PROVIDER_ID)) { mIdpProvider = new GoogleProvider( getActivity(), providerConfig, user.getEmail()); } else if (provider.equalsIgnoreCase(FacebookAuthProvider.PROVIDER_ID)) { mIdpProvider = new FacebookProvider(providerConfig, getFlowParams().themeId); } else if (provider.equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)) { mIdpProvider = new TwitterProvider(getContext()); } mIdpProvider.setAuthenticationCallback(this); if (savedInstanceState == null) { mIdpProvider.startLogin(getActivity()); } }
private List<String> getSupportedAccountTypes() { List<String> accounts = new ArrayList<>(); for (AuthUI.IdpConfig idpConfig : getFlowParams().providerInfo) { @AuthUI.SupportedProvider String providerId = idpConfig.getProviderId(); if (providerId.equals(GoogleAuthProvider.PROVIDER_ID) || providerId.equals(FacebookAuthProvider.PROVIDER_ID) || providerId.equals(TwitterAuthProvider.PROVIDER_ID)) { accounts.add(ProviderUtils.providerIdToAccountType(providerId)); } } return accounts; }
private static AuthCredential getTwitterAuthCredential(final JSONObject credential) throws Exception { final String token = credential.getString("token"); final String secret = credential.getString("secret"); return TwitterAuthProvider.getCredential(token, secret); }
@MainThread private void populateProfile() { FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user.getPhotoUrl() != null) { GlideApp.with(this) .load(user.getPhotoUrl()) .fitCenter() .into(mUserProfilePicture); } mUserEmail.setText( TextUtils.isEmpty(user.getEmail()) ? "No email" : user.getEmail()); mUserPhoneNumber.setText( TextUtils.isEmpty(user.getPhoneNumber()) ? "No phone number" : user.getPhoneNumber()); mUserDisplayName.setText( TextUtils.isEmpty(user.getDisplayName()) ? "No display name" : user.getDisplayName()); StringBuilder providerList = new StringBuilder(100); providerList.append("Providers used: "); if (user.getProviders() == null || user.getProviders().isEmpty()) { providerList.append("none"); } else { Iterator<String> providerIter = user.getProviders().iterator(); while (providerIter.hasNext()) { String provider = providerIter.next(); switch (provider) { case GoogleAuthProvider.PROVIDER_ID: providerList.append("Google"); break; case FacebookAuthProvider.PROVIDER_ID: providerList.append("Facebook"); break; case TwitterAuthProvider.PROVIDER_ID: providerList.append("Twitter"); break; case EmailAuthProvider.PROVIDER_ID: providerList.append("Email"); break; case PhoneAuthProvider.PROVIDER_ID: providerList.append("Phone"); break; default: throw new IllegalStateException("Unknown provider: " + provider); } if (providerIter.hasNext()) { providerList.append(", "); } } } mEnabledProviders.setText(providerList); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fui_welcome_back_idp_prompt_layout); IdpResponse newUserResponse = IdpResponse.fromResultIntent(getIntent()); if (newUserResponse != null) { mPrevCredential = ProviderUtils.getAuthCredential(newUserResponse); } User oldUser = User.getUser(getIntent()); String providerId = oldUser.getProviderId(); for (IdpConfig idpConfig : getFlowParams().providerInfo) { if (providerId.equals(idpConfig.getProviderId())) { switch (providerId) { case GoogleAuthProvider.PROVIDER_ID: mIdpProvider = new GoogleProvider(this, idpConfig, oldUser.getEmail()); break; case FacebookAuthProvider.PROVIDER_ID: mIdpProvider = new FacebookProvider( idpConfig, getFlowParams().themeId); break; case TwitterAuthProvider.PROVIDER_ID: mIdpProvider = new TwitterProvider(this); break; default: Log.w(TAG, "Unknown provider: " + providerId); finish(RESULT_CANCELED, IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR)); return; } } } if (mIdpProvider == null) { Log.w(TAG, "Firebase login unsuccessful." + " Account linking failed due to provider not enabled by application: " + providerId); finish(RESULT_CANCELED, IdpResponse.getErrorCodeIntent(ErrorCodes.UNKNOWN_ERROR)); return; } ((TextView) findViewById(R.id.welcome_back_idp_prompt)) .setText(getIdpPromptString(oldUser.getEmail())); mIdpProvider.setAuthenticationCallback(this); findViewById(R.id.welcome_back_idp_button).setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { getDialogHolder().showLoadingDialog(R.string.fui_progress_dialog_signing_in); mIdpProvider.startLogin(WelcomeBackIdpPrompt.this); } }); }
public static AuthCredential createAuthCredential(IdpResponse response) { if (!response.getProviderType().equalsIgnoreCase(TwitterAuthProvider.PROVIDER_ID)) { return null; } return TwitterAuthProvider.getCredential(response.getIdpToken(), response.getIdpSecret()); }
/** * Method get the Rx function to generate credential form FireBase * * @param token,secret Twitter credentials to generate the credential to fireBase * @return Observable<User> */ @Override public Observable<User> loginTwitter(String token, String secret) { AuthCredential credential = TwitterAuthProvider.getCredential(token, secret); return loginWithCredentials(credential); }