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.annotation.Nullable;
21 import android.content.pm.SigningInfo;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import com.android.internal.util.Preconditions;
26 
27 import java.util.Objects;
28 
29 /**
30  * Information pertaining to the calling application, including the package name and a list of
31  * app signatures.
32  */
33 public final class CallingAppInfo implements Parcelable {
34     @NonNull private final String mPackageName;
35     @NonNull private final SigningInfo mSigningInfo;
36     @Nullable
37     private final String mOrigin;
38 
39     /**
40      * Constructs a new instance.
41      *
42      * @throws IllegalArgumentException If {@code packageName} is null or empty.
43      * @throws NullPointerException If {@code signingInfo} is null.
44      */
CallingAppInfo(@onNull String packageName, @NonNull SigningInfo signingInfo)45     public CallingAppInfo(@NonNull String packageName,
46             @NonNull SigningInfo signingInfo) {
47         this(packageName, signingInfo, /*origin=*/ null);
48     }
49 
50     /**
51      * Constructs a new instance.
52      *
53      * @param packageName - the package name of the calling app
54      * @param signingInfo - the signing info on the calling app
55      * @param origin - the origin that the calling app wants to use when making request on behalf of
56      *               other
57      * @throws IllegalArgumentException If {@code packageName} is null or empty.
58      * @throws NullPointerException If {@code signingInfo} is null.
59      */
CallingAppInfo(@onNull String packageName, @NonNull SigningInfo signingInfo, @Nullable String origin)60     public CallingAppInfo(@NonNull String packageName,
61             @NonNull SigningInfo signingInfo, @Nullable String origin) {
62         mPackageName = Preconditions.checkStringNotEmpty(packageName, "package name"
63                 + "must not be null or empty");
64         mSigningInfo = Objects.requireNonNull(signingInfo);
65         mOrigin = origin;
66     }
67 
CallingAppInfo(@onNull Parcel in)68     private CallingAppInfo(@NonNull Parcel in) {
69         mPackageName = in.readString8();
70         mSigningInfo = in.readTypedObject(SigningInfo.CREATOR);
71         mOrigin = in.readString8();
72     }
73 
74     public static final @NonNull Creator<CallingAppInfo> CREATOR = new Creator<CallingAppInfo>() {
75         @Override
76         public CallingAppInfo createFromParcel(Parcel in) {
77             return new CallingAppInfo(in);
78         }
79 
80         @Override
81         public CallingAppInfo[] newArray(int size) {
82             return new CallingAppInfo[size];
83         }
84     };
85 
86     /** Returns the package name of the source of this info. */
getPackageName()87     @NonNull public String getPackageName() {
88         return mPackageName;
89     }
90 
91     /**
92      * Returns the SigningInfo object that contains an array of
93      * {@link android.content.pm.Signature} belonging to the app.
94      */
getSigningInfo()95     @NonNull public SigningInfo getSigningInfo() {
96         return mSigningInfo;
97     }
98 
99     /**
100      * Returns the origin of the calling app if set otherwise returns null.
101      * This value is set only if the origin is different than that of the calling app,
102      * and should be expected from privileged callers(browsers) only when making request on behalf
103      * of other applications.
104      *
105      * Android system makes sure that only applications that poses the permission
106      * {@link android.Manifest.permission.CREDENTIAL_MANAGER_SET_ORIGIN} can set the origin on
107      * the incoming {@link android.credentials.GetCredentialRequest} or
108      * {@link android.credentials.CreateCredentialRequest}.
109      */
110     @Nullable
getOrigin()111     public String getOrigin() {
112         return mOrigin;
113     }
114 
115     @Override
describeContents()116     public int describeContents() {
117         return 0;
118     }
119 
120     @Override
writeToParcel(@onNull Parcel dest, int flags)121     public void writeToParcel(@NonNull Parcel dest, int flags) {
122         dest.writeString8(mPackageName);
123         dest.writeTypedObject(mSigningInfo, flags);
124         dest.writeString8(mOrigin);
125     }
126 
127     @Override
toString()128     public String toString() {
129         StringBuilder builder =  new StringBuilder("CallingAppInfo {"
130                 + "packageName= " + mPackageName);
131         if (mSigningInfo != null) {
132             builder.append(", mSigningInfo : No. of signatures: " + mSigningInfo
133                     .getApkContentsSigners().length);
134         } else {
135             builder.append(", mSigningInfo: null");
136         }
137         builder.append(",mOrigin: " + mOrigin);
138         builder.append(" }");
139         return builder.toString();
140     }
141 }
142