|
4 | 4 |
|
5 | 5 | package com.marklogic.client.impl;
|
6 | 6 |
|
7 |
| -import java.io.IOException; |
8 |
| -import java.time.Instant; |
9 |
| -import java.util.concurrent.Executors; |
10 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
11 |
| - |
12 | 7 | import com.marklogic.client.DatabaseClientFactory.SAMLAuthContext.AuthorizerCallback;
|
13 | 8 | import com.marklogic.client.DatabaseClientFactory.SAMLAuthContext.ExpiringSAMLAuth;
|
14 | 9 | import com.marklogic.client.DatabaseClientFactory.SAMLAuthContext.RenewerCallback;
|
15 |
| - |
16 | 10 | import okhttp3.Interceptor;
|
17 | 11 | import okhttp3.Request;
|
18 | 12 | import okhttp3.Response;
|
19 | 13 |
|
| 14 | +import java.io.IOException; |
| 15 | +import java.time.Instant; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 18 | + |
20 | 19 | public class HTTPSamlAuthInterceptor implements Interceptor {
|
21 | 20 |
|
22 |
| - private String authorizationTokenValue; |
23 |
| - private AuthorizerCallback authorizer; |
| 21 | + private final AuthorizerCallback authorizer; |
| 22 | + private final RenewerCallback renewer; |
| 23 | + |
| 24 | + private String authorizationTokenValue; |
24 | 25 | private ExpiringSAMLAuth expiringSAMLAuth;
|
25 | 26 | private long threshold;
|
26 |
| - private RenewerCallback renewer; |
27 | 27 | private AtomicBoolean isCallbackExecuting;
|
28 | 28 |
|
29 | 29 | public HTTPSamlAuthInterceptor(String authToken) {
|
30 |
| - this.authorizationTokenValue = authToken; |
| 30 | + this.authorizationTokenValue = authToken; |
| 31 | + this.authorizer = null; |
| 32 | + this.renewer = null; |
31 | 33 | }
|
32 | 34 |
|
33 | 35 | public HTTPSamlAuthInterceptor(AuthorizerCallback authorizer) {
|
34 | 36 | this.authorizer = authorizer;
|
| 37 | + this.renewer = null; |
35 | 38 | }
|
36 | 39 |
|
37 | 40 | public HTTPSamlAuthInterceptor(ExpiringSAMLAuth authorization, RenewerCallback renew) {
|
38 | 41 | expiringSAMLAuth = authorization;
|
39 | 42 | renewer = renew;
|
40 | 43 | isCallbackExecuting = new AtomicBoolean(false);
|
| 44 | + this.authorizer = null; |
41 | 45 | }
|
42 | 46 |
|
43 | 47 | @Override
|
44 | 48 | public Response intercept(Chain chain) throws IOException {
|
45 |
| - Request request = chain.request(); |
46 |
| - if (authorizer != null) { |
47 |
| - if(expiringSAMLAuth == null) { |
48 |
| - authorizeCallbackWrapper(null); |
49 |
| - } else if(threshold<=Instant.now().getEpochSecond()){ |
50 |
| - authorizeCallbackWrapper(expiringSAMLAuth.getExpiry()); |
51 |
| - } |
52 |
| - } else if (renewer != null && threshold <= Instant.now().getEpochSecond() && isCallbackExecuting.compareAndSet(false, true)) { |
53 |
| - RenewCallbackWrapper renewCallbackWrapper = new RenewCallbackWrapper(expiringSAMLAuth); |
54 |
| - Executors.defaultThreadFactory().newThread(renewCallbackWrapper).start(); |
55 |
| - } |
56 |
| - String samlHeaderValue = RESTServices.AUTHORIZATION_TYPE_SAML + " " + RESTServices.AUTHORIZATION_PARAM_TOKEN |
57 |
| - + "=" + authorizationTokenValue; |
58 |
| - Request authenticatedRequest = request.newBuilder().header(RESTServices.HEADER_AUTHORIZATION, samlHeaderValue) |
59 |
| - .build(); |
60 |
| - return chain.proceed(authenticatedRequest); |
| 49 | + if (authorizer != null) { |
| 50 | + authorizeRequest(); |
| 51 | + } else if (renewer != null && threshold <= Instant.now().getEpochSecond() && isCallbackExecuting.compareAndSet(false, true)) { |
| 52 | + RenewCallbackWrapper renewCallbackWrapper = new RenewCallbackWrapper(expiringSAMLAuth); |
| 53 | + Executors.defaultThreadFactory().newThread(renewCallbackWrapper).start(); |
| 54 | + } |
| 55 | + |
| 56 | + Request authenticatedRequest = chain.request().newBuilder() |
| 57 | + .header(RESTServices.HEADER_AUTHORIZATION, buildSamlHeader()) |
| 58 | + .build(); |
| 59 | + |
| 60 | + return chain.proceed(authenticatedRequest); |
61 | 61 | }
|
62 | 62 |
|
| 63 | + private synchronized void authorizeRequest() { |
| 64 | + if (expiringSAMLAuth == null) { |
| 65 | + authorizeCallbackWrapper(null); |
| 66 | + } else if (threshold <= Instant.now().getEpochSecond()) { |
| 67 | + authorizeCallbackWrapper(expiringSAMLAuth.getExpiry()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private synchronized String buildSamlHeader() { |
| 72 | + return String.format("%s %s=%s", |
| 73 | + RESTServices.AUTHORIZATION_TYPE_SAML, |
| 74 | + RESTServices.AUTHORIZATION_PARAM_TOKEN, |
| 75 | + this.authorizationTokenValue); |
| 76 | + } |
| 77 | + |
63 | 78 | private synchronized void authorizeCallbackWrapper(Instant expiry) {
|
64 | 79 |
|
65 | 80 | if(expiry == null && expiringSAMLAuth != null) {
|
|
0 commit comments