Skip to content

Commit 60e9746

Browse files
authored
Merge pull request #248 from rma-bryson/CWMS-1875_Porting_Cwbi_Auth_Module
CWMS-1875 - Porting over cwbi-auth-http-client module from cumulus repo
2 parents dfbc72b + 73335f6 commit 60e9746

30 files changed

Lines changed: 1669 additions & 0 deletions

cwbi-auth-http-client/build.gradle

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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+
25+
plugins {
26+
id "cwms-data-api-client.java-conventions"
27+
id "cwms-data-api-client.deps-conventions"
28+
id "cwms-data-api-client.publishing-conventions"
29+
}
30+
31+
dependencies {
32+
api(project(":cwms-http-client"))
33+
34+
implementation(libs.jackson.databind)
35+
36+
testImplementation(testFixtures(project(":cwms-http-client")))
37+
testImplementation(libs.junit.api)
38+
testImplementation(platform(libs.okhttp.bom))
39+
testImplementation(libs.okhttp)
40+
testImplementation(libs.okhttp.mockwebserver)
41+
testRuntimeOnly(libs.junit.engine)
42+
}
43+
44+
45+
publishing {
46+
publications {
47+
maven(MavenPublication) {
48+
artifactId = "cwbi-auth-http-client"
49+
from components.java
50+
}
51+
}
52+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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 hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
27+
import java.io.IOException;
28+
import java.security.KeyManagementException;
29+
import java.security.NoSuchAlgorithmException;
30+
import java.util.List;
31+
import javax.net.ssl.KeyManager;
32+
import javax.net.ssl.SSLContext;
33+
import javax.net.ssl.SSLSocketFactory;
34+
import javax.net.ssl.TrustManager;
35+
36+
public final class CwbiAuthSslSocketFactory {
37+
38+
private CwbiAuthSslSocketFactory() {
39+
throw new AssertionError("Utility class");
40+
}
41+
42+
/**
43+
* Builds SSLSocketFactory configured for CWBI Auth and specified KeyManagers.
44+
* @param keyManagers - KeyManager list
45+
* @return SSLSocketFactory
46+
* @throws IOException - thrown if building SSLSocketFactory failed
47+
*/
48+
public static SSLSocketFactory buildSSLSocketFactory(List<KeyManager> keyManagers) throws IOException {
49+
try {
50+
SSLContext sc = SSLContext.getInstance("TLS");
51+
sc.init(keyManagers.toArray(new KeyManager[]{}),
52+
new TrustManager[] {CwbiAuthTrustManager.getTrustManager()}, null);
53+
return sc.getSocketFactory();
54+
} catch (NoSuchAlgorithmException | KeyManagementException e) {
55+
throw new IOException(e);
56+
}
57+
}
58+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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 javax.net.ssl.SSLSocketFactory;
28+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
29+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2TokenProvider;
30+
31+
public final class CwbiAuthTokenProvider implements OAuth2TokenProvider {
32+
33+
private OAuth2Token oauth2Token;
34+
private final String url;
35+
private final String clientId;
36+
private final SSLSocketFactory sslSocketFactory;
37+
38+
/**
39+
* Provider for OAuth2Tokens.
40+
*
41+
* @param url - URL we are fetching token from
42+
* @param clientId - client name
43+
* @param sslSocketFactory - ssl socket factory
44+
*/
45+
public CwbiAuthTokenProvider(String url, String clientId, SSLSocketFactory sslSocketFactory) {
46+
this.url = url;
47+
this.clientId = clientId;
48+
this.sslSocketFactory = sslSocketFactory;
49+
}
50+
51+
@Override
52+
public OAuth2Token getToken() throws IOException {
53+
if (oauth2Token == null) {
54+
oauth2Token = newToken();
55+
}
56+
return oauth2Token;
57+
}
58+
59+
@Override
60+
public OAuth2Token newToken() throws IOException {
61+
return new DirectGrantX509TokenRequestBuilder()
62+
.withSSlSocketFactory(sslSocketFactory)
63+
.withUrl(url)
64+
.withClientId(clientId)
65+
.fetchToken();
66+
}
67+
68+
@Override
69+
public OAuth2Token refreshToken() throws IOException {
70+
OAuth2Token token = new RefreshTokenRequestBuilder()
71+
.withRefreshToken(oauth2Token.getRefreshToken())
72+
.withUrl(url)
73+
.withClientId(clientId)
74+
.fetchToken();
75+
oauth2Token = token;
76+
return token;
77+
}
78+
79+
//package scoped for testing
80+
String getUrl() {
81+
return url;
82+
}
83+
84+
//package scoped for testing
85+
String getClientId() {
86+
return clientId;
87+
}
88+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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+
27+
import static hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager.TOKEN_URL;
28+
29+
import java.io.IOException;
30+
import java.util.Collections;
31+
import java.util.Objects;
32+
import javax.net.ssl.KeyManager;
33+
import javax.net.ssl.SSLSocketFactory;
34+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2TokenProvider;
35+
36+
public final class CwbiAuthUtil {
37+
38+
private CwbiAuthUtil() {
39+
throw new AssertionError("Utility class");
40+
}
41+
42+
/**
43+
* Builds CumulusTokenProvider for retrieving and refreshing tokens for cumulus authentication.
44+
* @param keyManager - KeyManager for client
45+
* @return OAuth2TokenProvider - CumulusTokenProvider
46+
* @throws IOException - thrown if failed to build CumulusTokenProvider
47+
*/
48+
public static OAuth2TokenProvider buildCwbiAuthTokenProvider(String clientId, KeyManager keyManager) throws IOException {
49+
SSLSocketFactory sslSocketFactory = CwbiAuthSslSocketFactory.buildSSLSocketFactory(
50+
Collections.singletonList(Objects.requireNonNull(keyManager, "Missing required KeyManager")));
51+
return new CwbiAuthTokenProvider(TOKEN_URL, clientId, sslSocketFactory);
52+
}
53+
54+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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 hec.army.usace.hec.cwbi.auth.http.client.trustmanagers.CwbiAuthTrustManager;
27+
import mil.army.usace.hec.cwms.http.client.ApiConnectionInfoBuilder;
28+
import mil.army.usace.hec.cwms.http.client.HttpRequestBuilderImpl;
29+
import mil.army.usace.hec.cwms.http.client.HttpRequestResponse;
30+
import mil.army.usace.hec.cwms.http.client.SslSocketData;
31+
import mil.army.usace.hec.cwms.http.client.auth.OAuth2Token;
32+
import mil.army.usace.hec.cwms.http.client.request.HttpRequestExecutor;
33+
34+
import javax.net.ssl.SSLSocketFactory;
35+
import java.io.IOException;
36+
import java.util.Objects;
37+
38+
public final class DirectGrantX509TokenRequestBuilder implements DirectGrantX509TokenRequestFluentBuilder {
39+
40+
private SslSocketData sslSocketData;
41+
42+
@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()
55+
.addPassword("")
56+
.addGrantType("password")
57+
.addScopes("openid", "profile")
58+
.addClientId(getClientId())
59+
.addUsername("")
60+
.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+
}
72+
}
73+
return retVal;
74+
}
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 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 javax.net.ssl.SSLSocketFactory;
27+
28+
public interface DirectGrantX509TokenRequestFluentBuilder {
29+
30+
TokenRequestFluentBuilder withSSlSocketFactory(SSLSocketFactory sslSocketFactory);
31+
}

0 commit comments

Comments
 (0)