From 5f6159d32de7ef213c41ada37048c2970ca44bf6 Mon Sep 17 00:00:00 2001 From: Reema Bajwa Date: Mon, 19 Dec 2022 06:01:24 +0000 Subject: [PATCH] Change provider side GetCredRequest to contain single option Test: Built locally Bug: 253155222 API-Coverage-Bug: 247549381 Change-Id: Iab0438205081119c7d9244a10f4efc3239bec0f0 --- core/api/current.txt | 10 +- .../credentials/GetCredentialRequest.java | 100 +++--------------- .../credentials/ProviderGetSession.java | 85 +++++++++------ 3 files changed, 69 insertions(+), 126 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index 7113150994cd3..1b0c5c20d2e30 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -39865,20 +39865,14 @@ package android.service.credentials { } public final class GetCredentialRequest implements android.os.Parcelable { + ctor public GetCredentialRequest(@NonNull android.service.credentials.CallingAppInfo, @NonNull android.credentials.GetCredentialOption); method public int describeContents(); method @NonNull public android.service.credentials.CallingAppInfo getCallingAppInfo(); - method @NonNull public java.util.List getGetCredentialOptions(); + method @NonNull public android.credentials.GetCredentialOption getGetCredentialOption(); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; } - public static final class GetCredentialRequest.Builder { - ctor public GetCredentialRequest.Builder(@NonNull android.service.credentials.CallingAppInfo); - method @NonNull public android.service.credentials.GetCredentialRequest.Builder addGetCredentialOption(@NonNull android.credentials.GetCredentialOption); - method @NonNull public android.service.credentials.GetCredentialRequest build(); - method @NonNull public android.service.credentials.GetCredentialRequest.Builder setGetCredentialOptions(@NonNull java.util.List); - } - } package android.service.dreams { diff --git a/core/java/android/service/credentials/GetCredentialRequest.java b/core/java/android/service/credentials/GetCredentialRequest.java index 5532b55d09ad2..4b946f016197e 100644 --- a/core/java/android/service/credentials/GetCredentialRequest.java +++ b/core/java/android/service/credentials/GetCredentialRequest.java @@ -22,37 +22,34 @@ import android.os.Parcel; import android.os.Parcelable; import com.android.internal.util.AnnotationValidations; -import com.android.internal.util.Preconditions; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; /** - * Request for getting user's credentials from a given credential provider. + * Request for getting user's credential from a given credential provider. + * + *

Provider will receive this request once the user selects a given {@link CredentialEntry} + * on the selector, that was sourced from provider's result to + * {@link CredentialProviderService#onBeginGetCredential}. */ public final class GetCredentialRequest implements Parcelable { /** Calling package of the app requesting for credentials. */ private final @NonNull CallingAppInfo mCallingAppInfo; /** - * List of credential options. Each {@link GetCredentialOption} object holds parameters to - * be used for retrieving specific type of credentials. + * Holds parameters to be used for retrieving a specific type of credential. */ - private final @NonNull List mGetCredentialOptions; + private final @NonNull GetCredentialOption mGetCredentialOption; - private GetCredentialRequest(@NonNull CallingAppInfo callingAppInfo, - @NonNull List getCredentialOptions) { + public GetCredentialRequest(@NonNull CallingAppInfo callingAppInfo, + @NonNull GetCredentialOption getCredentialOption) { this.mCallingAppInfo = callingAppInfo; - this.mGetCredentialOptions = getCredentialOptions; + this.mGetCredentialOption = getCredentialOption; } private GetCredentialRequest(@NonNull Parcel in) { mCallingAppInfo = in.readTypedObject(CallingAppInfo.CREATOR); - List getCredentialOptions = new ArrayList<>(); - in.readTypedList(getCredentialOptions, GetCredentialOption.CREATOR); - mGetCredentialOptions = getCredentialOptions; - AnnotationValidations.validate(NonNull.class, null, mGetCredentialOptions); + AnnotationValidations.validate(NonNull.class, null, mCallingAppInfo); + mGetCredentialOption = in.readTypedObject(GetCredentialOption.CREATOR); + AnnotationValidations.validate(NonNull.class, null, mGetCredentialOption); } public static final @NonNull Creator CREATOR = @@ -76,7 +73,7 @@ public final class GetCredentialRequest implements Parcelable { @Override public void writeToParcel(@NonNull Parcel dest, int flags) { dest.writeTypedObject(mCallingAppInfo, flags); - dest.writeTypedList(mGetCredentialOptions); + dest.writeTypedObject(mGetCredentialOption, flags); } /** @@ -87,72 +84,9 @@ public final class GetCredentialRequest implements Parcelable { } /** - * Returns the list of type specific credential options to return credentials for. + * Returns the parameters needed to return a given type of credential. */ - public @NonNull List getGetCredentialOptions() { - return mGetCredentialOptions; - } - - /** - * Builder for {@link GetCredentialRequest}. - */ - public static final class Builder { - private CallingAppInfo mCallingAppInfo; - private List mGetCredentialOptions = new ArrayList<>(); - - /** - * Creates a new builder. - * @param callingAppInfo info pertaining to the app requesting credentials - * - * @throws IllegalArgumentException If {@code callingPackag}e is null or empty. - */ - public Builder(@NonNull CallingAppInfo callingAppInfo) { - mCallingAppInfo = Objects.requireNonNull(callingAppInfo); - } - - /** - * Sets the list of credential options. - * - * @throws NullPointerException If {@code getCredentialOptions} itself or any of its - * elements is null. - * @throws IllegalArgumentException If {@code getCredentialOptions} is empty. - */ - public @NonNull Builder setGetCredentialOptions( - @NonNull List getCredentialOptions) { - Preconditions.checkCollectionNotEmpty(getCredentialOptions, - "getCredentialOptions"); - Preconditions.checkCollectionElementsNotNull(getCredentialOptions, - "getCredentialOptions"); - mGetCredentialOptions = getCredentialOptions; - return this; - } - - /** - * Adds a single {@link GetCredentialOption} object to the list of credential options. - * - * @throws NullPointerException If {@code getCredentialOption} is null. - */ - public @NonNull Builder addGetCredentialOption( - @NonNull GetCredentialOption getCredentialOption) { - Objects.requireNonNull(getCredentialOption, - "getCredentialOption must not be null"); - mGetCredentialOptions.add(getCredentialOption); - return this; - } - - /** - * Builds a new {@link GetCredentialRequest} instance. - * - * @throws NullPointerException If {@code getCredentialOptions} is null. - * @throws IllegalArgumentException If {@code getCredentialOptions} is empty, or if - * {@code callingAppInfo} is null or empty. - */ - public @NonNull GetCredentialRequest build() { - Objects.requireNonNull(mCallingAppInfo, - "mCallingAppInfo"); - Preconditions.checkCollectionNotEmpty(mGetCredentialOptions, - "getCredentialOptions"); - return new GetCredentialRequest(mCallingAppInfo, mGetCredentialOptions); - } + public @NonNull GetCredentialOption getGetCredentialOption() { + return mGetCredentialOption; } } diff --git a/services/credentials/java/com/android/server/credentials/ProviderGetSession.java b/services/credentials/java/com/android/server/credentials/ProviderGetSession.java index 42f872d2659ce..4a614a69f58a6 100644 --- a/services/credentials/java/com/android/server/credentials/ProviderGetSession.java +++ b/services/credentials/java/com/android/server/credentials/ProviderGetSession.java @@ -22,6 +22,7 @@ import android.annotation.UserIdInt; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.content.pm.Signature; import android.credentials.GetCredentialException; import android.credentials.GetCredentialOption; import android.credentials.GetCredentialResponse; @@ -78,7 +79,8 @@ public final class ProviderGetSession extends ProviderSession mUiAuthenticationAction = null; /** The complete request to be used in the second round. */ - private final GetCredentialRequest mCompleteRequest; + private final android.credentials.GetCredentialRequest mCompleteRequest; + private final CallingAppInfo mCallingAppInfo; private GetCredentialException mProviderException; @@ -89,36 +91,42 @@ public final class ProviderGetSession extends ProviderSession { - //TODO : Replace with option.getCandidateQueryData - // when ready - return new BeginGetCredentialOption( - option.getType(), - option.getCandidateQueryData()); - }).collect(Collectors.toList())) - .build(); + android.credentials.GetCredentialRequest filteredRequest = + filterOptions(providerInfo.getCapabilities(), + getRequestSession.mClientRequest); + if (filteredRequest != null) { + BeginGetCredentialRequest beginGetCredentialRequest = constructQueryPhaseRequest( + filteredRequest, getRequestSession.mClientCallingPackage); + return new ProviderGetSession(context, providerInfo, getRequestSession, userId, - remoteCredentialService, beginGetCredentialRequest, completeRequest); + remoteCredentialService, beginGetCredentialRequest, filteredRequest); } Log.i(TAG, "Unable to create provider session"); return null; } + private static BeginGetCredentialRequest constructQueryPhaseRequest( + android.credentials.GetCredentialRequest filteredRequest, + String clientCallingPackage + ) { + return new BeginGetCredentialRequest.Builder( + new CallingAppInfo(clientCallingPackage, + new ArraySet())) + .setBeginGetCredentialOptions( + filteredRequest.getGetCredentialOptions().stream().map( + option -> { + return new BeginGetCredentialOption( + option.getType(), + option.getCandidateQueryData()); + }).collect(Collectors.toList())) + .build(); + } + @Nullable - private static GetCredentialRequest createProviderRequest(List providerCapabilities, - android.credentials.GetCredentialRequest clientRequest, - String clientCallingPackage) { + private static android.credentials.GetCredentialRequest filterOptions( + List providerCapabilities, + android.credentials.GetCredentialRequest clientRequest + ) { List filteredOptions = new ArrayList<>(); for (GetCredentialOption option : clientRequest.getGetCredentialOptions()) { if (providerCapabilities.contains(option.getType())) { @@ -131,10 +139,10 @@ public final class ProviderGetSession extends ProviderSession())).setGetCredentialOptions( - filteredOptions).build(); + return new android.credentials.GetCredentialRequest + .Builder(clientRequest.getData()) + .setGetCredentialOptions( + filteredOptions).build(); } Log.i(TAG, "In createProviderRequest - returning null"); return null; @@ -145,9 +153,10 @@ public final class ProviderGetSession extends ProviderSession