Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4194e8ea authored by Reema Bajwa's avatar Reema Bajwa
Browse files

Use TYPE_NO_CREDENTIAL for GET & CREATE exceptions

This constant is used whenever there are no providers, no credentials
or other invalid state is reached.

Test: Built & deployed locally

Change-Id: I8175a8a0e5f6fca49151c746e20cfec85a67a50f
parent 0a86ed2d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -138,6 +138,6 @@ public final class ClearRequestSession extends RequestSession<ClearCredentialSta
            }
        }
        // TODO: Replace with properly defined error type
        respondToClientWithErrorAndFinish("unknown", "All providers failed");
        respondToClientWithErrorAndFinish("UNKNOWN", "All providers failed");
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.credentials.CreateCredentialException;
import android.credentials.CreateCredentialRequest;
import android.credentials.CreateCredentialResponse;
import android.credentials.CredentialManager;
@@ -98,8 +99,7 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
        if (response != null) {
            respondToClientWithResponseAndFinish(response);
        } else {
            // TODO("Replace with properly defined error type)
            respondToClientWithErrorAndFinish("unknown_type",
            respondToClientWithErrorAndFinish(CreateCredentialException.TYPE_NO_CREDENTIAL,
                    "Invalid response");
        }
    }
@@ -113,7 +113,7 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR
    @Override
    public void onUiCancellation() {
        // TODO("Replace with properly defined error type")
        respondToClientWithErrorAndFinish("user_cancelled",
        respondToClientWithErrorAndFinish(CreateCredentialException.TYPE_NO_CREDENTIAL,
                "User cancelled the selector");
    }

+8 −5
Original line number Diff line number Diff line
@@ -26,7 +26,9 @@ import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.credentials.ClearCredentialStateRequest;
import android.credentials.CreateCredentialException;
import android.credentials.CreateCredentialRequest;
import android.credentials.GetCredentialException;
import android.credentials.GetCredentialOption;
import android.credentials.GetCredentialRequest;
import android.credentials.IClearCredentialStateCallback;
@@ -235,8 +237,8 @@ public final class CredentialManagerService

            if (providerSessions.isEmpty()) {
                try {
                    // TODO("Replace with properly defined error type")
                    callback.onError("unknown_type", "No providers available to fulfill request.");
                    callback.onError(GetCredentialException.TYPE_NO_CREDENTIAL,
                            "No credentials available on this device.");
                } catch (RemoteException e) {
                    Log.i(
                            TAG,
@@ -284,8 +286,8 @@ public final class CredentialManagerService

            if (providerSessions.isEmpty()) {
                try {
                    // TODO("Replace with properly defined error type")
                    callback.onError("unknown_type", "No providers available to fulfill request.");
                    callback.onError(CreateCredentialException.TYPE_NO_CREDENTIAL,
                            "No credentials available on this device.");
                } catch (RemoteException e) {
                    Log.i(
                            TAG,
@@ -402,7 +404,8 @@ public final class CredentialManagerService
            if (providerSessions.isEmpty()) {
                try {
                    // TODO("Replace with properly defined error type")
                    callback.onError("unknown_type", "No providers available to fulfill request.");
                    callback.onError("UNKNOWN", "No crdentials available on this "
                            + "device");
                } catch (RemoteException e) {
                    Log.i(
                            TAG,
+7 −8
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.credentials;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.credentials.GetCredentialException;
import android.credentials.GetCredentialRequest;
import android.credentials.GetCredentialResponse;
import android.credentials.IGetCredentialCallback;
@@ -93,8 +94,7 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
        if (response != null) {
            respondToClientWithResponseAndFinish(response);
        } else {
            // TODO("Replace with no credentials/unknown type when ready)
            respondToClientWithErrorAndFinish("unknown_type",
            respondToClientWithErrorAndFinish(GetCredentialException.TYPE_NO_CREDENTIAL,
                    "Invalid response from provider");
        }
    }
@@ -108,29 +108,28 @@ public final class GetRequestSession extends RequestSession<GetCredentialRequest
    }

    private void respondToClientWithResponseAndFinish(GetCredentialResponse response) {
        Log.i(TAG, "respondToClientWithResponseAndFinish");
        try {
            mClientCallback.onResponse(response);
        } catch (RemoteException e) {
            e.printStackTrace();
            Log.i(TAG, "Issue while responding to client with a response : " + e.getMessage());
        }
        finishSession();
    }

    private void respondToClientWithErrorAndFinish(String errorType, String errorMsg) {
        Log.i(TAG, "respondToClientWithErrorAndFinish");
        try {
            mClientCallback.onError(errorType, errorMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
            Log.i(TAG, "Issue while responding to client with error : " + e.getMessage());

        }
        finishSession();
    }

    @Override
    public void onUiCancellation() {
        // TODO("Replace with properly defined error type")
        respondToClientWithErrorAndFinish("user_canceled",
        // TODO("Replace with user cancelled error type when ready")
        respondToClientWithErrorAndFinish(GetCredentialException.TYPE_NO_CREDENTIAL,
                "User cancelled the selector");
    }
}
+18 −17
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.credentials.CreateCredentialException;
import android.credentials.CreateCredentialResponse;
import android.credentials.ui.CreateCredentialProviderData;
import android.credentials.ui.Entry;
import android.credentials.ui.ProviderPendingIntentResponse;
@@ -99,7 +100,7 @@ public final class ProviderCreateSession extends ProviderSession<
    private ProviderCreateSession(
            @NonNull Context context,
            @NonNull CredentialProviderInfo info,
            @NonNull ProviderInternalCallback callbacks,
            @NonNull ProviderInternalCallback<CreateCredentialResponse> callbacks,
            @UserIdInt int userId,
            @NonNull RemoteCredentialService remoteCredentialService,
            @NonNull BeginCreateCredentialRequest beginCreateRequest,
@@ -181,9 +182,7 @@ public final class ProviderCreateSession extends ProviderSession<
                    onSaveEntrySelected(providerPendingIntentResponse);
                } else {
                    Log.i(TAG, "Unexpected save entry key");
                    // TODO("Replace with no credentials error type");
                    invokeCallbackWithError("unknown_type",
                            "Issue while retrieving credential");
                    invokeCallbackOnInternalInvalidState();
                }
                break;
            case REMOTE_ENTRY_KEY:
@@ -191,9 +190,7 @@ public final class ProviderCreateSession extends ProviderSession<
                    onRemoteEntrySelected(providerPendingIntentResponse);
                } else {
                    Log.i(TAG, "Unexpected remote entry key");
                    // TODO("Replace with unknown/no credentials exception")
                    invokeCallbackWithError("unknown_type",
                            "Issue while retrieving credential");
                    invokeCallbackOnInternalInvalidState();
                }
                break;
            default:
@@ -250,15 +247,8 @@ public final class ProviderCreateSession extends ProviderSession<
        } else {
            Log.i(TAG, "onSaveEntrySelected - no response or error found in pending "
                    + "intent response");
            invokeCallbackWithError(
                    // TODO("Replace with unknown/no credentials exception")
                    "unknown",
                    "Issue encountered while retrieving the credential");
        }
            invokeCallbackOnInternalInvalidState();
        }

    private void invokeCallbackWithError(String errorType, @Nullable String message) {
        mCallbacks.onFinalErrorReceived(mComponentName, errorType, message);
    }

    @Nullable
@@ -266,7 +256,7 @@ public final class ProviderCreateSession extends ProviderSession<
            ProviderPendingIntentResponse pendingIntentResponse) {
        if (pendingIntentResponse == null) {
            Log.i(TAG, "pendingIntentResponse is null");
            return null;
            return new CreateCredentialException(CreateCredentialException.TYPE_NO_CREDENTIAL);
        }
        if (PendingIntentResultHandler.isValidResponse(pendingIntentResponse)) {
            CreateCredentialException exception = PendingIntentResultHandler
@@ -278,8 +268,19 @@ public final class ProviderCreateSession extends ProviderSession<
        } else {
            Log.i(TAG, "Pending intent result code not Activity.RESULT_OK");
            // TODO("Update with unknown exception when ready")
            return new CreateCredentialException("unknown");
            return new CreateCredentialException(CreateCredentialException.TYPE_NO_CREDENTIAL);
        }
        return null;
    }

    /**
     * When an invalid state occurs, e.g. entry mismatch or no response from provider,
     * we send back a TYPE_NO_CREDENTIAL error as to the developer, it is the same as not
     * getting any credentials back.
     */
    private void invokeCallbackOnInternalInvalidState() {
        mCallbacks.onFinalErrorReceived(mComponentName,
                CreateCredentialException.TYPE_NO_CREDENTIAL,
                null);
    }
}
Loading