Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enabled auth server specific proxy settings #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _If you need support for other grant types or authentication methods, please che

## Getting started ##
Install with Maven:

```xml
<dependency>
<groupId>com.scalepoint</groupId>
Expand Down Expand Up @@ -52,3 +53,16 @@ ClientCredentialsGrantTokenClient tokenClient = new ClientCredentialsGrantTokenC

String accessToken = tokenClient.getToken("scope1", "scope2");
```

### Using Proxies ###

The token client respects the proxy settings in the system properties, e.g. `http.proxyHost` or `http.proxyPort`.

However in some cases the OAuth server needs to be accessed through a different proxy than the resource server.
In these cases the OAuth server's proxy can be set like this:

```java
String proxyHost = "proxy.example.com"; // ip address works as well
int proxyPort = 8080;
tokenClient.setProxy(proxyHost, proxyPort);
```
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public CustomGrantTokenClient(String tokenEndpointUri, ClientCredentials clientC
this.cache = cache;
}

/**
* Sets the proxy to be used for token requests.
*
* @param host Host name or IP address of the proxy
* @param port TCP port of the proxy
*/
public void setProxy(final String host, final int port) {
tokenEndpointHttpClient.setProxy(host, port);
}

/**
* Retrieve access token for the configured "client_id" and specified scopes. Request to the server is only performed if matching valid token is not in the cache
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.*;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
Expand All @@ -15,6 +17,8 @@ class TokenEndpointHttpClient {

private final String tokenEndpointUri;

private Proxy proxy;

public TokenEndpointHttpClient(String tokenEndpointUri) {
this.tokenEndpointUri = tokenEndpointUri;
}
Expand All @@ -27,6 +31,10 @@ ExpiringToken getToken(List<NameValuePair> params) throws IOException {
return parseResponse(tokenResponse);
}

void setProxy(final String host, final int port) {
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(host, port));
}

private String formatRequest(List<NameValuePair> params) throws UnsupportedEncodingException {
String body = "";
for (NameValuePair p: params) {
Expand All @@ -38,7 +46,13 @@ private String formatRequest(List<NameValuePair> params) throws UnsupportedEncod

private HttpURLConnection makeRequest(String body) throws IOException {
URL u = new URL(tokenEndpointUri);
HttpURLConnection c = (HttpURLConnection)u.openConnection();
HttpURLConnection c;
if (proxy != null) {
c = (HttpURLConnection)u.openConnection(proxy);
}
else {
c = (HttpURLConnection)u.openConnection();
}
c.setRequestMethod("POST");
c.setInstanceFollowRedirects(false);
c.setDoInput(true);
Expand Down