Skip to content

Commit 0cdfb8d

Browse files
#70 support for Bearer HTTP authorization
1 parent c0cf8e9 commit 0cdfb8d

File tree

7 files changed

+81
-24
lines changed

7 files changed

+81
-24
lines changed

src/main/java/com/softlayer/api/ApiClient.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.softlayer.api;
22

3+
import com.softlayer.api.http.HttpCredentials;
4+
35
/** Common interface for all API clients. {@link RestApiClient} is the preferred implementation */
46
public interface ApiClient {
57

@@ -9,7 +11,28 @@ public interface ApiClient {
911
* @return This instance
1012
*/
1113
ApiClient withCredentials(String username, String apiKey);
12-
14+
15+
/**
16+
* Uses a HTTP Bearer token for authentication instead of API key.
17+
*
18+
* @return This instance
19+
*/
20+
ApiClient withBearerToken(String token);
21+
22+
/**
23+
* Enables logging for client API calls
24+
*
25+
* @return This instance
26+
*/
27+
ApiClient withLoggingEnabled();
28+
29+
/**
30+
* Returns the HTTP Authorization header
31+
*
32+
* @return This instance
33+
*/
34+
HttpCredentials getCredentials();
35+
1336
/**
1437
* Get a service for the given sets of classes and optional ID. It is not recommended to call this
1538
* directly, but rather invoke the service method on the type class.

src/main/java/com/softlayer/api/RestApiClient.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import com.softlayer.api.annotation.ApiMethod;
2020
import com.softlayer.api.annotation.ApiService;
21+
import com.softlayer.api.http.HttpCredentials;
2122
import com.softlayer.api.http.HttpBasicAuthCredentials;
23+
import com.softlayer.api.http.HttpBearerCredentials;
2224
import com.softlayer.api.http.HttpClient;
2325
import com.softlayer.api.http.HttpClientFactory;
2426
import com.softlayer.api.http.HttpResponse;
@@ -64,7 +66,7 @@ public class RestApiClient implements ApiClient {
6466
private HttpClientFactory httpClientFactory;
6567
private JsonMarshallerFactory jsonMarshallerFactory;
6668
private boolean loggingEnabled = false;
67-
private HttpBasicAuthCredentials credentials;
69+
private HttpCredentials credentials;
6870

6971
/**
7072
* Create a Rest client that uses the publically available API.
@@ -114,6 +116,7 @@ public void setLoggingEnabled(boolean loggingEnabled) {
114116
this.loggingEnabled = loggingEnabled;
115117
}
116118

119+
@Override
117120
public RestApiClient withLoggingEnabled() {
118121
this.loggingEnabled = true;
119122
return this;
@@ -141,7 +144,14 @@ public RestApiClient withCredentials(String username, String apiKey) {
141144
return this;
142145
}
143146

144-
public HttpBasicAuthCredentials getCredentials() {
147+
@Override
148+
public RestApiClient withBearerToken(String token) {
149+
credentials = new HttpBearerCredentials(token);
150+
return this;
151+
}
152+
153+
@Override
154+
public HttpCredentials getCredentials() {
145155
return credentials;
146156
}
147157

src/main/java/com/softlayer/api/http/BuiltInHttpClientFactory.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void setThreadPool(ExecutorService threadPool) {
8989

9090
class BuiltInHttpClient implements HttpClient, HttpResponse {
9191

92-
final HttpBasicAuthCredentials credentials;
92+
final HttpCredentials credentials;
9393
final String method;
9494
final String fullUrl;
9595
final Map<String, List<String>> headers;
@@ -101,11 +101,7 @@ public BuiltInHttpClient(
101101
String fullUrl,
102102
Map<String, List<String>> headers
103103
) {
104-
// We only support basic auth
105-
if (credentials != null && !(credentials instanceof HttpBasicAuthCredentials)) {
106-
throw new UnsupportedOperationException("Only basic auth is supported, not " + credentials.getClass());
107-
}
108-
this.credentials = (HttpBasicAuthCredentials) credentials;
104+
this.credentials = credentials;
109105
this.method = method;
110106
this.fullUrl = fullUrl;
111107
this.headers = headers;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.softlayer.api.http;
2+
3+
/** HTTP bearer authorization support for bearer token fromhttps://iam.cloud.ibm.com/identity/token */
4+
public class HttpBearerCredentials implements HttpCredentials {
5+
6+
public final String token;
7+
8+
public HttpBearerCredentials(String token) {
9+
this.token = token;
10+
}
11+
12+
/**
13+
* Formats the token into a HTTP Authorization header.
14+
*
15+
* @return String
16+
*/
17+
public String getHeader() {
18+
return "Bearer " + token;
19+
}
20+
}

src/main/java/com/softlayer/api/http/HttpCredentials.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
/** Base interface for all accepted HTTP credentials */
44
public interface HttpCredentials {
5+
public String getHeader();
56
}

src/test/java/com/softlayer/api/http/BuiltInHttpClientFactoryTest.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,6 @@
1515

1616
public class BuiltInHttpClientFactoryTest {
1717

18-
@Test
19-
public void testGetHttpClientWithoutBasicAuth() {
20-
try {
21-
new BuiltInHttpClientFactory().getHttpClient(
22-
new HttpCredentials() { },
23-
"GET",
24-
"http://example.com",
25-
Collections.emptyMap()
26-
);
27-
fail();
28-
} catch (UnsupportedOperationException e) {
29-
assertTrue(e.getMessage().contains("basic auth"));
30-
}
31-
}
32-
3318
@Test
3419
public void testGetThreadPoolDefaultsToDaemonThreads() throws Exception {
3520
boolean daemon = new BuiltInHttpClientFactory().getThreadPool().submit(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.softlayer.api.http;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class HttpBearerCredentialsTest {
8+
9+
public final String bearerToken = "qqqqwwwweeerrttyyuuiiooppasddfgfgjghjkjklZXxcvcvbvbnnbm";
10+
@Test
11+
public void testConstructor() {
12+
HttpBearerCredentials authCredentials = new HttpBearerCredentials(bearerToken);
13+
assertEquals(bearerToken, authCredentials.token);
14+
}
15+
16+
@Test
17+
public void testGetHeader() {
18+
HttpBearerCredentials authCredentials = new HttpBearerCredentials(bearerToken);
19+
String header = "Bearer " + bearerToken;
20+
assertEquals(header, authCredentials.getHeader());
21+
}
22+
}

0 commit comments

Comments
 (0)