From 4ad3a945dcac56ea13706d39ce2338dd7d14c738 Mon Sep 17 00:00:00 2001 From: Justin Field Date: Mon, 9 Jan 2017 11:07:13 -0800 Subject: [PATCH] Move the OKTA specific references to the OKTA connector so that Guice does not explode at start up whehn OKTA is not enabled (#12) --- .../auth/connector/okta/OktaAuthHelper.java | 35 ++++++++++++------- .../server/config/guice/CmsGuiceModule.java | 26 -------------- .../connector/okta/OktaAuthHelperTest.java | 14 +++++--- 3 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelper.java b/src/main/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelper.java index 99c4188fd..dffa4399d 100644 --- a/src/main/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelper.java +++ b/src/main/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelper.java @@ -28,11 +28,13 @@ import com.nike.cerberus.error.DefaultApiError; import com.okta.sdk.clients.AuthApiClient; import com.okta.sdk.clients.UserApiClient; +import com.okta.sdk.framework.ApiClientConfiguration; import com.okta.sdk.models.auth.AuthResult; import com.okta.sdk.models.factors.Factor; import com.okta.sdk.models.usergroups.UserGroup; import org.apache.commons.lang3.text.WordUtils; +import javax.inject.Named; import java.io.IOException; import java.util.List; import java.util.Map; @@ -56,20 +58,27 @@ public class OktaAuthHelper { private final UserApiClient userApiClient; - @Inject - public OktaAuthHelper(final AuthApiClient authApiClient, final UserApiClient userApiClient) { - - this.objectMapper = new ObjectMapper(); - this.objectMapper.findAndRegisterModules(); - this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); - this.objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - this.objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); - this.objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - this.objectMapper.enable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); - this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT); - - this.authClient = authApiClient; + public OktaAuthHelper(AuthApiClient authClient, + UserApiClient userApiClient, + ObjectMapper objectMapper) { + + this.authClient = authClient; this.userApiClient = userApiClient; + this.objectMapper = objectMapper; + } + + @Inject + public OktaAuthHelper(@Named("auth.connector.okta.api_key") final String oktaApiKey, + @Named("auth.connector.okta.base_url") final String baseUrl, + final ObjectMapper objectMapper) { + + Preconditions.checkArgument(oktaApiKey != null, "okta api key cannot be null"); + Preconditions.checkArgument(baseUrl != null, "okta base url cannot be null"); + + this.objectMapper = objectMapper; + + this.authClient = new AuthApiClient(new ApiClientConfiguration(baseUrl, oktaApiKey)); + this.userApiClient = new UserApiClient(new ApiClientConfiguration(baseUrl, oktaApiKey)); } /** diff --git a/src/main/java/com/nike/cerberus/server/config/guice/CmsGuiceModule.java b/src/main/java/com/nike/cerberus/server/config/guice/CmsGuiceModule.java index c00abac25..bf8470955 100644 --- a/src/main/java/com/nike/cerberus/server/config/guice/CmsGuiceModule.java +++ b/src/main/java/com/nike/cerberus/server/config/guice/CmsGuiceModule.java @@ -18,7 +18,6 @@ package com.nike.cerberus.server.config.guice; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Preconditions; import com.google.inject.name.Names; import com.nike.backstopper.apierror.projectspecificinfo.ProjectApiErrors; import com.nike.cerberus.config.CmsEnvPropertiesLoader; @@ -65,9 +64,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; -import com.okta.sdk.clients.AuthApiClient; -import com.okta.sdk.clients.UserApiClient; -import com.okta.sdk.framework.ApiClientConfiguration; import com.typesafe.config.Config; import com.typesafe.config.ConfigValueFactory; @@ -299,26 +295,4 @@ public CmsRequestSecurityValidator authRequestSecurityValidator( public CompletableFuture appInfoFuture(AsyncHttpClientHelper asyncHttpClientHelper) { return AwsUtil.getAppInfoFutureWithAwsInfo(asyncHttpClientHelper); } - - @Singleton - @Provides - public AuthApiClient authApiClient(@Named("auth.connector.okta.api_key") final String oktaApiKey, - @Named("auth.connector.okta.base_url") final String baseUrl) { - - Preconditions.checkArgument(oktaApiKey != null, "okta api key cannot be null"); - Preconditions.checkArgument(baseUrl != null, "okta base url cannot be null"); - - return new AuthApiClient(new ApiClientConfiguration(baseUrl, oktaApiKey)); - } - - @Singleton - @Provides - public UserApiClient userApiClient(@Named("auth.connector.okta.api_key") final String oktaApiKey, - @Named("auth.connector.okta.base_url") final String baseUrl) { - - Preconditions.checkArgument(oktaApiKey != null, "okta api key cannot be null"); - Preconditions.checkArgument(baseUrl != null, "okta base url cannot be null"); - - return new UserApiClient(new ApiClientConfiguration(baseUrl, oktaApiKey)); - } } diff --git a/src/test/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelperTest.java b/src/test/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelperTest.java index 7dab36eeb..848d5433d 100644 --- a/src/test/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelperTest.java +++ b/src/test/java/com/nike/cerberus/auth/connector/okta/OktaAuthHelperTest.java @@ -17,6 +17,7 @@ package com.nike.cerberus.auth.connector.okta; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.nike.backstopper.exception.ApiException; @@ -28,6 +29,8 @@ import org.apache.commons.lang3.StringUtils; import org.junit.Before; import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; import java.io.IOException; import java.util.List; @@ -40,6 +43,7 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; /** * Tests the OktaAuthHelper class @@ -50,18 +54,20 @@ public class OktaAuthHelperTest { private OktaAuthHelper oktaAuthHelper; // dependencies + @Mock private AuthApiClient authApiClient; + + @Mock private UserApiClient userApiClient; @Before public void setup() { + initMocks(this); - // mock dependencies - authApiClient = mock(AuthApiClient.class); - userApiClient = mock(UserApiClient.class); + ObjectMapper mapper = new ObjectMapper(); // create test object - oktaAuthHelper = new OktaAuthHelper(authApiClient, userApiClient); + oktaAuthHelper = new OktaAuthHelper(authApiClient, userApiClient, mapper); } /////////////////////////