-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathToken.java
303 lines (274 loc) · 9.09 KB
/
Token.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
/**
* SPDX-FileCopyrightText: 2018-2023 SAP SE or an SAP affiliate company and Cloud Security Client Java contributors
* <p>
* SPDX-License-Identifier: Apache-2.0
*/
package com.sap.cloud.security.token;
import com.sap.cloud.security.config.Service;
import com.sap.cloud.security.json.JsonObject;
import com.sap.cloud.security.json.JsonParsingException;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.security.Principal;
import java.security.ProviderException;
import java.time.Instant;
import java.util.*;
import static com.sap.cloud.security.token.TokenClaims.*;
/**
* Represents a JSON Web Token (JWT).
*/
public interface Token extends Serializable {
@SuppressWarnings("unchecked")
List<TokenFactory> services = new ArrayList() {
{
ServiceLoader.load(TokenFactory.class).forEach(this::add);
LoggerFactory.getLogger(Token.class).info("loaded TokenFactory service providers: {}", this);
}
};
String DEFAULT_TOKEN_FACTORY = "com.sap.cloud.security.servlet.HybridTokenFactory";
/**
* Creates a token instance based on TokenFactory implementation.
*
* @param jwt
* encoded JWT token
* @return token instance
*/
static Token create(String jwt) {
if (services.isEmpty()) {
throw new ProviderNotFoundException("No TokenFactory implementation found in the classpath");
}
if (services.size() > 2) {
throw new ProviderException(
"More than 1 Custom TokenFactory service provider found. There should be only one");
}
if (services.size() == 2) {
return services.stream()
.filter(tokenFactory -> !tokenFactory.getClass().getName()
.equals(DEFAULT_TOKEN_FACTORY))
.findFirst().get().create(jwt);
}
return services.get(0).create(jwt);
}
/**
* Returns the header parameter value as string for the given header parameter name.
*
* @param headerName
* the name of the header parameter as defined here {@link TokenHeader}
* @return the value for the given header name or null, if the header is not provided.
*/
@Nullable
String getHeaderParameterAsString(@Nonnull String headerName);
/**
* Checks whether the token contains a given header parameter.
*
* @param headerName
* the name of the header parameter as defined here {@link TokenHeader}
* @return true when the given header name is found.
*/
boolean hasHeaderParameter(@Nonnull String headerName);
/**
* Checks whether the token contains a given claim.
*
* @param claimName
* the name of the claim as defined here {@link TokenClaims}.
* @return true when the claim with the given name is found.
*/
boolean hasClaim(@Nonnull String claimName);
/**
* Extracts the value as string for the given claim. If the claim is not found, it will return null. If the given
* claim is not a string, it will throw a {@link JsonParsingException}.
*
* @param claimName
* the name of the claim as defined here {@link TokenClaims}.
* @return the corresponding string value of the given claim or null.
* @throws JsonParsingException
* if the json object identified by the given claim is not a string.
*/
@Nullable
String getClaimAsString(@Nonnull String claimName);
/**
* Extracts the value as a list of strings for the given claim. If the claim is not found, it will return null. If
* the given claim is not a list of strings, it will throw a {@link JsonParsingException}.
*
* @param claimName
* the name of the claim as defined here {@link TokenClaims}.
* @return the data of the given claim as a list of strings or an empty list.
*/
@Nonnull
List<String> getClaimAsStringList(@Nonnull String claimName);
/**
* Extracts the value of the given as a JsonObject. Use this to extract nested objects. If the claim is not found,
* it will return null. If the vale for the given claim is not an object, it will throw a
* {@link JsonParsingException}.
*
* @param claimName
* the name of the claim for which the object should be extracted.
* @return the corresponding {@link JsonObject} for the given claim.
*/
@Nullable
JsonObject getClaimAsJsonObject(@Nonnull String claimName);
/**
* Returns the moment in time when the token will be expired.
*
* @return the expiration point in time if present.
*/
@Nullable
Instant getExpiration();
/**
* Returns true if the token is expired.
*
* @return true if the token is expired.
*/
boolean isExpired();
/**
* Returns the moment in time before which the token must not be accepted.
*
* @return the not before point in time if present.
*/
@Nullable
Instant getNotBefore();
/**
* Get the encoded jwt token, e.g. for token forwarding to another app.
*
* <p>
* Never expose this token via log or via HTTP.
*
* @return the encoded token.
*/
String getTokenValue();
/**
* Returns a principal, which can be used to represent any entity, such as an individual, a corporation, and a login
* id.
*
* @return the principal or null if not yet implemented.
*/
Principal getPrincipal();
/**
* Returns the identity service, the token is issued by.
*
* @return the service.
*/
Service getService();
/**
* Returns the (empty) list of audiences the token is issued for. <br>
*
* @return the audiences.
**/
default Set<String> getAudiences() {
return new LinkedHashSet<>(getClaimAsStringList(AUDIENCE));
}
/**
* @deprecated use {@link Token#getAppTid()} instead
*/
@Deprecated
default String getZoneId() {
return getAppTid();
}
/**
* Returns the app tenant identifier, which can be used as tenant discriminator (tenant guid).
*
* @return the unique application tenant identifier.
*/
default String getAppTid() {
return hasClaim(SAP_GLOBAL_APP_TID) ? getClaimAsString(SAP_GLOBAL_APP_TID)
: getClaimAsString(SAP_GLOBAL_ZONE_ID);
}
/**
* Returns the OAuth2 client identifier of the authentication token if present. Following OpenID Connect 1.0
* standard specifications, client identifier is obtained from "azp" claim if present or when "azp" is not present
* from "aud" claim, but only in case there is one audience.
*
* @return the OAuth client ID.
* @see <a href=
* "https://openid.net/specs/openid-connect-core-1_0.html">https://openid.net/specs/openid-connect-core-1_0.html</a>
*/
default String getClientId() {
String clientId = getClaimAsString(AUTHORIZATION_PARTY);
if (clientId == null || clientId.trim().isEmpty()) {
Set<String> audiences = getAudiences();
if (audiences.size() == 1) {
return audiences.stream().findFirst().get();
}
throw new InvalidTokenException("Couldn't get client id. Invalid authorized party or audience claims.");
} else {
return clientId;
}
}
/**
* Returns the identifier for the Issuer of the token. Its a URL that contains scheme, host, and optionally, port
* number and path components but no query or fragment components. This one is validated in the
* {@code JwtIssuerValidator} and used as base url to discover jwks_uri endpoint for downloading the token keys.
*
* @return the issuer.
*/
default String getIssuer() {
return getClaimAsString(ISSUER);
}
/**
* Returns the grant type of the jwt token. <br>
*
* @return the grant type
**/
@Nullable
default GrantType getGrantType() {
return GrantType.from(getClaimAsString(TokenClaims.XSUAA.GRANT_TYPE));
}
/**
* Returns the header(s).
*
* @return a {@code Map} of the header(s)
*/
default Map<String, Object> getHeaders() {
return Collections.emptyMap();
}
/**
* Returns the jwt claim set.
*
* @return a {@code Map} of the jwt claim set
*/
default Map<String, Object> getClaims() {
return Collections.emptyMap();
}
/**
* Returns the String value of a claim attribute. <br>
* <code>
* "claimName": { "attributeName": "attributeValueAsString" },
* </code><br>
* <br>
* Example: <br>
* <code>
* import static com.sap.cloud.security.token.TokenClaims.XSUAA.*;
* token.getAttributeFromClaimAsString(EXTERNAL_ATTRIBUTE, EXTERNAL_ATTRIBUTE_SUBACCOUNTID);
* </code>
*
* @return the String value of a claim attribute or null if claim or its attribute does not exist.
**/
@Nullable
default String getAttributeFromClaimAsString(String claimName, String attributeName) {
return Optional.ofNullable(getClaimAsJsonObject(claimName))
.map(claim -> claim.getAsString(attributeName))
.orElse(null);
}
/**
* Returns the String list of a claim attribute. <br>
* <code>
* "claimName": { "attributeName": ["attributeValueAsString", "attributeValue2AsString"] },
* </code><br>
* <br>
* Example: <br>
* <code>
* import static com.sap.cloud.security.token.TokenClaims.XSUAA.*;
* token.getAttributeFromClaimAsString(XS_USER_ATTRIBUTES, "custom_role");
* </code>
*
* @return the list of String values of a claim attribute or empty List if claim or its attribute does not exist.
**/
default List<String> getAttributeFromClaimAsStringList(String claimName, String attributeName) {
JsonObject claimAsJsonObject = getClaimAsJsonObject(claimName);
return Optional.ofNullable(claimAsJsonObject)
.map(jsonObject -> jsonObject.getAsList(attributeName, String.class))
.orElse(Collections.emptyList());
}
}