Skip to content

Commit

Permalink
Fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Schönborn authored and whiskeysierra committed Feb 5, 2019
1 parent cef45a1 commit d81337f
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.zalando.riptide.autoconfigure;

import org.zalando.stups.tokens.AccessTokens;
import org.zalando.stups.tokens.AccessTokensBuilder;
import org.zalando.stups.tokens.JsonFileBackedClientCredentialsProvider;
import org.zalando.stups.tokens.JsonFileBackedUserCredentialsProvider;
import org.zalando.stups.tokens.Tokens;

import javax.annotation.Nullable;
import java.net.URI;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkArgument;

@SuppressWarnings("unused")
final class AccessTokensFactory {

private AccessTokensFactory() {

}

public static AccessTokens createAccessTokens(final RiptideProperties properties) {
final RiptideProperties.GlobalOAuth oAuth = properties.getOauth();

final URI accessTokenUrl = getAccessTokenUrl(oAuth);
@Nullable final Path directory = oAuth.getCredentialsDirectory();
final TimeSpan connectTimeout = oAuth.getConnectTimeout();
final TimeSpan socketTimeout = oAuth.getSocketTimeout();

final AccessTokensBuilder builder = Tokens.createAccessTokensWithUri(accessTokenUrl)
.usingClientCredentialsProvider(getClientCredentialsProvider(directory))
.usingUserCredentialsProvider(getUserCredentialsProvider(directory))
.schedulingPeriod((int) oAuth.getSchedulingPeriod().getAmount())
.schedulingTimeUnit(oAuth.getSchedulingPeriod().getUnit())
.connectTimeout((int) connectTimeout.to(TimeUnit.MILLISECONDS))
.socketTimeout((int) socketTimeout.to(TimeUnit.MILLISECONDS));

properties.getClients().forEach((id, client) -> {
@Nullable final RiptideProperties.Client.OAuth clientOAuth = client.getOauth();

if (clientOAuth == null) {
return;
}

builder.manageToken(id)
.addScopesTypeSafe(clientOAuth.getScopes())
.done();
});

return builder.start();
}

private static JsonFileBackedClientCredentialsProvider getClientCredentialsProvider(@Nullable final Path directory) {
return directory == null ?
new JsonFileBackedClientCredentialsProvider() :
new JsonFileBackedClientCredentialsProvider(directory.resolve("client.json").toFile());
}

private static JsonFileBackedUserCredentialsProvider getUserCredentialsProvider(@Nullable final Path directory) {
return directory == null ?
new JsonFileBackedUserCredentialsProvider() :
new JsonFileBackedUserCredentialsProvider(directory.resolve("user.json").toFile());
}

private static URI getAccessTokenUrl(final RiptideProperties.GlobalOAuth oauth) {
@Nullable final URI accessTokenUrl = oauth.getAccessTokenUrl();

checkArgument(accessTokenUrl != null, "" +
"Neither 'riptide.oauth.access-token-url' nor 'ACCESS_TOKEN_URL' was set, " +
"but at least one client requires OAuth");

return accessTokenUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.zalando.riptide.OriginalStackTracePlugin;
import org.zalando.riptide.Plugin;
import org.zalando.riptide.PluginInterceptor;
import org.zalando.riptide.autoconfigure.RiptideProperties.Client;
import org.zalando.riptide.backup.BackupRequestPlugin;
import org.zalando.riptide.failsafe.CircuitBreakerListener;
import org.zalando.riptide.failsafe.FailsafePlugin;
Expand All @@ -37,7 +38,6 @@
import org.zalando.riptide.httpclient.RestAsyncClientHttpRequestFactory;
import org.zalando.riptide.httpclient.metrics.HttpConnectionPoolMetrics;
import org.zalando.riptide.metrics.MetricsPlugin;
import org.zalando.riptide.spring.RiptideProperties.Client;
import org.zalando.riptide.stream.Streams;
import org.zalando.riptide.timeout.TimeoutPlugin;
import org.zalando.stups.oauth2.httpcomponents.AccessTokensRequestInterceptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.riptide.spring;
package org.zalando.riptide.autoconfigure;

import net.jodah.failsafe.CircuitBreaker;
import net.jodah.failsafe.RetryPolicy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.riptide.spring;
package org.zalando.riptide.autoconfigure;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpRequestInterceptor;
Expand All @@ -13,7 +13,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.zalando.riptide.spring.RiptideProperties.Client;
import org.zalando.riptide.autoconfigure.RiptideProperties.Client;

import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.zalando.riptide.spring;
package org.zalando.riptide.autoconfigure;

import com.google.common.collect.ImmutableList;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public static final class Client {
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public static final class OAuth {
private List<String> scopes = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.zalando.riptide.spring;
package org.zalando.riptide.autoconfigure;

import com.google.common.collect.ImmutableMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.zalando.riptide.spring.RiptideProperties.Client.Keystore;
import org.zalando.riptide.spring.RiptideProperties.Defaults;
import org.zalando.riptide.spring.RiptideProperties.GlobalOAuth;
import org.zalando.riptide.autoconfigure.RiptideProperties.Client.Keystore;
import org.zalando.riptide.autoconfigure.RiptideProperties.Defaults;
import org.zalando.riptide.autoconfigure.RiptideProperties.GlobalOAuth;

import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ riptide:
oauth:
access-token-url: http://example.com
credentials-directory: src/test/resources

clients:
example:
oauth.scopes:
Expand Down

0 comments on commit d81337f

Please sign in to comment.