-
Notifications
You must be signed in to change notification settings - Fork 911
/
Copy pathIdToken.java
310 lines (267 loc) · 11.8 KB
/
IdToken.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright 2018 The AppAuth for Android Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openid.appauth;
import static net.openid.appauth.AdditionalParamsProcessor.builtInParams;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import net.openid.appauth.AuthorizationException.GeneralErrors;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* An OpenID Connect ID Token. Contains claims about the authentication of an End-User by an
* Authorization Server. Supports parsing ID Tokens from JWT Compact Serializations and validation
* according to the OpenID Connect specification.
*
* @see "OpenID Connect Core ID Token, Section 2
* <http://openid.net/specs/openid-connect-core-1_0.html#IDToken>"
* @see "OpenID Connect Core ID Token Validation, Section 3.1.3.7
* <http://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation>"
*/
public class IdToken {
private static final String KEY_ISSUER = "iss";
private static final String KEY_SUBJECT = "sub";
private static final String KEY_AUDIENCE = "aud";
private static final String KEY_EXPIRATION = "exp";
private static final String KEY_ISSUED_AT = "iat";
private static final String KEY_NONCE = "nonce";
private static final String KEY_AUTHORIZED_PARTY = "azp";
private static final Long MILLIS_PER_SECOND = 1000L;
private static final Long TEN_MINUTES_IN_SECONDS = 600L;
private static final Set<String> BUILT_IN_CLAIMS = builtInParams(
KEY_ISSUER,
KEY_SUBJECT,
KEY_AUDIENCE,
KEY_EXPIRATION,
KEY_ISSUED_AT,
KEY_NONCE,
KEY_AUTHORIZED_PARTY);
/**
* Issuer Identifier for the Issuer of the response.
*/
@NonNull
public final String issuer;
/**
* Subject Identifier. A locally unique and never reassigned identifier within the Issuer
* for the End-User.
*/
@NonNull
public final String subject;
/**
* Audience(s) that this ID Token is intended for.
*/
@NonNull
public final List<String> audience;
/**
* Expiration time on or after which the ID Token MUST NOT be accepted for processing.
*/
@NonNull
public final Long expiration;
/**
* Time at which the JWT was issued.
*/
@NonNull
public final Long issuedAt;
/**
* String value used to associate a Client session with an ID Token,
* and to mitigate replay attacks.
*/
@Nullable
public final String nonce;
/**
* Authorized party - the party to which the ID Token was issued.
* If present, it MUST contain the OAuth 2.0 Client ID of this party.
*/
@Nullable
public final String authorizedParty;
/**
* Additional claims present in this ID Token.
*/
@NonNull
public final Map<String, Object> additionalClaims;
@VisibleForTesting
IdToken(@NonNull String issuer,
@NonNull String subject,
@NonNull List<String> audience,
@NonNull Long expiration,
@NonNull Long issuedAt) {
this(issuer, subject, audience, expiration, issuedAt, null, null, Collections.emptyMap());
}
@VisibleForTesting
IdToken(@NonNull String issuer,
@NonNull String subject,
@NonNull List<String> audience,
@NonNull Long expiration,
@NonNull Long issuedAt,
@Nullable String nonce,
@Nullable String authorizedParty) {
this(issuer, subject, audience, expiration, issuedAt,
nonce, authorizedParty, Collections.emptyMap());
}
IdToken(@NonNull String issuer,
@NonNull String subject,
@NonNull List<String> audience,
@NonNull Long expiration,
@NonNull Long issuedAt,
@Nullable String nonce,
@Nullable String authorizedParty,
@NonNull Map<String, Object> additionalClaims) {
this.issuer = issuer;
this.subject = subject;
this.audience = audience;
this.expiration = expiration;
this.issuedAt = issuedAt;
this.nonce = nonce;
this.authorizedParty = authorizedParty;
this.additionalClaims = additionalClaims;
}
private static JSONObject parseJwtSection(String section) throws JSONException {
byte[] decodedSection = Base64.decode(section, Base64.URL_SAFE);
String jsonString = new String(decodedSection);
return new JSONObject(jsonString);
}
static IdToken from(String token) throws JSONException, IdTokenException {
String[] sections = token.split("\\.");
if (sections.length <= 1) {
throw new IdTokenException("ID token must have both header and claims section");
}
// We ignore header contents, but parse it to check that it is structurally valid JSON
parseJwtSection(sections[0]);
JSONObject claims = parseJwtSection(sections[1]);
final String issuer = JsonUtil.getString(claims, KEY_ISSUER);
final String subject = JsonUtil.getString(claims, KEY_SUBJECT);
List<String> audience;
try {
audience = JsonUtil.getStringList(claims, KEY_AUDIENCE);
} catch (JSONException jsonEx) {
audience = new ArrayList<>();
audience.add(JsonUtil.getString(claims, KEY_AUDIENCE));
}
final Long expiration = claims.getLong(KEY_EXPIRATION);
final Long issuedAt = claims.getLong(KEY_ISSUED_AT);
final String nonce = JsonUtil.getStringIfDefined(claims, KEY_NONCE);
final String authorizedParty = JsonUtil.getStringIfDefined(claims, KEY_AUTHORIZED_PARTY);
for (String key: BUILT_IN_CLAIMS) {
claims.remove(key);
}
Map<String, Object> additionalClaims = JsonUtil.toMap(claims);
return new IdToken(
issuer,
subject,
audience,
expiration,
issuedAt,
nonce,
authorizedParty,
additionalClaims
);
}
@VisibleForTesting
void validate(@NonNull TokenRequest tokenRequest, Clock clock) throws AuthorizationException {
validate(tokenRequest, clock, false);
}
void validate(@NonNull TokenRequest tokenRequest,
Clock clock,
boolean skipIssuerHttpsCheck) throws AuthorizationException {
// OpenID Connect Core Section 3.1.3.7. rule #1
// Not enforced: AppAuth does not support JWT encryption.
// OpenID Connect Core Section 3.1.3.7. rule #2
// Validates that the issuer in the ID Token matches that of the discovery document.
AuthorizationServiceDiscovery discoveryDoc = tokenRequest.configuration.discoveryDoc;
if (discoveryDoc != null) {
String expectedIssuer = discoveryDoc.getIssuer();
if (!this.issuer.equals(expectedIssuer)) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Issuer mismatch"));
}
// OpenID Connect Core Section 2.
// The iss value is a case sensitive URL using the https scheme that contains scheme,
// host, and optionally, port number and path components and no query or fragment
// components.
Uri issuerUri = Uri.parse(this.issuer);
if (!skipIssuerHttpsCheck && !issuerUri.getScheme().equals("https")) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Issuer must be an https URL"));
}
if (TextUtils.isEmpty(issuerUri.getHost())) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Issuer host can not be empty"));
}
if (issuerUri.getFragment() != null || issuerUri.getQueryParameterNames().size() > 0) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException(
"Issuer URL should not containt query parameters or fragment components"));
}
}
// OpenID Connect Core Section 3.1.3.7. rule #3 & Section 2 azp Claim
// Validates that the aud (audience) Claim contains the client ID, or that the azp
// (authorized party) Claim matches the client ID.
String clientId = tokenRequest.clientId;
if (!this.audience.contains(clientId) && !clientId.equals(this.authorizedParty)) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Audience mismatch"));
}
// OpenID Connect Core Section 3.1.3.7. rules #4 & #5
// Not enforced.
// OpenID Connect Core Section 3.1.3.7. rule #6
// As noted above, AppAuth only supports the code flow which results in direct
// communication of the ID Token from the Token Endpoint to the Client, and we are
// exercising the option to use TLS server validation instead of checking the token
// signature. Users may additionally check the token signature should they wish.
// OpenID Connect Core Section 3.1.3.7. rules #7 & #8
// Not enforced. See rule #6.
// OpenID Connect Core Section 3.1.3.7. rule #9
// Validates that the current time is before the expiry time.
Long nowInSeconds = clock.getCurrentTimeMillis() / MILLIS_PER_SECOND;
if (nowInSeconds > this.expiration) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("ID Token expired"));
}
// OpenID Connect Core Section 3.1.3.7. rule #10
// Validates that the issued at time is not more than +/- 10 minutes on the current
// time.
if (Math.abs(nowInSeconds - this.issuedAt) > TEN_MINUTES_IN_SECONDS) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Issued at time is more than 10 minutes "
+ "before or after the current time"));
}
// Only relevant for the authorization_code response type
if (GrantTypeValues.AUTHORIZATION_CODE.equals(tokenRequest.grantType)) {
// OpenID Connect Core Section 3.1.3.7. rule #11
// Validates the nonce.
String expectedNonce = tokenRequest.nonce;
if (!TextUtils.equals(this.nonce, expectedNonce)) {
throw AuthorizationException.fromTemplate(GeneralErrors.ID_TOKEN_VALIDATION_ERROR,
new IdTokenException("Nonce mismatch"));
}
}
// OpenID Connect Core Section 3.1.3.7. rules #12
// ACR is not directly supported by AppAuth.
// OpenID Connect Core Section 3.1.3.7. rules #13
// max_age is not directly supported by AppAuth.
}
static class IdTokenException extends Exception {
IdTokenException(String message) {
super(message);
}
}
}