Skip to content

Commit 82d5ab8

Browse files
committed
Override URL and SSL data for DirectGrant only.
1 parent 14b672e commit 82d5ab8

6 files changed

Lines changed: 41 additions & 11 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@
2424
package hec.army.usace.hec.cwbi.auth.http.client;
2525

2626
import hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
27+
28+
import java.io.IOException;
2729
import java.util.Objects;
30+
2831
import javax.net.ssl.SSLSocketFactory;
2932
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
3033
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfoBuilder;
3134
import mil.army.usace.hec.cwms.http.client.SslSocketData;
35+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
3236

37+
/**
38+
* Suitable only for CWBI Keycloaks direct grant setup.
39+
*/
3340
public final class CwbiAuthTokenProvider extends CwbiAuthTokenProviderBase {
3441

3542
private final SSLSocketFactory sslSocketFactory;
@@ -53,4 +60,19 @@ ApiConnectionInfo getUrl() {
5360
.build();
5461
}
5562

63+
@Override
64+
public ApiConnectionInfo getAuthUrl() {
65+
// This is specific to CWBI Direct Grant so this replacement as-is is fine
66+
return new ApiConnectionInfoBuilder(this.tokenUrl.getApiRoot().replace("identity", "identityc"))
67+
.withSslSocketData(new SslSocketData(sslSocketFactory, CwbiAuthTrustManager.getTrustManager()))
68+
.build();
69+
}
70+
71+
@Override
72+
public OAuth2Token newToken() throws IOException {
73+
return new DirectGrantX509TokenRequestBuilder()
74+
.withTokenUrl(getAuthUrl())
75+
.buildRequest().withClientId(clientId)
76+
.fetchToken();
77+
}
5678
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ public synchronized OAuth2Token getToken() throws IOException {
4545
return token;
4646
}
4747

48-
@Override
49-
public OAuth2Token newToken() throws IOException {
50-
return new DirectGrantX509TokenRequestBuilder()
51-
.withUrl(tokenUrl)
52-
.withClientId(clientId)
53-
.fetchToken();
54-
}
55-
5648
@Override
5749
public synchronized OAuth2Token refreshToken() throws IOException {
5850
OAuth2Token newToken = new RefreshTokenRequestBuilder()

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

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

26+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
2627
import mil.army.usace.hec.cwms.http.client.HttpRequestBuilderImpl;
2728
import mil.army.usace.hec.cwms.http.client.HttpRequestResponse;
2829
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
@@ -43,7 +44,7 @@ OAuth2Token retrieveToken() throws IOException {
4344
.addUsername("")
4445
.buildEncodedString();
4546
HttpRequestExecutor executor =
46-
new HttpRequestBuilderImpl(getUrl())
47+
new HttpRequestBuilderImpl(getTokenUrl())
4748
.post()
4849
.withBody(formBody)
4950
.withMediaType(MEDIA_TYPE);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,14 @@ public OAuth2Token newToken() throws IOException {
113113

114114
}
115115

116+
@Override
117+
public ApiConnectionInfo getAuthUrl() {
118+
return authUrl;
119+
}
120+
121+
@Override
122+
public ApiConnectionInfo getTokenUrl() {
123+
return tokenUrl;
124+
}
125+
116126
}

cwms-http-client/src/main/java/mil/army/usace/hec/cwms/http/client/SslSocketData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public SslSocketData(SSLSocketFactory sslSocketFactory, X509TrustManager x509Tru
3838
this.x509TrustManager = Objects.requireNonNull(x509TrustManager, "Missing required X509TrustManager");
3939
}
4040

41-
SSLSocketFactory getSslSocketFactory() {
41+
public SSLSocketFactory getSslSocketFactory() {
4242
return sslSocketFactory;
4343
}
4444

45-
X509TrustManager getX509TrustManager() {
45+
public X509TrustManager getX509TrustManager() {
4646
return x509TrustManager;
4747
}
4848
}

cwms-http-client/src/main/java/mil/army/usace/hec/cwms/http/client/auth/OAuth2TokenProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.net.URI;
2929
import java.util.function.Consumer;
3030

31+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfo;
32+
3133
/**
3234
*
3335
* TODO: needs additional support for alternative flows. deciding if attempting to
@@ -55,4 +57,7 @@ default Consumer<URI> getAuthCallback() {
5557
default void setAuthCallback(Consumer<URI> authCallback) {
5658
/** default do nothing... for now */
5759
}
60+
61+
ApiConnectionInfo getAuthUrl();
62+
ApiConnectionInfo getTokenUrl();
5863
}

0 commit comments

Comments
 (0)