com.google.android.gms.auth.api.credentials.CredentialRequest Java Examples

The following examples show how to use com.google.android.gms.auth.api.credentials.CredentialRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmartLock.java    From easygoogle with Apache License 2.0 6 votes vote down vote up
/**
 * Begin the process of retrieving a {@link Credential} for the device user. This can have
 * a few different results:
 *   1) If the user has auto sign-in enabled and exactly one previously saved credential,
 *      {@link SmartLockListener#onCredentialRetrieved(Credential)} will be called and
 *      you can sign the user in immediately.
 *   2) If the user has multiple saved credentials or one saved credential and has disabled
 *      auto sign-in, you will get the callback {@link SmartLockListener#onShouldShowCredentialPicker()}
 *      at which point you can choose to show the picker dialog to continue.
 *   3) If the user has no saved credentials or cancels the operation, you will receive the
 *      {@link SmartLockListener#onCredentialRetrievalFailed()} callback.
 */
public void getCredentials() {
    CredentialRequest request = buildCredentialRequest();

    Auth.CredentialsApi.request(getFragment().getGoogleApiClient(), request)
            .setResultCallback(new ResultCallback<CredentialRequestResult>() {
                @Override
                public void onResult(CredentialRequestResult result) {
                    if (result.getStatus().isSuccess()) {
                        // Single credential, auto sign-in
                        Credential credential = result.getCredential();
                        getListener().onCredentialRetrieved(credential);
                    } else if (result.getStatus().hasResolution() &&
                            result.getStatus().getStatusCode() != CommonStatusCodes.SIGN_IN_REQUIRED) {
                        // Multiple credentials or auto-sign in disabled.  If the status
                        // code is SIGN_IN_REQUIRED then it is a hint credential, which we
                        // do not want at this point.
                        getListener().onShouldShowCredentialPicker();
                    } else {
                        // Could not retrieve credentials
                        getListener().onCredentialRetrievalFailed();
                    }
                }
            });
}
 
Example #2
Source File: SmartLock.java    From easygoogle with Apache License 2.0 6 votes vote down vote up
/**
 * Show the dialog allowing the user to choose a Credential. This method shoud only be called
 * after you receive the {@link SmartLockListener#onShouldShowCredentialPicker()} callback.
 */
public void showCredentialPicker() {
    CredentialRequest request = buildCredentialRequest();
    Activity activity = getFragment().getActivity();
    int maskedCode = getFragment().maskRequestCode(RC_READ);

    Auth.CredentialsApi.request(getFragment().getGoogleApiClient(), request)
            .setResultCallback(new ResolvingResultCallbacks<CredentialRequestResult>(activity, maskedCode) {
                @Override
                public void onSuccess(CredentialRequestResult result) {
                    getListener().onCredentialRetrieved(result.getCredential());
                }

                @Override
                public void onUnresolvableFailure(Status status) {
                    Log.e(TAG, "showCredentialPicker:onUnresolvableFailure:" + status);
                }
            });
}
 
Example #3
Source File: MainActivity.java    From identity-samples with Apache License 2.0 5 votes vote down vote up
private void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
    CredentialRequest.Builder crBuilder = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true);

    if (!onlyPasswords) {
        crBuilder.setAccountTypes(IdentityProviders.GOOGLE);
    }

    showProgress();
    mCredentialsClient.request(crBuilder.build()).addOnCompleteListener(
            new OnCompleteListener<CredentialRequestResponse>() {
                @Override
                public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
                    hideProgress();

                    if (task.isSuccessful()) {
                        // Auto sign-in success
                        handleCredential(task.getResult().getCredential());
                        return;
                    }

                    Exception e = task.getException();
                    if (e instanceof ResolvableApiException && shouldResolve) {
                        // Getting credential needs to show some UI, start resolution
                        ResolvableApiException rae = (ResolvableApiException) e;
                        resolveResult(rae, RC_CREDENTIALS_READ);
                    } else {
                        Log.w(TAG, "request: not handling exception", e);
                    }
                }
            });
}
 
Example #4
Source File: SecondActivity.java    From journaldev with MIT License 5 votes vote down vote up
private void requestCredentials() {
    mCredentialRequest = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true)
            .build();

    Auth.CredentialsApi.request(mGoogleApiClient, mCredentialRequest).setResultCallback(this);
}
 
Example #5
Source File: MainActivity.java    From android-credentials with Apache License 2.0 5 votes vote down vote up
private void requestCredentials(final boolean shouldResolve, boolean onlyPasswords) {
    CredentialRequest.Builder crBuilder = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true);

    if (!onlyPasswords) {
        crBuilder.setAccountTypes(IdentityProviders.GOOGLE);
    }

    showProgress();
    mCredentialsClient.request(crBuilder.build()).addOnCompleteListener(
            new OnCompleteListener<CredentialRequestResponse>() {
                @Override
                public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
                    hideProgress();

                    if (task.isSuccessful()) {
                        // Auto sign-in success
                        handleCredential(task.getResult().getCredential());
                        return;
                    }

                    Exception e = task.getException();
                    if (e instanceof ResolvableApiException && shouldResolve) {
                        // Getting credential needs to show some UI, start resolution
                        ResolvableApiException rae = (ResolvableApiException) e;
                        resolveResult(rae, RC_CREDENTIALS_READ);
                    } else {
                        Log.w(TAG, "request: not handling exception", e);
                    }
                }
            });
}
 
Example #6
Source File: SmartLock.java    From easygoogle with Apache License 2.0 5 votes vote down vote up
private CredentialRequest buildCredentialRequest() {
    return new CredentialRequest.Builder()
            .setCredentialPickerConfig(new CredentialPickerConfig.Builder()
                .setShowAddAccountButton(false)
                .setShowCancelButton(true)
                .setForNewAccount(false)
                .build())
            .setPasswordLoginSupported(true)
            .build();
}
 
Example #7
Source File: MainActivity.java    From identity-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Request Credentials from the Credentials API.
 */
private void requestCredentials() {
    // Request all of the user's saved username/password credentials.  We are not using
    // setAccountTypes so we will not load any credentials from other Identity Providers.
    CredentialRequest request = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true)
            .setIdTokenRequested(shouldRequestIdToken())
            .build();

    showProgress();

    mCredentialsClient.request(request).addOnCompleteListener(
            new OnCompleteListener<CredentialRequestResponse>() {
                @Override
                public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
                    hideProgress();

                    if (task.isSuccessful()) {
                        // Successfully read the credential without any user interaction, this
                        // means there was only a single credential and the user has auto
                        // sign-in enabled.
                        processRetrievedCredential(task.getResult().getCredential(), false);
                        return;
                    }

                    Exception e = task.getException();
                    if (e instanceof ResolvableApiException) {
                        // This is most likely the case where the user has multiple saved
                        // credentials and needs to pick one. This requires showing UI to
                        // resolve the read request.
                        ResolvableApiException rae = (ResolvableApiException) e;
                        resolveResult(rae, RC_READ);
                        return;
                    }

                    if (e instanceof ApiException) {
                        ApiException ae = (ApiException) e;
                        if (ae.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED) {
                            // This means only a hint is available, but we are handling that
                            // elsewhere so no need to act here.
                        } else {
                            Log.w(TAG, "Unexpected status code: " + ae.getStatusCode());
                        }
                    }
                }
            });
}
 
Example #8
Source File: RxSmartLockPasswordsFragment.java    From RxSocialAuth with Apache License 2.0 4 votes vote down vote up
public void setCredentialRequest(CredentialRequest credentialRequest) {
    mCredentialRequest = credentialRequest;
}
 
Example #9
Source File: RxSmartLockPasswords.java    From RxSocialAuth with Apache License 2.0 4 votes vote down vote up
public Builder(FragmentActivity activity) {
    this.activity = activity;
    credentialRequestBuilder = new CredentialRequest.Builder();
    // at least one provider (mandatory)
    credentialRequestBuilder.setAccountTypes(IdentityProviders.GOOGLE);
}
 
Example #10
Source File: MainActivity.java    From journaldev with MIT License 4 votes vote down vote up
public void createCredentialRequest() {
    mCredentialRequest = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true)
            .setAccountTypes(IdentityProviders.GOOGLE)
            .build();
}
 
Example #11
Source File: MainActivity.java    From android-credentials with Apache License 2.0 4 votes vote down vote up
/**
 * Request Credentials from the Credentials API.
 */
private void requestCredentials() {
    // Request all of the user's saved username/password credentials.  We are not using
    // setAccountTypes so we will not load any credentials from other Identity Providers.
    CredentialRequest request = new CredentialRequest.Builder()
            .setPasswordLoginSupported(true)
            .setIdTokenRequested(shouldRequestIdToken())
            .build();

    showProgress();

    mCredentialsClient.request(request).addOnCompleteListener(
            new OnCompleteListener<CredentialRequestResponse>() {
                @Override
                public void onComplete(@NonNull Task<CredentialRequestResponse> task) {
                    hideProgress();

                    if (task.isSuccessful()) {
                        // Successfully read the credential without any user interaction, this
                        // means there was only a single credential and the user has auto
                        // sign-in enabled.
                        processRetrievedCredential(task.getResult().getCredential(), false);
                        return;
                    }

                    Exception e = task.getException();
                    if (e instanceof ResolvableApiException) {
                        // This is most likely the case where the user has multiple saved
                        // credentials and needs to pick one. This requires showing UI to
                        // resolve the read request.
                        ResolvableApiException rae = (ResolvableApiException) e;
                        resolveResult(rae, RC_READ);
                        return;
                    }

                    if (e instanceof ApiException) {
                        ApiException ae = (ApiException) e;
                        if (ae.getStatusCode() == CommonStatusCodes.SIGN_IN_REQUIRED) {
                            // This means only a hint is available, but we are handling that
                            // elsewhere so no need to act here.
                        } else {
                            Log.w(TAG, "Unexpected status code: " + ae.getStatusCode());
                        }
                    }
                }
            });
}