Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Move the OKTA specific references to the OKTA connector so that Guice…
Browse files Browse the repository at this point in the history
… does not explode at start up whehn OKTA is not enabled (#12)
  • Loading branch information
fieldju authored and sdford committed Jan 9, 2017
1 parent 0836bb0 commit 4ad3a94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -299,26 +295,4 @@ public CmsRequestSecurityValidator authRequestSecurityValidator(
public CompletableFuture<AppInfo> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

/////////////////////////
Expand Down

0 comments on commit 4ad3a94

Please sign in to comment.