-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathOAuth2Options.java
More file actions
708 lines (625 loc) · 18.3 KB
/
Copy pathOAuth2Options.java
File metadata and controls
708 lines (625 loc) · 18.3 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/*
* Copyright (c) 2011-2014 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.vertx.ext.auth.oauth2;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.internal.logging.Logger;
import io.vertx.core.internal.logging.LoggerFactory;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.JWTOptions;
import io.vertx.ext.auth.PubSecKeyOptions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Options describing how an OAuth2 {@link HttpClient} will make connections.
*
* @author <a href="mailto:plopes@redhat.com">Paulo Lopes</a>
*/
@DataObject
@JsonGen(publicConverter = false)
public class OAuth2Options {
private static final Logger LOG = LoggerFactory.getLogger(OAuth2Options.class);
// Defaults
private static final boolean BASIC_AUTHORIZATION = true;
private static final String AUTHORIZATION_PATH = "/oauth/authorize";
private static final String TOKEN_PATH = "/oauth/token";
private static final String REVOCATION_PATH = "/oauth/revoke";
private static final String SCOPE_SEPARATOR = " ";
private static final boolean VALIDATE_ISSUER = true;
//seconds of JWK's default age (-1 means no rotation)
private static final long JWK_DEFAULT_AGE = -1L;
private List<String> supportedGrantTypes;
private String authorizationPath;
private String tokenPath;
private String revocationPath;
private String scopeSeparator;
// this is an openid-connect extension
private boolean validateIssuer;
private String logoutPath;
private String userInfoPath;
// extra parameters to be added while requesting the user info
private JsonObject userInfoParams;
// introspection RFC7662
private String introspectionPath;
// JWK path RFC7517
private String jwkPath;
//seconds of JWKs lifetime
private long jwkMaxAge;
// OpenID non standard
private String tenant;
private String site;
private String clientId;
private String clientSecret;
private boolean useBasicAuthorization;
//https://tools.ietf.org/html/rfc7521
private String clientAssertionType;
private String clientAssertion;
private String userAgent;
private JsonObject headers;
private List<PubSecKeyOptions> pubSecKeys;
private JWTOptions jwtOptions;
// extra parameters to be added while requesting a token
private JsonObject extraParams;
// client config
private HttpClientOptions httpClientOptions = new HttpClientOptions();
private List<JsonObject> jwks;
public String getSite() {
return site;
}
/**
* Default constructor
*/
public OAuth2Options() {
init();
}
/**
* Copy constructor
*
* @param other the options to copy
*/
public OAuth2Options(OAuth2Options other) {
tenant = other.getTenant();
clientId = other.getClientId();
clientSecret = other.getClientSecret();
useBasicAuthorization = other.isUseBasicAuthorization();
clientAssertionType = other.getClientAssertionType();
clientAssertion = other.getClientAssertion();
validateIssuer = other.isValidateIssuer();
authorizationPath = other.getAuthorizationPath();
tokenPath = other.getTokenPath();
revocationPath = other.getRevocationPath();
userInfoPath = other.getUserInfoPath();
introspectionPath = other.getIntrospectionPath();
scopeSeparator = other.getScopeSeparator();
site = other.getSite();
if (other.pubSecKeys == null) {
pubSecKeys = null;
} else {
List<PubSecKeyOptions> list = new ArrayList<>(other.pubSecKeys.size());
for (PubSecKeyOptions pubSecKey : other.pubSecKeys) {
list.add(new PubSecKeyOptions(pubSecKey));
}
pubSecKeys = list;
}
jwtOptions = other.jwtOptions == null ? null : new JWTOptions(other.jwtOptions);
logoutPath = other.getLogoutPath();
extraParams = other.extraParams == null ? null : other.extraParams.copy();
userInfoParams = other.userInfoParams == null ? null : other.userInfoParams.copy();
headers = other.headers == null ? null : other.headers.copy();
jwkPath = other.getJwkPath();
jwkMaxAge = other.getJwkMaxAgeInSeconds();
httpClientOptions = other.httpClientOptions == null ? null : new HttpClientOptions(other.httpClientOptions);
userAgent = other.getUserAgent();
supportedGrantTypes = other.supportedGrantTypes == null ? null : new ArrayList<>(other.supportedGrantTypes);
if (other.jwks == null) {
jwks = null;
} else {
List<JsonObject> list = new ArrayList<>(other.jwks.size());
for (JsonObject jwk : other.jwks) {
list.add(jwk.copy());
}
jwks = list;
}
// compute paths with variables, at this moment it is only relevant that
// the paths and site are properly computed
replaceVariables(false);
}
private void init() {
validateIssuer = VALIDATE_ISSUER;
authorizationPath = AUTHORIZATION_PATH;
tokenPath = TOKEN_PATH;
revocationPath = REVOCATION_PATH;
scopeSeparator = SCOPE_SEPARATOR;
jwtOptions = new JWTOptions();
jwkMaxAge = JWK_DEFAULT_AGE;
useBasicAuthorization = BASIC_AUTHORIZATION;
}
/**
* Constructor to create an options from JSON
*
* @param json the JSON
*/
public OAuth2Options(JsonObject json) {
init();
OAuth2OptionsConverter.fromJson(json, this);
// compute paths with variables, at this moment it is only relevant that
// the paths and site are properly computed
replaceVariables(false);
}
/**
* Get the Oauth2 authorization resource path. e.g.: /oauth/authorize
*
* @return authorization path
*/
public String getAuthorizationPath() {
return authorizationPath;
}
public OAuth2Options setAuthorizationPath(String authorizationPath) {
this.authorizationPath = authorizationPath;
return this;
}
/**
* Get the Oauth2 token resource path. e.g.: /oauth/token
*
* @return token path
*/
public String getTokenPath() {
return tokenPath;
}
public OAuth2Options setTokenPath(String tokenPath) {
this.tokenPath = tokenPath;
return this;
}
/**
* Get the Oauth2 revocation resource path. e.g.: /oauth/revoke
*
* @return revocation path
*/
public String getRevocationPath() {
return revocationPath;
}
/**
* Set the Oauth2 revocation resource path. e.g.: /oauth/revoke
*
* @return self
*/
public OAuth2Options setRevocationPath(String revocationPath) {
this.revocationPath = revocationPath;
return this;
}
/**
* Root URL for the provider without trailing slashes
*
* @param site a url
* @return self
*/
public OAuth2Options setSite(String site) {
this.site = site;
return this;
}
/**
* Get the provider client id
*
* @return client id
*/
public String getClientId() {
return clientId;
}
/**
* Set the provider client id
*
* @param clientId client id
* @return self
*/
public OAuth2Options setClientId(String clientId) {
this.clientId = clientId;
return this;
}
/**
* Get the provider client secret
*
* @return the client secret
*/
public String getClientSecret() {
return clientSecret;
}
/**
* Set the provider client secret
*
* @param clientSecret client secret
* @return self
*/
public OAuth2Options setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
}
public OAuth2Options setUseBasicAuthorization(boolean useBasicAuthorization) {
this.useBasicAuthorization = useBasicAuthorization;
return this;
}
public boolean isUseBasicAuthorization() {
return useBasicAuthorization;
}
public String getClientAssertionType() {
return clientAssertionType;
}
public OAuth2Options setClientAssertionType(String clientAssertionType) {
this.clientAssertionType = clientAssertionType;
this.useBasicAuthorization = false;
return this;
}
public String getClientAssertion() {
return clientAssertion;
}
public OAuth2Options setClientAssertion(String clientAssertion) {
this.clientAssertion = clientAssertion;
return this;
}
/**
* The User-Agent header to use when communicating with a provider
*
* @return the user agent string
*/
public String getUserAgent() {
return userAgent;
}
/**
* Set a custom user agent to use when communicating to a provider
*
* @param userAgent the user agent
* @return self
*/
public OAuth2Options setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
/**
* Custom headers to send along with every request.
*
* @return the headers as a json structure
*/
public JsonObject getHeaders() {
return headers;
}
/**
* Set custom headers to be sent with every request to the provider
*
* @param headers the headers
* @return self
*/
public OAuth2Options setHeaders(JsonObject headers) {
this.headers = headers;
return this;
}
/**
* The provider PubSec key options
*
* @return the pub sec key options
*/
public List<PubSecKeyOptions> getPubSecKeys() {
return pubSecKeys;
}
public OAuth2Options setPubSecKeys(List<PubSecKeyOptions> pubSecKeys) {
this.pubSecKeys = pubSecKeys;
return this;
}
public OAuth2Options addPubSecKey(PubSecKeyOptions pubSecKey) {
if (pubSecKeys == null) {
pubSecKeys = new ArrayList<>();
}
pubSecKeys.add(pubSecKey);
return this;
}
/**
* The provider logout path
*
* @return a logout resource path
*/
public String getLogoutPath() {
return logoutPath;
}
/**
* Set the provider logout path
*
* @param logoutPath a logout resource path
* @return self
*/
public OAuth2Options setLogoutPath(String logoutPath) {
this.logoutPath = logoutPath;
return this;
}
/**
* The provider userInfo resource path
*
* @return a resouce path
*/
public String getUserInfoPath() {
return userInfoPath;
}
/**
* Set the provider userInfo resource path
*
* @param userInfoPath a resource path
* @return self
*/
public OAuth2Options setUserInfoPath(String userInfoPath) {
this.userInfoPath = userInfoPath;
return this;
}
/**
* Set the provider scope separator
*
* @return a single character string usually a space or a plus
*/
public String getScopeSeparator() {
return scopeSeparator;
}
/**
* Set the provider scope separator
*
* @param scopeSeparator a separator e.g.: ' ', '+', ','
* @return self
*/
public OAuth2Options setScopeSeparator(String scopeSeparator) {
this.scopeSeparator = scopeSeparator;
return this;
}
/**
* Extra parameters to send to the provider
*
* @return a json representation of the parameters
*/
public JsonObject getExtraParameters() {
return extraParams;
}
/**
* Set extra parameters to be sent to the provider on each request
*
* @param extraParams a json representation of the parameters
* @return self
*/
public OAuth2Options setExtraParameters(JsonObject extraParams) {
this.extraParams = extraParams;
return this;
}
/**
* The provider token introspection resource path
*
* @return the resource path
*/
public String getIntrospectionPath() {
return introspectionPath;
}
/**
* Set the provider token introspection resource path
*
* @param introspectionPath a resource path
* @return self
*/
public OAuth2Options setIntrospectionPath(String introspectionPath) {
this.introspectionPath = introspectionPath;
return this;
}
/**
* Set the provider custom userInfo parameters to send when requesting them.
*
* @return a json representation of the extra parameters
*/
public JsonObject getUserInfoParameters() {
return userInfoParams;
}
/**
* Set custom parameters to be sent during the userInfo resource request
*
* @param userInfoParams json representation of the parameters
* @return self
*/
public OAuth2Options setUserInfoParameters(JsonObject userInfoParams) {
this.userInfoParams = userInfoParams;
return this;
}
public String getJwkPath() {
return jwkPath;
}
public OAuth2Options setJwkPath(String jwkPath) {
this.jwkPath = jwkPath;
return this;
}
public JWTOptions getJWTOptions() {
return jwtOptions;
}
public OAuth2Options setJWTOptions(JWTOptions jwtOptions) {
this.jwtOptions = jwtOptions;
return this;
}
public boolean isValidateIssuer() {
return validateIssuer;
}
public OAuth2Options setValidateIssuer(boolean validateIssuer) {
this.validateIssuer = validateIssuer;
return this;
}
public String getTenant() {
return tenant;
}
/**
* Sets an optional tenant. Tenants are used in some OpenID servers as placeholders for the URLs.
* The tenant should be set prior to any URL as it affects the way the URLs will be stored.
* <p>
* Some provders may name this differently, for example: `realm`.
*
* @param tenant the tenant/realm for this config.
* @return self
*/
public OAuth2Options setTenant(String tenant) {
this.tenant = tenant;
return this;
}
/**
* The provider supported grant types
*
* @return the supported grant types options
*/
public List<String> getSupportedGrantTypes() {
return supportedGrantTypes;
}
public OAuth2Options setSupportedGrantTypes(List<String> supportedGrantTypes) {
this.supportedGrantTypes = supportedGrantTypes;
return this;
}
public OAuth2Options addSupportedGrantType(String supportedGrantType) {
if (supportedGrantTypes == null) {
supportedGrantTypes = new ArrayList<>();
}
supportedGrantTypes.add(supportedGrantType);
return this;
}
public void replaceVariables(boolean strict) {
// strip trailing slashes if present
if (site != null && site.endsWith("/")) {
site = site.substring(0, site.length() - 1);
}
site = replaceVariables(site);
authorizationPath = replaceVariables(authorizationPath);
tokenPath = replaceVariables(tokenPath);
revocationPath = replaceVariables(revocationPath);
logoutPath = replaceVariables(logoutPath);
userInfoPath = replaceVariables(userInfoPath);
introspectionPath = replaceVariables(introspectionPath);
jwkPath = replaceVariables(jwkPath);
if (extraParams != null) {
for (Map.Entry<String, Object> kv : extraParams) {
Object v = kv.getValue();
if (v instanceof String) {
try {
kv.setValue(replaceVariables((String) v));
} catch (IllegalStateException e) {
// if we're strict the we assert that even the optional extra parameters must
// be updated with the variable value
if (strict) {
throw e;
}
}
}
}
}
}
private static final Pattern TENANT_PATTER = Pattern.compile("\\{(tenant|realm)}");
private String replaceVariables(String path) {
if (path != null) {
final Matcher matcher = TENANT_PATTER.matcher(path);
if (matcher.find()) {
if (tenant == null) {
throw new IllegalStateException("Configuration with placeholders require that \"tenant\" is prior set");
}
return matcher.replaceAll(tenant);
}
}
return path;
}
public void validate() throws IllegalStateException {
List<String> supportedGrantTypes = getSupportedGrantTypes();
if (supportedGrantTypes == null) {
// we default to AUTH_CODE and IMPLICIT as defined in the OpenID Connect spec
supportedGrantTypes = Arrays.asList(OAuth2FlowType.AUTH_CODE.getGrantType(), OAuth2FlowType.IMPLICIT.getGrantType());
}
for (OAuth2FlowType flow : OAuth2FlowType.values()) {
if (!supportedGrantTypes.contains(flow.getGrantType())) {
continue;
}
switch (flow) {
case AUTH_CODE:
case AUTH_JWT:
case AAD_OBO:
if (clientAssertionType == null) {
// not using client assertions
if (clientId == null) {
throw new IllegalStateException("Configuration missing. You need to specify [clientId]");
}
}
break;
case PASSWORD:
if (clientAssertionType == null) {
// not using client assertions
if (clientId == null) {
LOG.debug("If you are using Client Oauth2 Resource Owner flow. You need to specify [clientId]");
}
}
break;
}
}
}
public JsonObject toJson() {
final JsonObject json = new JsonObject();
OAuth2OptionsConverter.toJson(this, json);
return json;
}
@Override
public String toString() {
return toJson().encode();
}
public HttpClientOptions getHttpClientOptions() {
return httpClientOptions;
}
public OAuth2Options setHttpClientOptions(HttpClientOptions httpClientOptions) {
this.httpClientOptions = httpClientOptions;
return this;
}
public long getJwkMaxAgeInSeconds() {
return jwkMaxAge;
}
/**
* -1 means no rotation for JWKs
*
* @param jwkMaxAgeInSeconds timeout of JWKs rotation
*/
public void setJwkMaxAgeInSeconds(long jwkMaxAgeInSeconds) {
this.jwkMaxAge = jwkMaxAgeInSeconds;
}
public List<JsonObject> getJwks() {
return jwks;
}
/**
* Sets the initial local JWKs
* @param jwks a json array as defined in https://tools.ietf.org/html/rfc7517#section-5
* @return self
*/
@Fluent
public OAuth2Options setJwks(List<JsonObject> jwks) {
this.jwks = jwks;
return this;
}
/**
* Adds a local JWKs
* @param jwk a single keyas defined in https://tools.ietf.org/html/rfc7517#section-5
* @return self
*/
@Fluent
public OAuth2Options addJwk(JsonObject jwk) {
if (this.jwks == null) {
this.jwks = new ArrayList<>();
}
this.jwks.add(jwk);
return this;
}
}