Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
'clientCredentialsProvider' merged into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellmann committed Sep 29, 2015
2 parents d008418 + 10f3d81 commit a8b2ab8
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
*/
package org.zalando.stups.tokens.config;

import org.springframework.beans.factory.annotation.Autowired;
import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.zalando.stups.tokens.AccessTokensBean;
import org.zalando.stups.tokens.ClientCredentialsProvider;
import org.zalando.stups.tokens.JsonFileBackedClientCredentialsProvider;

/**
* @author jbellmann
Expand All @@ -38,4 +42,16 @@ public class AccessTokensBeanAutoConfiguration {
public AccessTokensBean accessTokensBean() {
return new AccessTokensBean(accessTokensBeanProperties);
}

@Bean
@ConditionalOnProperty(prefix="tokens", name="exposeClientCredentialProvider", havingValue="true")
public ClientCredentialsProvider clientCredentialsProvider(){
return new JsonFileBackedClientCredentialsProvider(getCredentialsFile(
accessTokensBeanProperties.getClientCredentialsFilename()));
}

protected File getCredentialsFile(final String credentialsFilename) {
return new File(accessTokensBeanProperties.getCredentialsDirectory(), credentialsFilename);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class AccessTokensBeanProperties {
private boolean autoStartup = true;

private int phase = 0;

private boolean exposeClientCredentialProvider = false;

private List<TokenConfiguration> tokenConfigurationList = new ArrayList<TokenConfiguration>(0);

Expand Down Expand Up @@ -88,18 +90,10 @@ public List<TokenConfiguration> getTokenConfigurationList() {
return tokenConfigurationList;
}

// public void setTokenConfigurationList(final List<TokenConfiguration> tokenConfigurationList) {
// this.tokenConfigurationList = tokenConfigurationList;
// }

public String getUserCredentialsFilename() {
return userCredentialsFilename;
}

// public void setUserCredentialsFilename(final String userCredentialsFilename) {
// this.userCredentialsFilename = userCredentialsFilename;
// }

public String getClientCredentialsFilename() {
return clientCredentialsFilename;
}
Expand Down Expand Up @@ -128,8 +122,11 @@ public void setPhase(final int phase) {
this.phase = phase;
}

// public void setClientCredentialsFilename(final String clientCredentialsFilename) {
// this.clientCredentialsFilename = clientCredentialsFilename;
// }
public boolean isExposeClientCredentialProvider() {
return exposeClientCredentialProvider;
}

public void setExposeClientCredentialProvider(boolean exposeClientCredentialProvider) {
this.exposeClientCredentialProvider = exposeClientCredentialProvider;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.unknown.pkg;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.assertj.core.api.Assertions;

import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;

import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.zalando.stups.tokens.AccessToken;
import org.zalando.stups.tokens.AccessTokens;
import org.zalando.stups.tokens.AccessTokensBean;
import org.zalando.stups.tokens.ClientCredentialsProvider;
import org.zalando.stups.tokens.config.AccessTokensBeanProperties;
import org.zalando.stups.tokens.config.TokenConfiguration;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

/**
* @author jbellmann
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TokenTestApplication.class})
@WebIntegrationTest(randomPort = false)
@ActiveProfiles("exposeClient")
public class ClientCredentialsProviderIT {

static final String OAUTH2_ACCESS_TOKENS = "OAUTH2_ACCESS_TOKENS";

@Autowired
private AccessTokensBean tokens;

@Autowired
private AccessTokensBeanProperties accessTokensBeanProperties;

@Autowired
private AccessTokens accessTokens;

@Autowired
private ClientCredentialsProvider clientCredentialsProvider;

@BeforeClass
public static void setUp() {
System.getProperties().remove(OAUTH2_ACCESS_TOKENS);
}

@Test
public void testClientCredentialProviderIsPresent() throws InterruptedException {
Assertions.assertThat(clientCredentialsProvider).isNotNull();
String clientId = clientCredentialsProvider.get().getId();
Assertions.assertThat(clientId).isNotNull();
Assertions.assertThat(clientId).isEqualTo("foo");
String clientSecret = clientCredentialsProvider.get().getSecret();
Assertions.assertThat(clientSecret).isNotNull();
Assertions.assertThat(clientSecret).isEqualTo("bar");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
*
* Licensed 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 CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.unknown.pkg;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.assertj.core.api.Assertions;

import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;

import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.zalando.stups.tokens.AccessToken;
import org.zalando.stups.tokens.AccessTokens;
import org.zalando.stups.tokens.AccessTokensBean;
import org.zalando.stups.tokens.ClientCredentialsProvider;
import org.zalando.stups.tokens.config.AccessTokensBeanProperties;
import org.zalando.stups.tokens.config.TokenConfiguration;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

/**
* @author jbellmann
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TokenTestApplication.class})
@WebIntegrationTest(randomPort = false)
@ActiveProfiles("notExposeClient")
public class DoNotClientCredentialsProviderIT {

static final String OAUTH2_ACCESS_TOKENS = "OAUTH2_ACCESS_TOKENS";

@Autowired(required=false)
private ClientCredentialsProvider clientCredentialsProvider;

@BeforeClass
public static void setUp() {
System.getProperties().remove(OAUTH2_ACCESS_TOKENS);
}

@Test
public void testClientCredentialProviderIsPresent() throws InterruptedException {
Assertions.assertThat(clientCredentialsProvider).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server:
port: 9192
#
#
tokens:
accessTokenUri: http://localhost:9191/access_token?realm=whatever
credentialsDirectory: ${user.dir}/somepath/credentials
refreshPercentLeft: 30
warnPercentLeft: 10
autoStartup: true
exposeClientCredentialProvider: true

token-configuration-list:
- tokenId: firstService
scopes:
- refole:read
- refole:write
- refole:all
- tokenId: secondService
scopes: singleScope:all
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
server:
port: 9193
#
#
tokens:
accessTokenUri: http://localhost:9191/access_token?realm=whatever
credentialsDirectory: ${user.dir}/somepath/credentials
refreshPercentLeft: 30
warnPercentLeft: 10
autoStartup: true

token-configuration-list:
- tokenId: firstService
scopes:
- refole:read
- refole:write
- refole:all
- tokenId: secondService
scopes: singleScope:all

0 comments on commit a8b2ab8

Please sign in to comment.