-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDToken.java
More file actions
192 lines (166 loc) · 6.4 KB
/
Copy pathIDToken.java
File metadata and controls
192 lines (166 loc) · 6.4 KB
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
/*
* Copyright (C) 2018 Google Inc.
*
* 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 org.oidc.msg.oidc;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.oidc.msg.ErrorDetails;
import org.oidc.msg.ErrorType;
import org.oidc.msg.ParameterVerification;
/** ID Token as in http://openid.net/specs/openid-connect-core-1_0.html#IDToken. */
public class IDToken extends OpenIDSchema {
/**
* TODO functionality: Missing to_jwt related functionality like adding c_hash, jti etc. These are
* OP features.
*/
/** Issuer to match the id token to. */
private String issuer;
/** Client ID to match the id token to. */
private String clientId;
/** Nonce to match the id token to. */
private String nonce;
/** Skew in seconds for calculating if the id token has expired or not. */
private long skew = 0;
/** Nonce storage time in seconds. */
private long storageTime = 4 * 60 * 10 * 1000;
{
// Updating parameter requirements.
paramVerDefs.put("iss", ParameterVerification.SINGLE_REQUIRED_STRING.getValue());
paramVerDefs.put("sub", ParameterVerification.SINGLE_REQUIRED_STRING.getValue());
paramVerDefs.put("aud", ParameterVerification.REQUIRED_LIST_OF_STRINGS.getValue());
paramVerDefs.put("exp", ParameterVerification.SINGLE_REQUIRED_DATE.getValue());
paramVerDefs.put("iat", ParameterVerification.SINGLE_REQUIRED_DATE.getValue());
paramVerDefs.put("auth_time", ParameterVerification.SINGLE_OPTIONAL_DATE.getValue());
paramVerDefs.put("nonce", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
paramVerDefs.put("at_hash", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
paramVerDefs.put("c_hash", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
paramVerDefs.put("acr", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
paramVerDefs.put("amr", ParameterVerification.OPTIONAL_LIST_OF_STRINGS.getValue());
paramVerDefs.put("azp", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
paramVerDefs.put("sub_jwk", ParameterVerification.SINGLE_OPTIONAL_STRING.getValue());
}
/**
* Constructor.
*/
public IDToken() {
this(new HashMap<String, Object>());
}
/**
* Constructor.
*
* @param claims
* ID Token claims as described in
* http://openid.net/specs/openid-connect-core-1_0.html#IDToken.
*/
public IDToken(Map<String, Object> claims) {
super(claims);
}
/**
* Set Issuer to use when verifying id token.
*
* @param issuer
* Issuer to match the id token to.
*/
public void setIssuer(String issuer) {
this.issuer = issuer;
}
/**
* Set Client ID to use when verifying id token.
*
* @param clientId
* Client ID to match the id token to.
*/
public void setClientId(String clientId) {
this.clientId = clientId;
}
/**
* Set Nonce to use when verifying id token. Comparison is done only if id token has nonce.
*
* @param nonce
* Nonce to match the id token to.
*/
public void setNonce(String nonce) {
this.nonce = nonce;
}
/**
* Set Skew in seconds for calculating if the id token has expired or not.
*
* @param skew
* Skew in seconds for calculating if the id token has expired or not.
*/
public void setSkew(long skew) {
this.skew = skew * 1000;
}
/**
* Set nonce storage time in seconds. Id token must not have been issued longer ago than nonce
* storage time is. Default is 4h.
*
* @param storageTime
* nonce storage time in seconds
*/
public void setStorageTime(long storageTime) {
this.storageTime = storageTime * 1000;
}
/** {@inheritDoc} */
@Override
protected void doVerify() {
if (issuer != null && !issuer.equals(getClaims().get("iss"))) {
getError().getDetails()
.add(new ErrorDetails("iss", ErrorType.VALUE_NOT_ALLOWED,
String.format(
"Issuer mismatch, expected value '%s' for iss claim but got '%s' instead", issuer,
getClaims().get("iss"))));
}
List<String> aud = (List<String>) getClaims().get("aud");
if (clientId != null && (aud == null || !aud.contains(clientId))) {
getError().getDetails().add(new ErrorDetails("aud", ErrorType.MISSING_REQUIRED_VALUE,
String.format("Client ID '%s' is not listed in the aud claim", clientId)));
}
if (aud != null && aud.size() > 1 && ((getClaims().get("azp") == null)
|| !aud.contains(getClaims().get("azp")))) {
getError().getDetails().add(new ErrorDetails("azp", ErrorType.MISSING_REQUIRED_VALUE,
"If claim aud has multiple values one of them must have value of azp claim."));
}
if (getClaims().get("azp") != null && clientId != null
&& !clientId.equals((String) getClaims().get("azp"))) {
getError().getDetails()
.add(new ErrorDetails("azp", ErrorType.VALUE_NOT_ALLOWED,
String.format("Client ID '%s' should equal to azp claim value '%s'", clientId,
getClaims().get("azp"))));
}
long now = System.currentTimeMillis();
if (getClaims().containsKey("exp")) {
long exp = ((Date) getClaims().get("exp")).getTime();
if (now - skew > exp) {
getError().getDetails()
.add(new ErrorDetails("exp", ErrorType.VALUE_NOT_ALLOWED, "Claim exp is in the past"));
}
}
if (getClaims().containsKey("iat")) {
long iat = ((Date) getClaims().get("iat")).getTime();
if (iat + storageTime < now - skew) {
getError().getDetails().add(new ErrorDetails("iat", ErrorType.VALUE_NOT_ALLOWED,
"id token has been issued too long ago"));
}
}
if (nonce != null && getClaims().get("nonce") != null
&& !nonce.equals(getClaims().get("nonce"))) {
getError().getDetails()
.add(new ErrorDetails("nonce", ErrorType.VALUE_NOT_ALLOWED, "nonce mismatch"));
}
}
}