Merge "Use TYPE_NO_CREDENTIAL for GET & CREATE exceptions"

This commit is contained in:
TreeHugger Robot
2023-01-11 18:53:29 +00:00
committed by Android (Google) Code Review
7 changed files with 63 additions and 64 deletions

View File

@@ -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");
}
}

View File

@@ -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");
}

View File

@@ -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;
@@ -286,8 +288,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,
@@ -331,8 +333,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,
@@ -447,7 +449,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,

View File

@@ -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");
}
}

View File

@@ -22,6 +22,7 @@ import android.annotation.UserIdInt;
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;
@@ -98,7 +99,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,
@@ -180,9 +181,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:
@@ -190,9 +189,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:
@@ -248,23 +245,16 @@ 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
private CreateCredentialException maybeGetPendingIntentException(
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
@@ -276,8 +266,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);
}
}

View File

@@ -144,7 +144,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
public ProviderGetSession(Context context,
CredentialProviderInfo info,
ProviderInternalCallback callbacks,
ProviderInternalCallback<GetCredentialResponse> callbacks,
int userId, RemoteCredentialService remoteCredentialService,
BeginGetCredentialRequest beginGetRequest,
android.credentials.GetCredentialRequest completeGetRequest) {
@@ -195,9 +195,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
CredentialEntry credentialEntry = mUiCredentialEntries.get(entryKey);
if (credentialEntry == null) {
Log.i(TAG, "Unexpected credential entry key");
// TODO("Replace with no credentials/unknown exception")
invokeCallbackWithError("unknown_type",
"Issue while retrieving credential");
invokeCallbackOnInternalInvalidState();
return;
}
onCredentialEntrySelected(credentialEntry, providerPendingIntentResponse);
@@ -206,9 +204,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
Action actionEntry = mUiActionsEntries.get(entryKey);
if (actionEntry == null) {
Log.i(TAG, "Unexpected action entry key");
// TODO("Replace with no credentials/unknown exception")
invokeCallbackWithError("unknown_type",
"Issue while retrieving credential");
invokeCallbackOnInternalInvalidState();
return;
}
onActionEntrySelected(providerPendingIntentResponse);
@@ -218,9 +214,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
onAuthenticationEntrySelected(providerPendingIntentResponse);
} else {
Log.i(TAG, "Unexpected authentication entry key");
// TODO("Replace with no credentials/unknown exception")
invokeCallbackWithError("unknown_type",
"Issue while retrieving credential");
invokeCallbackOnInternalInvalidState();
}
break;
case REMOTE_ENTRY_KEY:
@@ -228,9 +222,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
onRemoteEntrySelected(providerPendingIntentResponse);
} else {
Log.i(TAG, "Unexpected remote entry key");
// TODO("Replace with no credentials/unknown exception")
invokeCallbackWithError("unknown_type",
"Issue while retrieving credential");
invokeCallbackOnInternalInvalidState();
}
break;
default:
@@ -238,11 +230,6 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
}
}
private void invokeCallbackWithError(String errorType, @Nullable String errorMessage) {
// TODO: Determine what the error message should be
mCallbacks.onFinalErrorReceived(mComponentName, errorType, errorMessage);
}
@Override // Call from request session to data to be shown on the UI
@Nullable protected GetCredentialProviderData prepareUiData() throws IllegalArgumentException {
Log.i(TAG, "In prepareUiData");
@@ -369,14 +356,10 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
}
Log.i(TAG, "Pending intent response contains no credential, or error");
// TODO("Replace with no credentials/unknown error when ready)
invokeCallbackWithError("unknown_type",
"Issue while retrieving credential");
invokeCallbackOnInternalInvalidState();
}
Log.i(TAG, "CredentialEntry does not have a credential or a pending intent result");
// TODO("Replace with no credentials/unknown error when ready)
invokeCallbackWithError("unknown_type",
"Error encountered while retrieving the credential");
invokeCallbackOnInternalInvalidState();
}
private void onAuthenticationEntrySelected(
@@ -401,9 +384,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
}
Log.i(TAG, "No error or respond found in pending intent response");
// TODO("Replace with no credentials/unknown error when ready)
invokeCallbackWithError("unknown type", "Issue"
+ " while retrieving credential");
invokeCallbackOnInternalInvalidState();
}
private void onActionEntrySelected(ProviderPendingIntentResponse
@@ -430,7 +411,7 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
ProviderPendingIntentResponse pendingIntentResponse) {
if (pendingIntentResponse == null) {
Log.i(TAG, "pendingIntentResponse is null");
return null;
return new GetCredentialException(GetCredentialException.TYPE_NO_CREDENTIAL);
}
if (PendingIntentResultHandler.isValidResponse(pendingIntentResponse)) {
GetCredentialException exception = PendingIntentResultHandler
@@ -441,9 +422,19 @@ public final class ProviderGetSession extends ProviderSession<BeginGetCredential
}
} else {
Log.i(TAG, "Pending intent result code not Activity.RESULT_OK");
// TODO("Update with unknown exception when ready")
return new GetCredentialException("unknown");
return new GetCredentialException(GetCredentialException.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,
GetCredentialException.TYPE_NO_CREDENTIAL,
null);
}
}

View File

@@ -191,6 +191,11 @@ public abstract class ProviderSession<T, R>
return mProviderResponse != null || mProviderResponseSet;
}
protected void invokeCallbackWithError(String errorType, @Nullable String errorMessage) {
// TODO: Determine what the error message should be
mCallbacks.onFinalErrorReceived(mComponentName, errorType, errorMessage);
}
/** Update the response state stored with the provider session. */
@Nullable protected R getProviderResponse() {
return mProviderResponse;