Skip to content

Commit 4216072

Browse files
authored
ISSUE-30 Proxy support added for IMS and JWT authentication endpoint (#31)
* ISSUE-30 Proxy support added for IMS and JWT authentication endpoint
1 parent 38b7414 commit 4216072

File tree

15 files changed

+437
-30
lines changed

15 files changed

+437
-30
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
##
1212

1313
group=com.adobe.platform.streaming
14-
versionMain=0.0.6
14+
versionMain=0.0.9
1515
versionQualifier=
1616

1717
param_artifactory_user=

streaming-connect-common/src/main/java/com/adobe/platform/streaming/auth/impl/AuthProviderFactory.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,26 @@
2727
*/
2828
public final class AuthProviderFactory {
2929

30-
public static AuthProvider getAuthProvider(TokenType tokenType, Map<String, String> authProperties)
31-
throws AuthException {
30+
public static AuthProvider getAuthProvider(TokenType tokenType, Map<String, String> authProperties,
31+
AuthProxyConfiguration authProxyConfiguration) throws AuthException {
3232
if (MapUtils.isEmpty(authProperties)) {
3333
throw new AuthException("Invalid properties to get auth provider");
3434
}
3535

3636
switch (tokenType) {
3737
case ACCESS_TOKEN:
38-
return getIMSAuthProvider(authProperties);
38+
return getIMSAuthProvider(authProperties, authProxyConfiguration);
3939

4040
case JWT_TOKEN:
41-
return getJWTAuthProvider(authProperties);
41+
return getJWTAuthProvider(authProperties, authProxyConfiguration);
4242

4343
default:
4444
throw new AuthException("Invalid token type to get auth provider");
4545
}
4646
}
4747

48-
private static AuthProvider getIMSAuthProvider(Map<String, String> authProperties) {
48+
private static AuthProvider getIMSAuthProvider(Map<String, String> authProperties,
49+
AuthProxyConfiguration authProxyConfiguration) {
4950
String clientId = authProperties.get(AuthUtils.AUTH_CLIENT_ID);
5051
String clientCode = authProperties.get(AuthUtils.AUTH_CLIENT_CODE);
5152
String clientSecret = authProperties.get(AuthUtils.AUTH_CLIENT_SECRET);
@@ -56,11 +57,12 @@ private static AuthProvider getIMSAuthProvider(Map<String, String> authPropertie
5657

5758
String endpoint = authProperties.get(AuthUtils.AUTH_ENDPOINT);
5859
return StringUtils.isEmpty(endpoint) ?
59-
new IMSTokenProvider(clientId, clientCode, clientSecret) :
60-
new IMSTokenProvider(endpoint, clientId, clientCode, clientSecret);
60+
new IMSTokenProvider(clientId, clientCode, clientSecret, authProxyConfiguration) :
61+
new IMSTokenProvider(endpoint, clientId, clientCode, clientSecret, authProxyConfiguration);
6162
}
6263

63-
private static AuthProvider getJWTAuthProvider(Map<String, String> authProperties) {
64+
private static AuthProvider getJWTAuthProvider(Map<String, String> authProperties,
65+
AuthProxyConfiguration authProxyConfiguration) {
6466
final String clientId = authProperties.get(AuthUtils.AUTH_CLIENT_ID);
6567
final String clientSecret = authProperties.get(AuthUtils.AUTH_CLIENT_SECRET);
6668
final String imsOrgId = authProperties.get(AuthUtils.AUTH_IMS_ORG_ID);
@@ -74,8 +76,9 @@ private static AuthProvider getJWTAuthProvider(Map<String, String> authPropertie
7476

7577
String endpoint = authProperties.get(AuthUtils.AUTH_ENDPOINT);
7678
return StringUtils.isEmpty(endpoint) ?
77-
new JWTTokenProvider(clientId, clientSecret, imsOrgId, technicalAccountKey, filePath) :
78-
new JWTTokenProvider(endpoint, clientId, clientSecret, imsOrgId, technicalAccountKey, filePath);
79+
new JWTTokenProvider(clientId, clientSecret, imsOrgId, technicalAccountKey, filePath, authProxyConfiguration) :
80+
new JWTTokenProvider(endpoint, clientId, clientSecret, imsOrgId, technicalAccountKey, filePath,
81+
authProxyConfiguration);
7982
}
8083

8184
private AuthProviderFactory() {}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2021 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
package com.adobe.platform.streaming.auth.impl;
14+
15+
/**
16+
* @author Adobe Inc.
17+
*/
18+
public class AuthProxyConfiguration {
19+
20+
private final String proxyHost;
21+
private final int proxyPort;
22+
private final String proxyUsername;
23+
private final String proxyPassword;
24+
25+
public AuthProxyConfiguration(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) {
26+
this.proxyHost = proxyHost;
27+
this.proxyPort = proxyPort;
28+
this.proxyUsername = proxyUsername;
29+
this.proxyPassword = proxyPassword;
30+
}
31+
32+
public String getProxyHost() {
33+
return proxyHost;
34+
}
35+
36+
public int getProxyPort() {
37+
return proxyPort;
38+
}
39+
40+
public String getProxyUsername() {
41+
return proxyUsername;
42+
}
43+
44+
public String getProxyPassword() {
45+
return proxyPassword;
46+
}
47+
48+
public static AuthProxyConfiguration.AuthProxyConfigurationBuilder builder() {
49+
return new AuthProxyConfiguration.AuthProxyConfigurationBuilder();
50+
}
51+
52+
/**
53+
* @author Adobe Inc.
54+
*/
55+
public static class AuthProxyConfigurationBuilder {
56+
private String proxyHost;
57+
private int proxyPort;
58+
private String proxyUsername;
59+
private String proxyPassword;
60+
61+
AuthProxyConfigurationBuilder() {
62+
}
63+
64+
public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyHost(String proxyHost) {
65+
this.proxyHost = proxyHost;
66+
return this;
67+
}
68+
69+
public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyPort(int proxyPort) {
70+
this.proxyPort = proxyPort;
71+
return this;
72+
}
73+
74+
public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyUsername(String proxyUsername) {
75+
this.proxyUsername = proxyUsername;
76+
return this;
77+
}
78+
79+
public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyPassword(String proxyPassword) {
80+
this.proxyPassword = proxyPassword;
81+
return this;
82+
}
83+
84+
public AuthProxyConfiguration build() {
85+
return new AuthProxyConfiguration(this.proxyHost, this.proxyPort, this.proxyUsername, this.proxyPassword);
86+
}
87+
}
88+
89+
}

streaming-connect-common/src/main/java/com/adobe/platform/streaming/auth/impl/IMSTokenProvider.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,31 @@ public class IMSTokenProvider extends AbstractAuthProvider {
3535
private final String clientId;
3636
private final String clientCode;
3737
private final String clientSecret;
38+
private HttpProducer httpProducer;
3839

39-
IMSTokenProvider(String clientId, String clientCode, String clientSecret) {
40+
IMSTokenProvider(String clientId, String clientCode, String clientSecret,
41+
AuthProxyConfiguration authProxyConfiguration) {
4042
this.clientId = clientId;
4143
this.clientCode = clientCode;
4244
this.clientSecret = clientSecret;
45+
this.httpProducer = HttpProducer.newBuilder(endpoint)
46+
.withProxyHost(authProxyConfiguration.getProxyHost())
47+
.withProxyPort(authProxyConfiguration.getProxyPort())
48+
.withProxyUser(authProxyConfiguration.getProxyUsername())
49+
.withProxyPassword(authProxyConfiguration.getProxyPassword())
50+
.build();
4351
}
4452

45-
IMSTokenProvider(String endpoint, String clientId, String clientCode, String clientSecret) {
46-
this(clientId, clientCode, clientSecret);
53+
IMSTokenProvider(String endpoint, String clientId, String clientCode, String clientSecret,
54+
AuthProxyConfiguration authProxyConfiguration) {
55+
this(clientId, clientCode, clientSecret, authProxyConfiguration);
4756
this.endpoint = endpoint;
57+
this.httpProducer = HttpProducer.newBuilder(endpoint)
58+
.withProxyHost(authProxyConfiguration.getProxyHost())
59+
.withProxyPort(authProxyConfiguration.getProxyPort())
60+
.withProxyUser(authProxyConfiguration.getProxyUsername())
61+
.withProxyPassword(authProxyConfiguration.getProxyPassword())
62+
.build();
4863
}
4964

5065
@Override
@@ -57,7 +72,7 @@ protected TokenResponse getTokenResponse() throws AuthException {
5772
.append("&code=").append(clientCode);
5873

5974
try {
60-
return HttpProducer.newBuilder(endpoint).build().post(
75+
return httpProducer.post(
6176
IMS_ENDPOINT_PATH,
6277
params.toString().getBytes(),
6378
ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),

streaming-connect-common/src/main/java/com/adobe/platform/streaming/auth/impl/JWTTokenProvider.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,33 @@ public class JWTTokenProvider extends AbstractAuthProvider {
6464
private final String clientSecret;
6565
private final String keyPath;
6666
private String jwtToken;
67+
private HttpProducer httpProducer;
6768

6869
JWTTokenProvider(String endpoint, String clientId, String clientSecret, String imsOrgId, String technicalAccountKey,
69-
String keyPath) {
70-
this(clientId, clientSecret, imsOrgId, technicalAccountKey, keyPath);
70+
String keyPath, AuthProxyConfiguration authProxyConfiguration) {
71+
this(clientId, clientSecret, imsOrgId, technicalAccountKey, keyPath, authProxyConfiguration);
7172
this.endpoint = endpoint;
73+
this.httpProducer = HttpProducer.newBuilder(endpoint)
74+
.withProxyHost(authProxyConfiguration.getProxyHost())
75+
.withProxyPort(authProxyConfiguration.getProxyPort())
76+
.withProxyUser(authProxyConfiguration.getProxyUsername())
77+
.withProxyPassword(authProxyConfiguration.getProxyPassword())
78+
.build();
7279
}
7380

74-
JWTTokenProvider(String clientId, String clientSecret, String imsOrgId, String technicalAccountKey, String keyPath) {
81+
JWTTokenProvider(String clientId, String clientSecret, String imsOrgId, String technicalAccountKey, String keyPath,
82+
AuthProxyConfiguration authProxyConfiguration) {
7583
this.imsOrgId = imsOrgId;
7684
this.clientId = clientId;
7785
this.clientSecret = clientSecret;
7886
this.technicalAccountKey = technicalAccountKey;
7987
this.keyPath = keyPath;
88+
this.httpProducer = HttpProducer.newBuilder(endpoint)
89+
.withProxyHost(authProxyConfiguration.getProxyHost())
90+
.withProxyPort(authProxyConfiguration.getProxyPort())
91+
.withProxyUser(authProxyConfiguration.getProxyUsername())
92+
.withProxyPassword(authProxyConfiguration.getProxyPassword())
93+
.build();
8094
}
8195

8296
@Override
@@ -88,7 +102,7 @@ protected TokenResponse getTokenResponse() throws AuthException {
88102
.append("&jwt_token=").append(getJWTToken());
89103

90104
try {
91-
return HttpProducer.newBuilder(endpoint).build().post(
105+
return httpProducer.post(
92106
IMS_ENDPOINT_PATH,
93107
params.toString().getBytes(),
94108
ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),

streaming-connect-common/src/test/java/com/adobe/platform/streaming/auth/impl/IMSTokenProviderTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ class IMSTokenProviderTest {
2929

3030
@Test
3131
void testGetTokenInvalidClientId() {
32-
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, null, TEST_CLIENT_CODE, TEST_CLIENT_SECRET);
32+
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, null, TEST_CLIENT_CODE,
33+
TEST_CLIENT_SECRET, AuthProxyConfiguration.builder().build());
3334
assertThrows(AuthException.class, imsTokenProvider::getToken);
3435
}
3536

3637
@Test
3738
void testGetTokenInvalidClientCode() {
38-
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, null, TEST_CLIENT_SECRET);
39+
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, null,
40+
TEST_CLIENT_SECRET, AuthProxyConfiguration.builder().build());
3941
assertThrows(AuthException.class, imsTokenProvider::getToken);
4042
}
4143

4244
@Test
4345
void testGetTokenInvalidSecret() {
44-
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, TEST_CLIENT_CODE, null);
46+
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, TEST_CLIENT_CODE,
47+
null, AuthProxyConfiguration.builder().build());
4548
assertThrows(AuthException.class, imsTokenProvider::getToken);
4649
}
4750

@@ -51,7 +54,8 @@ void testGetToken() {
5154
TEST_ENDPOINT,
5255
TEST_CLIENT_ID,
5356
TEST_CLIENT_CODE,
54-
TEST_CLIENT_SECRET
57+
TEST_CLIENT_SECRET,
58+
AuthProxyConfiguration.builder().build()
5559
);
5660
assertThrows(AuthException.class, imsTokenProvider::getTokenResponse);
5761
}

streaming-connect-common/src/test/java/com/adobe/platform/streaming/auth/impl/JWTTokenProviderTest.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,36 @@ class JWTTokenProviderTest {
3333

3434
@Test
3535
void testGetTokenWithInvalidIMSOrg() {
36-
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, null, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_FILE_PATH);
36+
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, null, TEST_CLIENT, TEST_ACCOUNT_ID,
37+
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
3738
assertThrows(AuthException.class, tokenProvider::getToken);
3839
}
3940

4041
@Test
4142
void testGetTokenWithInvalidClientId() {
42-
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, null, TEST_ACCOUNT_ID, TEST_FILE_PATH);
43+
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, null, TEST_ACCOUNT_ID,
44+
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
4345
assertThrows(AuthException.class, tokenProvider::getToken);
4446
}
4547

4648
@Test
4749
void testGetTokenWithInvalidAccountId() {
48-
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, null, TEST_FILE_PATH);
50+
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, null,
51+
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
4952
assertThrows(AuthException.class, tokenProvider::getToken);
5053
}
5154

5255
@Test
5356
void testGetTokenInvalidPath() {
54-
JWTTokenProvider tokenProvider =
55-
new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_INVALID_FILE_PATH);
57+
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT,
58+
TEST_ACCOUNT_ID, TEST_INVALID_FILE_PATH, AuthProxyConfiguration.builder().build());
5659
assertThrows(AuthException.class, tokenProvider::getToken);
5760
}
5861

5962
@Test
6063
void testGetTokenValidPath() {
61-
JWTTokenProvider tokenProvider =
62-
new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_FILE_PATH);
64+
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT,
65+
TEST_ACCOUNT_ID, TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
6366
assertThrows(AuthException.class, tokenProvider::getToken);
6467
}
6568

streaming-connect-sink/src/main/java/com/adobe/platform/streaming/sink/AbstractAEPPublisher.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.adobe.platform.streaming.auth.AuthUtils;
1919
import com.adobe.platform.streaming.auth.TokenType;
2020
import com.adobe.platform.streaming.auth.impl.AuthProviderFactory;
21+
import com.adobe.platform.streaming.auth.impl.AuthProxyConfiguration;
2122
import com.adobe.platform.streaming.http.HttpProducer;
2223
import com.adobe.platform.streaming.sink.utils.SinkUtils;
2324
import com.google.common.collect.ImmutableMap;
@@ -124,6 +125,12 @@ private AuthProvider getIMSTokenProvider(Map<String, String> props) throws AuthE
124125
.put(AuthUtils.AUTH_CLIENT_ID, props.get(AEP_CONNECTION_AUTH_CLIENT_ID))
125126
.put(AuthUtils.AUTH_CLIENT_CODE, props.get(AEP_CONNECTION_AUTH_CLIENT_CODE))
126127
.put(AuthUtils.AUTH_CLIENT_SECRET, props.get(AEP_CONNECTION_AUTH_CLIENT_SECRET))
128+
.put(AuthUtils.AUTH_ENDPOINT, props.get(AEP_CONNECTION_AUTH_ENDPOINT))
129+
.build(), AuthProxyConfiguration.builder()
130+
.proxyHost(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_HOST, null))
131+
.proxyPort(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PORT, 443))
132+
.proxyUsername(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_USER, null))
133+
.proxyPassword(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PASSWORD, null))
127134
.build());
128135
}
129136

@@ -135,6 +142,11 @@ private AuthProvider getJWTTokenProvider(Map<String, String> props) throws AuthE
135142
.put(AuthUtils.AUTH_IMS_ORG_ID, props.get(AEP_CONNECTION_AUTH_IMS_ORG))
136143
.put(AuthUtils.AUTH_TECHNICAL_ACCOUNT_ID, props.get(AEP_CONNECTION_AUTH_ACCOUNT_KEY))
137144
.put(AuthUtils.AUTH_PRIVATE_KEY_FILE_PATH, props.get(AEP_CONNECTION_AUTH_FILE_PATH))
145+
.build(), AuthProxyConfiguration.builder()
146+
.proxyHost(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_HOST, null))
147+
.proxyPort(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PORT, 443))
148+
.proxyUsername(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_USER, null))
149+
.proxyPassword(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PASSWORD, null))
138150
.build());
139151
}
140152

0 commit comments

Comments
 (0)