/
ProviderInjectionEndpoint.java
359 lines (350 loc) · 11.8 KB
/
ProviderInjectionEndpoint.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Copyright (c) 2016-2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.eclipse.microprofile.jwt.tck.container.jaxrs;
import java.util.Optional;
import java.util.Set;
import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.json.Json;
import javax.json.JsonNumber;
import javax.json.JsonObject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.jwt.Claim;
import org.eclipse.microprofile.jwt.Claims;
@Path("/endp")
@RolesAllowed({"Echoer", "Tester"})
@RequestScoped
public class ProviderInjectionEndpoint {
@Inject
@Claim("raw_token")
private Provider<String> rawToken;
@Inject
@Claim("iss")
private Provider<String> issuer;
@Inject
@Claim("jti")
private Provider<String> jti;
@Inject
@Claim("aud")
private Provider<Set<String>> aud;
@Inject
@Claim("iat")
private Provider<Long> issuedAt;
@Inject
@Claim("sub")
private Provider<Optional<String>> optSubject;
@Inject
@Claim("auth_time")
private Provider<Optional<Long>> authTime;
@Inject
@Claim("custom-missing")
private Provider<Optional<Long>> custom;
@Inject
@Claim("customString")
private Provider<String> customString;
@Inject
@Claim("customInteger")
private Provider<JsonNumber> customInteger;
@Inject
@Claim("customDouble")
private Provider<JsonNumber> customDouble;
@GET
@Path("/verifyInjectedIssuer")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedIssuer(@QueryParam("iss") String iss) {
boolean pass = false;
String msg;
String issValue = issuer.get();
if(issValue == null || issValue.length() == 0) {
msg = Claims.iss.name()+"value is null or empty, FAIL";
}
else if(issValue.equals(iss)) {
msg = Claims.iss.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.iss.name(), issValue, iss);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedRawToken")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedRawToken(@QueryParam("raw_token") String rt) {
boolean pass = false;
String msg;
// raw_token
String rawTokenValue = rawToken.get();
if(rawTokenValue == null || rawTokenValue.length() == 0) {
msg = Claims.raw_token.name()+"value is null or empty, FAIL";
}
else if(rawTokenValue.equals(rt)) {
msg = Claims.raw_token.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.raw_token.name(), rawTokenValue, rt);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedJTI")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedJTI(@QueryParam("jti") String jwtID) {
boolean pass = false;
String msg;
// jti
String jtiValue = jti.get();
if(jtiValue == null || jtiValue.length() == 0) {
msg = Claims.jti.name()+"value is null or empty, FAIL";
}
else if(jtiValue.equals(jwtID)) {
msg = Claims.jti.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.jti.name(), jtiValue, jwtID);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedAudience")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedAudience(@QueryParam("aud") String audience) {
boolean pass = false;
String msg;
// aud
Set<String> audValue = aud.get();
if(audValue == null || audValue.size() == 0) {
msg = Claims.aud.name()+"value is null or empty, FAIL";
}
else if(audValue.contains(audience)) {
msg = Claims.aud.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.aud.name(), audValue, audience);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedIssuedAt")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedIssuedAt(@QueryParam("iat") Long iat) {
boolean pass = false;
String msg;
// iat
Long iatValue = issuedAt.get();
if(iatValue == null || iatValue.intValue() == 0) {
msg = Claims.iat.name()+"value is null or empty, FAIL";
}
else if(iatValue.equals(iat)) {
msg = Claims.iat.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.iat.name(), iatValue, iat);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedOptionalSubject")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedOptionalSubject(@QueryParam("sub") String subject) {
boolean pass = false;
String msg;
// sub
Optional<String> optSubValue = optSubject.get();
if(optSubValue == null || !optSubValue.isPresent()) {
msg = Claims.sub.name()+" value is null or missing, FAIL";
}
else if(optSubValue.get().equals(subject)) {
msg = Claims.sub.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.sub.name(), optSubValue, subject);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedOptionalAuthTime")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedOptionalAuthTime(@QueryParam("auth_time") Long authTime) {
boolean pass = false;
String msg;
// auth_time
Optional<Long> optAuthTimeValue = this.authTime.get();
if(optAuthTimeValue == null || !optAuthTimeValue.isPresent()) {
msg = Claims.auth_time.name()+" value is null or missing, FAIL";
}
else if(optAuthTimeValue.get().equals(authTime)) {
msg = Claims.auth_time.name()+" PASS";
pass = true;
}
else {
msg = String.format("%s: %s != %s", Claims.auth_time.name(), optAuthTimeValue, authTime);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
/**
* Verify that values exist and that types match the corresponding Claims enum
* @return a series of pass/fail statements regarding the check for each injected claim
*/
@GET
@Path("/verifyInjectedOptionalCustomMissing")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedOptionalCustomMissing() {
boolean pass = false;
String msg;
// custom-missing
Optional<Long> customValue = custom.get();
if(customValue == null) {
msg = "custom-missing value is null, FAIL";
}
else if(!customValue.isPresent()) {
msg = "custom-missing PASS";
pass = true;
}
else {
msg = String.format("custom: %s != %s", null, customValue.get());
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedCustomString")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomString(@QueryParam("value") String value) {
boolean pass = false;
String msg;
// iat
String customValue = customString.get();
if(customValue == null || customValue.length() == 0) {
msg = "customString value is null or empty, FAIL";
}
else if(customValue.equals(value)) {
msg = "customString PASS";
pass = true;
}
else {
msg = String.format("customString: %s != %s", customValue, value);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedCustomInteger")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomInteger(@QueryParam("value") Long value) {
boolean pass = false;
String msg;
// iat
Object test = customInteger.get();
System.out.printf("+++ verifyInjectedCustomInteger, JsonNumber.class.CL: %s\n",
JsonNumber.class.getClassLoader());
System.out.printf("+++ customInteger.CL: %s\n",
test.getClass().getClassLoader());
Class[] ifaces = test.getClass().getInterfaces();
for(Class iface : ifaces) {
System.out.printf("%s: %s\n", iface, iface.getClassLoader());
}
JsonNumber customValue = JsonNumber.class.cast(test);
if(customValue == null || customValue.isIntegral() == false) {
msg = "customInteger value is null or not integral, FAIL";
}
else if(customValue.longValueExact() == value) {
msg = "customInteger PASS";
pass = true;
}
else {
msg = String.format("customInteger: %d != %d", customValue, value);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
@GET
@Path("/verifyInjectedCustomDouble")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomDouble(@QueryParam("value") Double value) {
boolean pass = false;
String msg;
// iat
JsonNumber customValue = customDouble.get();
if(customValue == null) {
msg = "customDouble value is null, FAIL";
}
else if(Math.abs(customValue.doubleValue() - value.doubleValue()) < 0.00001) {
msg = "customDouble PASS";
pass = true;
}
else {
msg = String.format("customDouble: %s != %.8f", customValue, value);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
}