Search in sources :

Example 1 with UserInfo

use of com.google.firebase.auth.UserInfo in project FirebaseUI-Android by firebase.

the class SmartLockBase method credentialsFromFirebaseUser.

/**
     * Make a list of {@link Credential} from a FirebaseUser. Useful for deleting Credentials,
     * not for saving since we don't have access to the password.
     */
public static List<Credential> credentialsFromFirebaseUser(@NonNull FirebaseUser user) {
    if (TextUtils.isEmpty(user.getEmail())) {
        Log.w(TAG, "Can't get credentials from user with no email: " + user);
        return Collections.emptyList();
    }
    List<Credential> credentials = new ArrayList<>();
    for (UserInfo userInfo : user.getProviderData()) {
        // Get provider ID from Firebase Auth
        String providerId = userInfo.getProviderId();
        // Convert to Credentials API account type
        String accountType = providerIdToAccountType(providerId);
        // Build and add credential
        Credential.Builder builder = new Credential.Builder(user.getEmail()).setAccountType(accountType);
        // to make deletion succeed.
        if (accountType == null) {
            builder.setPassword("some_password");
        }
        credentials.add(builder.build());
    }
    return credentials;
}
Also used : Credential(com.google.android.gms.auth.api.credentials.Credential) ArrayList(java.util.ArrayList) UserInfo(com.google.firebase.auth.UserInfo)

Example 2 with UserInfo

use of com.google.firebase.auth.UserInfo in project mxisd by kamax-io.

the class GoogleFirebaseAuthenticator method authenticate.

@Override
public BackendAuthResult authenticate(_MatrixID mxid, String password) {
    if (!isEnabled()) {
        throw new IllegalStateException();
    }
    log.info("Trying to authenticate {}", mxid);
    final BackendAuthResult result = BackendAuthResult.failure();
    String localpart = mxid.getLocalPart();
    CountDownLatch l = new CountDownLatch(1);
    getFirebase().verifyIdToken(password).addOnSuccessListener(token -> {
        try {
            if (!StringUtils.equals(localpart, token.getUid())) {
                log.info("Failure to authenticate {}: Matrix ID localpart '{}' does not match Firebase UID '{}'", mxid, localpart, token.getUid());
                result.fail();
                return;
            }
            result.succeed(mxid.getId(), UserIdType.MatrixID.getId(), token.getName());
            log.info("{} was successfully authenticated", mxid);
            log.info("Fetching profile for {}", mxid);
            CountDownLatch userRecordLatch = new CountDownLatch(1);
            getFirebase().getUser(token.getUid()).addOnSuccessListener(user -> {
                try {
                    toEmail(result, user.getEmail());
                    toMsisdn(result, user.getPhoneNumber());
                    for (UserInfo info : user.getProviderData()) {
                        toEmail(result, info.getEmail());
                        toMsisdn(result, info.getPhoneNumber());
                    }
                    log.info("Got {} 3PIDs in profile", result.getProfile().getThreePids().size());
                } finally {
                    userRecordLatch.countDown();
                }
            }).addOnFailureListener(e -> {
                try {
                    log.warn("Unable to fetch Firebase user profile for {}", mxid);
                    result.fail();
                } finally {
                    userRecordLatch.countDown();
                }
            });
            waitOnLatch(result, userRecordLatch, "Firebase user profile");
        } finally {
            l.countDown();
        }
    }).addOnFailureListener(e -> {
        try {
            if (e instanceof IllegalArgumentException) {
                log.info("Failure to authenticate {}: invalid firebase token", mxid);
            } else {
                log.info("Failure to authenticate {}: {}", mxid, e.getMessage(), e);
                log.info("Exception", e);
            }
            result.fail();
        } finally {
            l.countDown();
        }
    });
    waitOnLatch(result, l, "Firebase auth check");
    return result;
}
Also used : BackendAuthResult(io.kamax.mxisd.auth.provider.BackendAuthResult) ThreePidMedium(io.kamax.matrix.ThreePidMedium) StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) UserInfo(com.google.firebase.auth.UserInfo) AuthenticatorProvider(io.kamax.mxisd.auth.provider.AuthenticatorProvider) LoggerFactory(org.slf4j.LoggerFactory) io.kamax.matrix._MatrixID(io.kamax.matrix._MatrixID) BackendAuthResult(io.kamax.mxisd.auth.provider.BackendAuthResult) ThreePid(io.kamax.matrix.ThreePid) TimeUnit(java.util.concurrent.TimeUnit) PhoneNumberUtil(com.google.i18n.phonenumbers.PhoneNumberUtil) CountDownLatch(java.util.concurrent.CountDownLatch) NumberParseException(com.google.i18n.phonenumbers.NumberParseException) UserIdType(io.kamax.mxisd.UserIdType) UserInfo(com.google.firebase.auth.UserInfo) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with UserInfo

use of com.google.firebase.auth.UserInfo in project FirebaseAuth-Android by jirawatee.

the class ManageUserActivity method updateUI.

private void updateUI(FirebaseUser user) {
    if (user != null) {
        // User's profile
        mTextViewProfile.setText("Firebase ID: " + user.getUid());
        mTextViewProfile.append("\n");
        mTextViewProfile.append("DisplayName: " + user.getDisplayName());
        mTextViewProfile.append("\n");
        mTextViewProfile.append("Email: " + user.getEmail());
        mTextViewProfile.append("\n");
        mTextViewProfile.append("Photo URL: " + user.getPhotoUrl());
        // User's provider
        mTextViewProvider.setText(null);
        for (UserInfo profile : user.getProviderData()) {
            // Id of the provider (ex: google.com)
            mTextViewProvider.append("providerId: " + profile.getProviderId());
            mTextViewProvider.append("\n");
            // UID specific to the provider
            mTextViewProvider.append("UID: " + profile.getUid());
            mTextViewProvider.append("\n");
            mTextViewProvider.append("name: " + profile.getDisplayName());
            mTextViewProvider.append("\n");
            mTextViewProvider.append("email: " + profile.getEmail());
            mTextViewProvider.append("\n");
            mTextViewProvider.append("photoUrl: " + profile.getPhotoUrl());
            if (!"password".equals(profile.getProviderId())) {
                mTextViewProvider.append("\n\n");
            }
        }
        findViewById(R.id.provider_fields).setVisibility(View.VISIBLE);
        findViewById(R.id.update_profile_fields).setVisibility(View.VISIBLE);
        findViewById(R.id.update_email_fields).setVisibility(View.VISIBLE);
        findViewById(R.id.update_password_fields).setVisibility(View.VISIBLE);
        findViewById(R.id.send_password_reset_fields).setVisibility(View.VISIBLE);
        findViewById(R.id.delete_fields).setVisibility(View.VISIBLE);
    } else {
        mTextViewProfile.setText(R.string.signed_out);
        mTextViewProvider.setText(null);
        findViewById(R.id.provider_fields).setVisibility(View.GONE);
        findViewById(R.id.update_profile_fields).setVisibility(View.GONE);
        findViewById(R.id.update_email_fields).setVisibility(View.GONE);
        findViewById(R.id.update_password_fields).setVisibility(View.GONE);
        findViewById(R.id.send_password_reset_fields).setVisibility(View.GONE);
        findViewById(R.id.delete_fields).setVisibility(View.GONE);
    }
    hideProgressDialog();
}
Also used : UserInfo(com.google.firebase.auth.UserInfo)

Example 4 with UserInfo

use of com.google.firebase.auth.UserInfo in project FirebaseUI-Android by firebase.

the class SignedInActivity method populateProfile.

private void populateProfile(@Nullable IdpResponse response) {
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user.getPhotoUrl() != null) {
        GlideApp.with(this).load(user.getPhotoUrl()).fitCenter().into(mBinding.userProfilePicture);
    }
    mBinding.userEmail.setText(TextUtils.isEmpty(user.getEmail()) ? "No email" : user.getEmail());
    mBinding.userPhoneNumber.setText(TextUtils.isEmpty(user.getPhoneNumber()) ? "No phone number" : user.getPhoneNumber());
    mBinding.userDisplayName.setText(TextUtils.isEmpty(user.getDisplayName()) ? "No display name" : user.getDisplayName());
    if (response == null) {
        mBinding.userIsNew.setVisibility(View.GONE);
    } else {
        mBinding.userIsNew.setVisibility(View.VISIBLE);
        mBinding.userIsNew.setText(response.isNewUser() ? "New user" : "Existing user");
    }
    List<String> providers = new ArrayList<>();
    if (user.getProviderData().isEmpty()) {
        providers.add(getString(R.string.providers_anonymous));
    } else {
        for (UserInfo info : user.getProviderData()) {
            switch(info.getProviderId()) {
                case GoogleAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_google));
                    break;
                case FacebookAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_facebook));
                    break;
                case TwitterAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_twitter));
                    break;
                case EmailAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_email));
                    break;
                case PhoneAuthProvider.PROVIDER_ID:
                    providers.add(getString(R.string.providers_phone));
                    break;
                case EMAIL_LINK_PROVIDER:
                    providers.add(getString(R.string.providers_email_link));
                    break;
                case FirebaseAuthProvider.PROVIDER_ID:
                    // Ignore this provider, it's not very meaningful
                    break;
                default:
                    providers.add(info.getProviderId());
            }
        }
    }
    mBinding.userEnabledProviders.setText(getString(R.string.used_providers, providers));
}
Also used : ArrayList(java.util.ArrayList) UserInfo(com.google.firebase.auth.UserInfo) FirebaseUser(com.google.firebase.auth.FirebaseUser)

Aggregations

UserInfo (com.google.firebase.auth.UserInfo)4 ArrayList (java.util.ArrayList)2 Credential (com.google.android.gms.auth.api.credentials.Credential)1 FirebaseUser (com.google.firebase.auth.FirebaseUser)1 NumberParseException (com.google.i18n.phonenumbers.NumberParseException)1 PhoneNumberUtil (com.google.i18n.phonenumbers.PhoneNumberUtil)1 ThreePid (io.kamax.matrix.ThreePid)1 ThreePidMedium (io.kamax.matrix.ThreePidMedium)1 io.kamax.matrix._MatrixID (io.kamax.matrix._MatrixID)1 UserIdType (io.kamax.mxisd.UserIdType)1 AuthenticatorProvider (io.kamax.mxisd.auth.provider.AuthenticatorProvider)1 BackendAuthResult (io.kamax.mxisd.auth.provider.BackendAuthResult)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 StringUtils (org.apache.commons.lang.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1