1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.service.credentials;
18 
19 import android.annotation.NonNull;
20 import android.credentials.CredentialOption;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import com.android.internal.util.AnnotationValidations;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Objects;
29 
30 /**
31  * Request for getting user's credential from a given credential provider.
32  *
33  * <p>A credential provider will receive this request once the user selects a
34  * given {@link CredentialEntry}, or {@link RemoteEntry} on the selector, that was sourced
35  * from provider's initial response to {@link CredentialProviderService#onBeginGetCredential}.
36  */
37 public final class GetCredentialRequest implements Parcelable {
38     /** Calling package of the app requesting for credentials. */
39     @NonNull
40     private final CallingAppInfo mCallingAppInfo;
41 
42     /**
43      * Holds a list of options (parameters) to be used for retrieving a specific type of credential.
44      */
45     @NonNull
46     private final List<CredentialOption> mCredentialOptions;
47 
GetCredentialRequest(@onNull CallingAppInfo callingAppInfo, @NonNull List<CredentialOption> credentialOptions)48     public GetCredentialRequest(@NonNull CallingAppInfo callingAppInfo,
49             @NonNull List<CredentialOption> credentialOptions) {
50         this.mCallingAppInfo = Objects.requireNonNull(callingAppInfo,
51                 "callingAppInfo must not be null");
52         this.mCredentialOptions = Objects.requireNonNull(credentialOptions,
53                 "credentialOptions must not be null");
54     }
55 
GetCredentialRequest(@onNull Parcel in)56     private GetCredentialRequest(@NonNull Parcel in) {
57         mCallingAppInfo = in.readTypedObject(CallingAppInfo.CREATOR);
58         AnnotationValidations.validate(NonNull.class, null, mCallingAppInfo);
59 
60         List<CredentialOption> credentialOptions = new ArrayList<>();
61         in.readTypedList(credentialOptions, CredentialOption.CREATOR);
62         mCredentialOptions = credentialOptions;
63         AnnotationValidations.validate(NonNull.class, null, mCredentialOptions);
64     }
65 
66     @NonNull public static final  Creator<GetCredentialRequest> CREATOR =
67             new Creator<>() {
68                 @Override
69                 public GetCredentialRequest createFromParcel(Parcel in) {
70                     return new GetCredentialRequest(in);
71                 }
72 
73                 @Override
74                 public GetCredentialRequest[] newArray(int size) {
75                     return new GetCredentialRequest[size];
76                 }
77             };
78 
79     @Override
describeContents()80     public int describeContents() {
81         return 0;
82     }
83 
84     @Override
writeToParcel(@onNull Parcel dest, int flags)85     public void writeToParcel(@NonNull Parcel dest, int flags) {
86         dest.writeTypedObject(mCallingAppInfo, flags);
87         dest.writeTypedList(mCredentialOptions, flags);
88     }
89 
90     /**
91      * Returns info pertaining to the app requesting credentials.
92      */
93     @NonNull
getCallingAppInfo()94     public CallingAppInfo getCallingAppInfo() {
95         return mCallingAppInfo;
96     }
97 
98     /**
99      * Returns a list of options containing parameters needed to return a given type of credential.
100      * This is part of the request that the credential provider receives after the user has
101      * selected an entry on a selector UI.
102      *
103      * When the user selects a {@link CredentialEntry} and the credential provider receives a
104      * {@link GetCredentialRequest}, this list is expected to contain a single
105      * {@link CredentialOption} only. A {@link CredentialEntry} is always created for a given
106      * {@link BeginGetCredentialOption}, and hence when the user selects it, the provider
107      * receives a corresponding {@link CredentialOption} that contains all the required parameters
108      * to actually retrieve the credential.
109      *
110      * When the user selects a {@link RemoteEntry} and the credential provider receives a
111      * {@link GetCredentialRequest}, this list may contain greater than a single
112      * {@link CredentialOption}, representing the number of options specified by the developer
113      * in the original {@link android.credentials.GetCredentialRequest}. This is because a
114      * {@link RemoteEntry} indicates that the entire request will be processed on a different
115      * device and is not tied to a particular option.
116      */
117     @NonNull
getCredentialOptions()118     public List<CredentialOption> getCredentialOptions() {
119         return mCredentialOptions;
120     }
121 }
122