Skip to content

Commit 6800475

Browse files
authored
CWMS-2055 - Updates to allow for a token provider with a discovered token url that the client implements (#263)
Updates to allow for a token provider with a discovered token url that the client implements Update TokenUrlDiscoveryService to be an interface Update TokenUrlDiscoveryService to return ApiConnectionInfo. Refactors code to pass ApiConnectionInfo for token url. This moved responsibility of creation of ssl socket data to client for discovered token url.
1 parent 8aa1c8d commit 6800475

15 files changed

+512
-208
lines changed
Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2024 Hydrologic Engineering Center
4+
* Copyright (c) 2025 Hydrologic Engineering Center
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -23,18 +23,17 @@
2323
*/
2424
package hec.army.usace.hec.cwbi.auth.http.client;
2525

26-
import java.io.IOException;
26+
import hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
2727
import java.util.Objects;
2828
import javax.net.ssl.SSLSocketFactory;
29-
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
30-
import mil.army.usace.hec.cwms.http.client.auth.OAuth2TokenProvider;
29+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
30+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfoBuilder;
31+
import mil.army.usace.hec.cwms.http.client.SslSocketData;
3132

32-
public final class CwbiAuthTokenProvider implements OAuth2TokenProvider {
33+
public final class CwbiAuthTokenProvider extends CwbiAuthTokenProviderBase {
3334

34-
private OAuth2Token oauth2Token;
35-
private final String url;
36-
private final String clientId;
3735
private final SSLSocketFactory sslSocketFactory;
36+
private final String url;
3837

3938
/**
4039
* Provider for OAuth2Tokens.
@@ -44,52 +43,16 @@ public final class CwbiAuthTokenProvider implements OAuth2TokenProvider {
4443
* @param sslSocketFactory - ssl socket factory
4544
*/
4645
public CwbiAuthTokenProvider(String tokenUrl, String clientId, SSLSocketFactory sslSocketFactory) {
46+
super(clientId);
47+
this.sslSocketFactory = Objects.requireNonNull(sslSocketFactory, "Missing required sslSocketFactory");
4748
this.url = Objects.requireNonNull(tokenUrl, "Missing required tokenUrl");
48-
this.clientId = Objects.requireNonNull(clientId, "Missing required clientId");
49-
this.sslSocketFactory =Objects.requireNonNull(sslSocketFactory, "Missing required KeyManager");
50-
}
51-
52-
@Override
53-
public synchronized void clear() {
54-
oauth2Token = null;
55-
}
56-
57-
@Override
58-
public synchronized OAuth2Token getToken() throws IOException {
59-
if (oauth2Token == null) {
60-
oauth2Token = newToken();
61-
}
62-
return oauth2Token;
63-
}
64-
65-
@Override
66-
public OAuth2Token newToken() throws IOException {
67-
return new DirectGrantX509TokenRequestBuilder()
68-
.withSSlSocketFactory(sslSocketFactory)
69-
.withUrl(url)
70-
.withClientId(clientId)
71-
.fetchToken();
7249
}
7350

7451
@Override
75-
public synchronized OAuth2Token refreshToken() throws IOException {
76-
OAuth2Token token = new RefreshTokenRequestBuilder()
77-
.withSSlSocketFactory(sslSocketFactory)
78-
.withRefreshToken(oauth2Token.getRefreshToken())
79-
.withUrl(url)
80-
.withClientId(clientId)
81-
.fetchToken();
82-
oauth2Token = token;
83-
return token;
52+
ApiConnectionInfo getUrl() {
53+
return new ApiConnectionInfoBuilder(url)
54+
.withSslSocketData(new SslSocketData(sslSocketFactory, CwbiAuthTrustManager.getTrustManager()))
55+
.build();
8456
}
8557

86-
//package scoped for testing
87-
String getUrl() {
88-
return url;
89-
}
90-
91-
//package scoped for testing
92-
String getClientId() {
93-
return clientId;
94-
}
9558
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Hydrologic Engineering Center
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package hec.army.usace.hec.cwbi.auth.http.client;
25+
26+
import java.io.IOException;
27+
import java.util.Objects;
28+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
29+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
30+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2TokenProvider;
31+
32+
abstract class CwbiAuthTokenProviderBase implements OAuth2TokenProvider {
33+
protected OAuth2Token oauth2Token;
34+
protected final String clientId;
35+
36+
protected CwbiAuthTokenProviderBase(String clientId) {
37+
this.clientId = Objects.requireNonNull(clientId, "Missing required clientId");
38+
}
39+
40+
abstract ApiConnectionInfo getUrl();
41+
42+
@Override
43+
public synchronized void clear() {
44+
oauth2Token = null;
45+
}
46+
47+
@Override
48+
public synchronized OAuth2Token getToken() throws IOException {
49+
if (oauth2Token == null) {
50+
oauth2Token = newToken();
51+
}
52+
return oauth2Token;
53+
}
54+
55+
@Override
56+
public OAuth2Token newToken() throws IOException {
57+
return new DirectGrantX509TokenRequestBuilder()
58+
.withUrl(getUrl())
59+
.withClientId(clientId)
60+
.fetchToken();
61+
}
62+
63+
@Override
64+
public synchronized OAuth2Token refreshToken() throws IOException {
65+
OAuth2Token token = new RefreshTokenRequestBuilder()
66+
.withRefreshToken(oauth2Token.getRefreshToken())
67+
.withUrl(getUrl())
68+
.withClientId(clientId)
69+
.fetchToken();
70+
oauth2Token = token;
71+
return token;
72+
}
73+
74+
//package scoped for testing
75+
String getClientId() {
76+
return clientId;
77+
}
78+
}

cwbi-auth-http-client/src/main/java/hec/army/usace/hec/cwbi/auth/http/client/DirectGrantX509TokenRequestBuilder.java

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,36 @@
2323
*/
2424
package hec.army.usace.hec.cwbi.auth.http.client;
2525

26-
import hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
27-
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfoBuilder;
2826
import mil.army.usace.hec.cwms.http.client.HttpRequestBuilderImpl;
2927
import mil.army.usace.hec.cwms.http.client.HttpRequestResponse;
30-
import mil.army.usace.hec.cwms.http.client.SslSocketData;
3128
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
3229
import mil.army.usace.hec.cwms.http.client.request.HttpRequestExecutor;
3330

34-
import javax.net.ssl.SSLSocketFactory;
3531
import java.io.IOException;
36-
import java.util.Objects;
3732

38-
public final class DirectGrantX509TokenRequestBuilder implements DirectGrantX509TokenRequestFluentBuilder {
39-
40-
private SslSocketData sslSocketData;
33+
public final class DirectGrantX509TokenRequestBuilder extends TokenRequestBuilder {
4134

4235
@Override
43-
public TokenRequestFluentBuilder withSSlSocketFactory(SSLSocketFactory sslSocketFactory) {
44-
this.sslSocketData = new SslSocketData(Objects.requireNonNull(sslSocketFactory, "Missing required SSLSocketFactory"),
45-
CwbiAuthTrustManager.getTrustManager());
46-
return new TokenRequestBuilderImpl();
47-
}
48-
49-
private class TokenRequestBuilderImpl extends TokenRequestBuilder {
50-
51-
@Override
52-
OAuth2Token retrieveToken() throws IOException {
53-
OAuth2Token retVal = null;
54-
String formBody = new UrlEncodedFormData()
36+
OAuth2Token retrieveToken() throws IOException {
37+
OAuth2Token retVal = null;
38+
String formBody = new UrlEncodedFormData()
5539
.addPassword("")
5640
.addGrantType("password")
5741
.addScopes("openid", "profile")
5842
.addClientId(getClientId())
5943
.addUsername("")
6044
.buildEncodedString();
61-
HttpRequestExecutor executor =
62-
new HttpRequestBuilderImpl(new ApiConnectionInfoBuilder(getUrl())
63-
.withSslSocketData(sslSocketData).build())
64-
.post()
65-
.withBody(formBody)
66-
.withMediaType(MEDIA_TYPE);
67-
try (HttpRequestResponse response = executor.execute()) {
68-
String body = response.getBody();
69-
if (body != null) {
70-
retVal = OAuth2ObjectMapper.mapJsonToObject(body, OAuth2Token.class);
71-
}
45+
HttpRequestExecutor executor =
46+
new HttpRequestBuilderImpl(getUrl())
47+
.post()
48+
.withBody(formBody)
49+
.withMediaType(MEDIA_TYPE);
50+
try (HttpRequestResponse response = executor.execute()) {
51+
String body = response.getBody();
52+
if (body != null) {
53+
retVal = OAuth2ObjectMapper.mapJsonToObject(body, OAuth2Token.class);
7254
}
73-
return retVal;
7455
}
56+
return retVal;
7557
}
7658
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2025 Hydrologic Engineering Center
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package hec.army.usace.hec.cwbi.auth.http.client;
25+
26+
import java.util.Objects;
27+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
28+
29+
public final class DiscoveredCwbiAuthTokenProvider extends CwbiAuthTokenProviderBase
30+
{
31+
private final TokenUrlDiscoveryService tokenUrlDiscoveryService;
32+
private ApiConnectionInfo url;
33+
34+
public DiscoveredCwbiAuthTokenProvider(String clientId, TokenUrlDiscoveryService tokenUrlDiscoveryService)
35+
{
36+
super(clientId);
37+
this.tokenUrlDiscoveryService = Objects.requireNonNull(tokenUrlDiscoveryService, "Missing required tokenUrlDiscoveryService");
38+
}
39+
40+
@Override
41+
synchronized ApiConnectionInfo getUrl()
42+
{
43+
if(url == null)
44+
{
45+
url = tokenUrlDiscoveryService.discoverTokenUrl();
46+
}
47+
return url;
48+
}
49+
}

cwbi-auth-http-client/src/main/java/hec/army/usace/hec/cwbi/auth/http/client/RefreshTokenRequestBuilder.java

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package hec.army.usace.hec.cwbi.auth.http.client;
22

3-
import hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
4-
import java.util.Optional;
5-
import javax.net.ssl.SSLSocketFactory;
6-
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfoBuilder;
3+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
74
import mil.army.usace.hec.cwms.http.client.HttpRequestBuilderImpl;
85
import mil.army.usace.hec.cwms.http.client.HttpRequestResponse;
9-
import mil.army.usace.hec.cwms.http.client.SslSocketData;
106
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
117
import mil.army.usace.hec.cwms.http.client.request.HttpRequestExecutor;
128

@@ -16,7 +12,6 @@
1612
public final class RefreshTokenRequestBuilder implements RefreshTokenRequestFluentBuilder {
1713

1814
private String refreshToken;
19-
private SSLSocketFactory sslSocketFactory;
2015

2116
/**
2217
* Retrieved token via a refresh token.
@@ -29,32 +24,13 @@ public TokenRequestFluentBuilder withRefreshToken(String refreshToken) {
2924
return new RefreshTokenRequestExecutor();
3025
}
3126

32-
/**
33-
* Set the SSLSocketFactory for the refresh request should it be needed.
34-
* @param sslSocketFactory - SSLSocketFactory to use
35-
* @return Builder for http request
36-
*/
37-
@Override
38-
public RefreshTokenRequestBuilder withSSlSocketFactory(SSLSocketFactory sslSocketFactory) {
39-
this.sslSocketFactory = sslSocketFactory;
40-
return this;
41-
}
42-
43-
//package scoped for testing
44-
Optional<SSLSocketFactory> getSslSocketFactory() {
45-
return Optional.ofNullable(sslSocketFactory);
46-
}
47-
48-
private class RefreshTokenRequestExecutor extends TokenRequestBuilder {
27+
class RefreshTokenRequestExecutor extends TokenRequestBuilder {
4928

5029
@Override
5130
OAuth2Token retrieveToken() throws IOException {
5231
OAuth2Token retVal = null;
53-
SslSocketData sslSocketData = getSslSocketFactory().map(sf -> new SslSocketData(sf, CwbiAuthTrustManager.getTrustManager()))
54-
.orElse(null);
5532
HttpRequestExecutor executor =
56-
new HttpRequestBuilderImpl(new ApiConnectionInfoBuilder(getUrl())
57-
.withSslSocketData(sslSocketData).build())
33+
new HttpRequestBuilderImpl(getUrl())
5834
.post()
5935
.withBody(new UrlEncodedFormData()
6036
.addRefreshToken(refreshToken)
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package hec.army.usace.hec.cwbi.auth.http.client;
22

3-
import javax.net.ssl.SSLSocketFactory;
4-
53
public interface RefreshTokenRequestFluentBuilder {
64
TokenRequestFluentBuilder withRefreshToken(String refreshToken);
7-
RefreshTokenRequestBuilder withSSlSocketFactory(SSLSocketFactory sslSocketFactory);
85
}

cwbi-auth-http-client/src/main/java/hec/army/usace/hec/cwbi/auth/http/client/TokenRequestBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@
2525

2626
import java.io.IOException;
2727
import java.util.Objects;
28+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
2829
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
2930

3031
abstract class TokenRequestBuilder implements TokenRequestFluentBuilder {
3132

3233
static final String MEDIA_TYPE = "application/x-www-form-urlencoded";
33-
private String url;
34+
private ApiConnectionInfo url;
3435
private String clientId;
3536

3637
abstract OAuth2Token retrieveToken() throws IOException;
3738

38-
String getUrl() {
39+
ApiConnectionInfo getUrl() {
3940
return url;
4041
}
4142

@@ -44,7 +45,7 @@ String getClientId() {
4445
}
4546

4647
@Override
47-
public RequestClientId withUrl(String url) {
48+
public RequestClientId withUrl(ApiConnectionInfo url) {
4849
this.url = Objects.requireNonNull(url, "Missing required URL");
4950
return new RequestClientIdImpl();
5051
}

cwbi-auth-http-client/src/main/java/hec/army/usace/hec/cwbi/auth/http/client/TokenRequestFluentBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*/
2424
package hec.army.usace.hec.cwbi.auth.http.client;
2525

26+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
27+
2628
public interface TokenRequestFluentBuilder {
2729

28-
RequestClientId withUrl(String url);
30+
RequestClientId withUrl(ApiConnectionInfo url);
2931
}

0 commit comments

Comments
 (0)