Skip to content

Commit

Permalink
ISSUE-30 Proxy support added for IMS and JWT authentication endpoint (#…
Browse files Browse the repository at this point in the history
…31)

* ISSUE-30 Proxy support added for IMS and JWT authentication endpoint
  • Loading branch information
vivetiwa committed Nov 2, 2021
1 parent 38b7414 commit 4216072
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 30 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
##

group=com.adobe.platform.streaming
versionMain=0.0.6
versionMain=0.0.9
versionQualifier=

param_artifactory_user=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@
*/
public final class AuthProviderFactory {

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

switch (tokenType) {
case ACCESS_TOKEN:
return getIMSAuthProvider(authProperties);
return getIMSAuthProvider(authProperties, authProxyConfiguration);

case JWT_TOKEN:
return getJWTAuthProvider(authProperties);
return getJWTAuthProvider(authProperties, authProxyConfiguration);

default:
throw new AuthException("Invalid token type to get auth provider");
}
}

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

String endpoint = authProperties.get(AuthUtils.AUTH_ENDPOINT);
return StringUtils.isEmpty(endpoint) ?
new IMSTokenProvider(clientId, clientCode, clientSecret) :
new IMSTokenProvider(endpoint, clientId, clientCode, clientSecret);
new IMSTokenProvider(clientId, clientCode, clientSecret, authProxyConfiguration) :
new IMSTokenProvider(endpoint, clientId, clientCode, clientSecret, authProxyConfiguration);
}

private static AuthProvider getJWTAuthProvider(Map<String, String> authProperties) {
private static AuthProvider getJWTAuthProvider(Map<String, String> authProperties,
AuthProxyConfiguration authProxyConfiguration) {
final String clientId = authProperties.get(AuthUtils.AUTH_CLIENT_ID);
final String clientSecret = authProperties.get(AuthUtils.AUTH_CLIENT_SECRET);
final String imsOrgId = authProperties.get(AuthUtils.AUTH_IMS_ORG_ID);
Expand All @@ -74,8 +76,9 @@ private static AuthProvider getJWTAuthProvider(Map<String, String> authPropertie

String endpoint = authProperties.get(AuthUtils.AUTH_ENDPOINT);
return StringUtils.isEmpty(endpoint) ?
new JWTTokenProvider(clientId, clientSecret, imsOrgId, technicalAccountKey, filePath) :
new JWTTokenProvider(endpoint, clientId, clientSecret, imsOrgId, technicalAccountKey, filePath);
new JWTTokenProvider(clientId, clientSecret, imsOrgId, technicalAccountKey, filePath, authProxyConfiguration) :
new JWTTokenProvider(endpoint, clientId, clientSecret, imsOrgId, technicalAccountKey, filePath,
authProxyConfiguration);
}

private AuthProviderFactory() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2021 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

package com.adobe.platform.streaming.auth.impl;

/**
* @author Adobe Inc.
*/
public class AuthProxyConfiguration {

private final String proxyHost;
private final int proxyPort;
private final String proxyUsername;
private final String proxyPassword;

public AuthProxyConfiguration(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) {
this.proxyHost = proxyHost;
this.proxyPort = proxyPort;
this.proxyUsername = proxyUsername;
this.proxyPassword = proxyPassword;
}

public String getProxyHost() {
return proxyHost;
}

public int getProxyPort() {
return proxyPort;
}

public String getProxyUsername() {
return proxyUsername;
}

public String getProxyPassword() {
return proxyPassword;
}

public static AuthProxyConfiguration.AuthProxyConfigurationBuilder builder() {
return new AuthProxyConfiguration.AuthProxyConfigurationBuilder();
}

/**
* @author Adobe Inc.
*/
public static class AuthProxyConfigurationBuilder {
private String proxyHost;
private int proxyPort;
private String proxyUsername;
private String proxyPassword;

AuthProxyConfigurationBuilder() {
}

public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyHost(String proxyHost) {
this.proxyHost = proxyHost;
return this;
}

public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyPort(int proxyPort) {
this.proxyPort = proxyPort;
return this;
}

public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyUsername(String proxyUsername) {
this.proxyUsername = proxyUsername;
return this;
}

public AuthProxyConfiguration.AuthProxyConfigurationBuilder proxyPassword(String proxyPassword) {
this.proxyPassword = proxyPassword;
return this;
}

public AuthProxyConfiguration build() {
return new AuthProxyConfiguration(this.proxyHost, this.proxyPort, this.proxyUsername, this.proxyPassword);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,31 @@ public class IMSTokenProvider extends AbstractAuthProvider {
private final String clientId;
private final String clientCode;
private final String clientSecret;
private HttpProducer httpProducer;

IMSTokenProvider(String clientId, String clientCode, String clientSecret) {
IMSTokenProvider(String clientId, String clientCode, String clientSecret,
AuthProxyConfiguration authProxyConfiguration) {
this.clientId = clientId;
this.clientCode = clientCode;
this.clientSecret = clientSecret;
this.httpProducer = HttpProducer.newBuilder(endpoint)
.withProxyHost(authProxyConfiguration.getProxyHost())
.withProxyPort(authProxyConfiguration.getProxyPort())
.withProxyUser(authProxyConfiguration.getProxyUsername())
.withProxyPassword(authProxyConfiguration.getProxyPassword())
.build();
}

IMSTokenProvider(String endpoint, String clientId, String clientCode, String clientSecret) {
this(clientId, clientCode, clientSecret);
IMSTokenProvider(String endpoint, String clientId, String clientCode, String clientSecret,
AuthProxyConfiguration authProxyConfiguration) {
this(clientId, clientCode, clientSecret, authProxyConfiguration);
this.endpoint = endpoint;
this.httpProducer = HttpProducer.newBuilder(endpoint)
.withProxyHost(authProxyConfiguration.getProxyHost())
.withProxyPort(authProxyConfiguration.getProxyPort())
.withProxyUser(authProxyConfiguration.getProxyUsername())
.withProxyPassword(authProxyConfiguration.getProxyPassword())
.build();
}

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

try {
return HttpProducer.newBuilder(endpoint).build().post(
return httpProducer.post(
IMS_ENDPOINT_PATH,
params.toString().getBytes(),
ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,33 @@ public class JWTTokenProvider extends AbstractAuthProvider {
private final String clientSecret;
private final String keyPath;
private String jwtToken;
private HttpProducer httpProducer;

JWTTokenProvider(String endpoint, String clientId, String clientSecret, String imsOrgId, String technicalAccountKey,
String keyPath) {
this(clientId, clientSecret, imsOrgId, technicalAccountKey, keyPath);
String keyPath, AuthProxyConfiguration authProxyConfiguration) {
this(clientId, clientSecret, imsOrgId, technicalAccountKey, keyPath, authProxyConfiguration);
this.endpoint = endpoint;
this.httpProducer = HttpProducer.newBuilder(endpoint)
.withProxyHost(authProxyConfiguration.getProxyHost())
.withProxyPort(authProxyConfiguration.getProxyPort())
.withProxyUser(authProxyConfiguration.getProxyUsername())
.withProxyPassword(authProxyConfiguration.getProxyPassword())
.build();
}

JWTTokenProvider(String clientId, String clientSecret, String imsOrgId, String technicalAccountKey, String keyPath) {
JWTTokenProvider(String clientId, String clientSecret, String imsOrgId, String technicalAccountKey, String keyPath,
AuthProxyConfiguration authProxyConfiguration) {
this.imsOrgId = imsOrgId;
this.clientId = clientId;
this.clientSecret = clientSecret;
this.technicalAccountKey = technicalAccountKey;
this.keyPath = keyPath;
this.httpProducer = HttpProducer.newBuilder(endpoint)
.withProxyHost(authProxyConfiguration.getProxyHost())
.withProxyPort(authProxyConfiguration.getProxyPort())
.withProxyUser(authProxyConfiguration.getProxyUsername())
.withProxyPassword(authProxyConfiguration.getProxyPassword())
.build();
}

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

try {
return HttpProducer.newBuilder(endpoint).build().post(
return httpProducer.post(
IMS_ENDPOINT_PATH,
params.toString().getBytes(),
ContentType.APPLICATION_FORM_URLENCODED.getMimeType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,22 @@ class IMSTokenProviderTest {

@Test
void testGetTokenInvalidClientId() {
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, null, TEST_CLIENT_CODE, TEST_CLIENT_SECRET);
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, null, TEST_CLIENT_CODE,
TEST_CLIENT_SECRET, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, imsTokenProvider::getToken);
}

@Test
void testGetTokenInvalidClientCode() {
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, null, TEST_CLIENT_SECRET);
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, null,
TEST_CLIENT_SECRET, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, imsTokenProvider::getToken);
}

@Test
void testGetTokenInvalidSecret() {
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, TEST_CLIENT_CODE, null);
IMSTokenProvider imsTokenProvider = new IMSTokenProvider(TEST_ENDPOINT, TEST_CLIENT_ID, TEST_CLIENT_CODE,
null, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, imsTokenProvider::getToken);
}

Expand All @@ -51,7 +54,8 @@ void testGetToken() {
TEST_ENDPOINT,
TEST_CLIENT_ID,
TEST_CLIENT_CODE,
TEST_CLIENT_SECRET
TEST_CLIENT_SECRET,
AuthProxyConfiguration.builder().build()
);
assertThrows(AuthException.class, imsTokenProvider::getTokenResponse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,36 @@ class JWTTokenProviderTest {

@Test
void testGetTokenWithInvalidIMSOrg() {
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, null, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_FILE_PATH);
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, null, TEST_CLIENT, TEST_ACCOUNT_ID,
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, tokenProvider::getToken);
}

@Test
void testGetTokenWithInvalidClientId() {
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, null, TEST_ACCOUNT_ID, TEST_FILE_PATH);
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, null, TEST_ACCOUNT_ID,
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, tokenProvider::getToken);
}

@Test
void testGetTokenWithInvalidAccountId() {
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, null, TEST_FILE_PATH);
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, null,
TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, tokenProvider::getToken);
}

@Test
void testGetTokenInvalidPath() {
JWTTokenProvider tokenProvider =
new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_INVALID_FILE_PATH);
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT,
TEST_ACCOUNT_ID, TEST_INVALID_FILE_PATH, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, tokenProvider::getToken);
}

@Test
void testGetTokenValidPath() {
JWTTokenProvider tokenProvider =
new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT, TEST_ACCOUNT_ID, TEST_FILE_PATH);
JWTTokenProvider tokenProvider = new JWTTokenProvider(ENDPOINT, TEST_ORG, TEST_CLIENT,
TEST_ACCOUNT_ID, TEST_FILE_PATH, AuthProxyConfiguration.builder().build());
assertThrows(AuthException.class, tokenProvider::getToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.adobe.platform.streaming.auth.AuthUtils;
import com.adobe.platform.streaming.auth.TokenType;
import com.adobe.platform.streaming.auth.impl.AuthProviderFactory;
import com.adobe.platform.streaming.auth.impl.AuthProxyConfiguration;
import com.adobe.platform.streaming.http.HttpProducer;
import com.adobe.platform.streaming.sink.utils.SinkUtils;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -124,6 +125,12 @@ private AuthProvider getIMSTokenProvider(Map<String, String> props) throws AuthE
.put(AuthUtils.AUTH_CLIENT_ID, props.get(AEP_CONNECTION_AUTH_CLIENT_ID))
.put(AuthUtils.AUTH_CLIENT_CODE, props.get(AEP_CONNECTION_AUTH_CLIENT_CODE))
.put(AuthUtils.AUTH_CLIENT_SECRET, props.get(AEP_CONNECTION_AUTH_CLIENT_SECRET))
.put(AuthUtils.AUTH_ENDPOINT, props.get(AEP_CONNECTION_AUTH_ENDPOINT))
.build(), AuthProxyConfiguration.builder()
.proxyHost(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_HOST, null))
.proxyPort(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PORT, 443))
.proxyUsername(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_USER, null))
.proxyPassword(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PASSWORD, null))
.build());
}

Expand All @@ -135,6 +142,11 @@ private AuthProvider getJWTTokenProvider(Map<String, String> props) throws AuthE
.put(AuthUtils.AUTH_IMS_ORG_ID, props.get(AEP_CONNECTION_AUTH_IMS_ORG))
.put(AuthUtils.AUTH_TECHNICAL_ACCOUNT_ID, props.get(AEP_CONNECTION_AUTH_ACCOUNT_KEY))
.put(AuthUtils.AUTH_PRIVATE_KEY_FILE_PATH, props.get(AEP_CONNECTION_AUTH_FILE_PATH))
.build(), AuthProxyConfiguration.builder()
.proxyHost(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_HOST, null))
.proxyPort(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PORT, 443))
.proxyUsername(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_USER, null))
.proxyPassword(SinkUtils.getProperty(props, AEP_CONNECTION_PROXY_PASSWORD, null))
.build());
}

Expand Down
Loading

0 comments on commit 4216072

Please sign in to comment.