1 /*
2  * Copyright (C) 2023 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 com.android.libraries.entitlement.odsa;
18 
19 import androidx.annotation.NonNull;
20 import androidx.annotation.Nullable;
21 
22 import com.android.libraries.entitlement.EsimOdsaOperation.OdsaOperation;
23 import com.android.libraries.entitlement.utils.Ts43Constants;
24 import com.android.libraries.entitlement.utils.Ts43Constants.AppId;
25 
26 import com.google.auto.value.AutoValue;
27 import com.google.common.collect.ImmutableList;
28 
29 import java.time.Instant;
30 
31 /**
32  * Acquire temporary token operation described in GSMA Service Entitlement Configuration section 6.
33  */
34 public final class AcquireTemporaryTokenOperation {
35     /**
36      * Acquire temporary token request described in GSMA Service Entitlement Configuration section
37      * 6.2.
38      */
39     @AutoValue
40     public abstract static class AcquireTemporaryTokenRequest {
41         /**
42          * Returns the application id. Can only be {@link Ts43Constants#APP_ODSA_COMPANION}, {@link
43          * Ts43Constants#APP_ODSA_PRIMARY}, or
44          * {@link Ts43Constants#APP_ODSA_SERVER_INITIATED_REQUESTS}.
45          */
46         @NonNull
47         @AppId
appId()48         public abstract String appId();
49 
50         /**
51          * Returns the comma separated list of operation targets used with temporary token from
52          * {@code AcquireTemporaryToken} operation. Used by HTTP parameter
53          * {@code operation_targets}.
54          */
55         @NonNull
56         @OdsaOperation
operationTargets()57         public abstract ImmutableList<String> operationTargets();
58 
59         /**
60          * Returns the unique identifier of the companion device, like IMEI. Used by HTTP parameter
61          * {@code companion_terminal_id}.
62          */
63         @NonNull
companionTerminalId()64         public abstract String companionTerminalId();
65 
66         /** Returns a new {@link Builder} object. */
67         @NonNull
builder()68         public static Builder builder() {
69             return new AutoValue_AcquireTemporaryTokenOperation_AcquireTemporaryTokenRequest
70                     .Builder()
71                     .setAppId(Ts43Constants.APP_UNKNOWN)
72                     .setOperationTargets(ImmutableList.of())
73                     .setCompanionTerminalId("");
74         }
75 
76         /** Builder. */
77         @AutoValue.Builder
78         public abstract static class Builder {
79             /**
80              * Sets the application id.
81              *
82              * @param appId The application id. Can only be
83              *              {@link Ts43Constants#APP_ODSA_COMPANION},
84              *              {@link Ts43Constants#APP_ODSA_PRIMARY}, or
85              *              {@link Ts43Constants#APP_ODSA_SERVER_INITIATED_REQUESTS}.
86              * @return The builder.
87              */
88             @NonNull
setAppId(@onNull @ppId String appId)89             public abstract Builder setAppId(@NonNull @AppId String appId);
90 
91             /**
92              * Sets the operation targets to be used with temporary token from {@code
93              * AcquireTemporaryToken} operation. Used by HTTP parameter {@code operation_targets} if
94              * set.
95              *
96              * @param operationTargets The operation targets to be used with temporary token from
97              *                         {@code AcquireTemporaryToken} operation.
98              * @return The builder.
99              */
100             @NonNull
setOperationTargets( @onNull @dsaOperation ImmutableList<String> operationTargets)101             public abstract Builder setOperationTargets(
102                     @NonNull @OdsaOperation ImmutableList<String> operationTargets);
103 
104             /**
105              * Sets the unique identifier of the companion device, like IMEI. Used by HTTP parameter
106              * {@code companion_terminal_id} if set.
107              *
108              * <p>Used by companion device ODSA operation.
109              *
110              * @param companionTerminalId The unique identifier of the companion device.
111              * @return The builder.
112              */
113             @NonNull
setCompanionTerminalId(@onNull String companionTerminalId)114             public abstract Builder setCompanionTerminalId(@NonNull String companionTerminalId);
115 
116             /** Returns the {@link AcquireTemporaryTokenRequest} object. */
117             @NonNull
build()118             public abstract AcquireTemporaryTokenRequest build();
119         }
120     }
121 
122     /**
123      * Acquire temporary token response described in GSMA Service Entitlement Configuration section
124      * 6.5.7.
125      */
126     @AutoValue
127     @AutoValue.CopyAnnotations
128     @SuppressWarnings("AndroidJdkLibsChecker") // java.time.Instant
129     public abstract static class AcquireTemporaryTokenResponse extends OdsaResponse {
130         /** The temporary token used to establish trust between ECS and the client. */
131         @NonNull
temporaryToken()132         public abstract String temporaryToken();
133 
134         /** The expiration time (UTC time) of the token. {@code null} if not available. */
135         @AutoValue.CopyAnnotations
136         @SuppressWarnings("AndroidJdkLibsChecker") // java.time.Instant
137 
138         @Nullable
temporaryTokenExpiry()139         public abstract Instant temporaryTokenExpiry();
140 
141         /** The allowed ODSA operations requested using {@link #temporaryToken()}. */
142         @NonNull
143         @OdsaOperation
operationTargets()144         public abstract ImmutableList<String> operationTargets();
145 
146         /** Returns a new {@link AcquireTemporaryTokenRequest.Builder} object. */
147         @NonNull
builder()148         public static Builder builder() {
149             return new AutoValue_AcquireTemporaryTokenOperation_AcquireTemporaryTokenResponse
150                     .Builder()
151                     .setTemporaryToken("")
152                     .setTemporaryTokenExpiry(null)
153                     .setOperationTargets(ImmutableList.of());
154         }
155 
156         /** Builder. */
157         @AutoValue.Builder
158         @AutoValue.CopyAnnotations
159         @SuppressWarnings("AndroidJdkLibsChecker") // java.time.Instant
160         public abstract static class Builder extends OdsaResponse.Builder {
161             /**
162              * Sets the temporary token.
163              *
164              * @param token The temporary token used to establish trust between ECS and the client.
165              * @return The builder.
166              */
167             @NonNull
setTemporaryToken(@onNull String token)168             public abstract Builder setTemporaryToken(@NonNull String token);
169 
170             /**
171              * Sets the expiration time of the token.
172              *
173              * @param expiry The expiration time (UTC time) of the token.
174              * @return The builder.
175              */
176             @AutoValue.CopyAnnotations
177             @SuppressWarnings("AndroidJdkLibsChecker") // java.time.Instant
178             @NonNull
setTemporaryTokenExpiry(@onNull Instant expiry)179             public abstract Builder setTemporaryTokenExpiry(@NonNull Instant expiry);
180 
181             /**
182              * Sets the allowed ODSA operations requested using {@link #temporaryToken()}.
183              *
184              * @param operationTargets The allowed ODSA operations requested using {@link
185              *                         #temporaryToken()}.
186              * @return The builder.
187              */
188             @NonNull
setOperationTargets( @onNull @dsaOperation ImmutableList<String> operationTargets)189             public abstract Builder setOperationTargets(
190                     @NonNull @OdsaOperation ImmutableList<String> operationTargets);
191 
192             /** Returns the {@link AcquireTemporaryTokenResponse} object. */
193             @NonNull
build()194             public abstract AcquireTemporaryTokenResponse build();
195         }
196     }
197 
AcquireTemporaryTokenOperation()198     private AcquireTemporaryTokenOperation() {
199     }
200 }
201