Skip to content

Commit 5ebe58f

Browse files
Merge pull request #71 from allmightyspiff/bearerTokenAuth
Bearer token auth support
2 parents d51f8a1 + 1e82fa3 commit 5ebe58f

File tree

14 files changed

+174
-34
lines changed

14 files changed

+174
-34
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2020 The SoftLayer Developer Network
3+
Copyright (c) 2021 The SoftLayer Developer Network
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,45 @@ additions to the SoftLayer API.
4040
<dependency>
4141
<groupId>com.softlayer.api</groupId>
4242
<artifactId>softlayer-api-client</artifactId>
43-
<version>0.3.1</version>
43+
<version>0.3.2</version>
4444
</dependency>
4545
```
4646

4747
### Gradle
4848

4949
```groovy
50-
implementation 'com.softlayer.api:softlayer-api-client:0.3.1'
50+
implementation 'com.softlayer.api:softlayer-api-client:0.3.2'
5151
```
5252

5353
### Kotlin
5454

5555
```kotlin
56-
compile("com.softlayer.api:softlayer-api-client:0.3.1")
56+
compile("com.softlayer.api:softlayer-api-client:0.3.2")
5757
```
5858

5959
### Creating a Client
6060

6161
All clients are instances of `ApiClient`. Currently there is only one implementation, the `RestApiClient`. Simply
6262
instantiate it and provide your credentials:
6363

64+
65+
#### Username and API Key
66+
For using a Classic Infrastructure or IBM Cloud API key. When using the IBM Cloud Api key, your username is the literal string `apikey`, more information about that can be found on the SLDN [Authenticating to the SoftLayer API](https://sldn.softlayer.com/article/authenticating-softlayer-api/#cloud-api) article.
67+
6468
```java
6569
import com.softlayer.api.*;
6670

6771
ApiClient client = new RestApiClient().withCredentials("my user", "my api key");
6872
```
6973

74+
#### Access Token
75+
Information on how to get a temoprary api token can be found on the SLDN [Authenticating to the SoftLayer API](https://sldn.softlayer.com/article/authenticating-softlayer-api/#temp-token) article.
76+
77+
```java
78+
import com.softlayer.api.*;
79+
ApiClient client = new RestApiClient().withBearerToken("qqqqwwwweeeaaassddd....");
80+
```
81+
7082
If the end point isn't at the normal SoftLayer API, you can provide the prefix to the constructor of the
7183
`RestApiClient`. By default it is set to the public API endpoint, `https://api.softlayer.com/rest/v3.1/`.
7284

@@ -296,4 +308,4 @@ fully qualified class name of your implementation on a single line in a file in
296308

297309
## Copyright
298310

299-
This software is Copyright (c) 2020 The SoftLayer Developer Network. See the bundled LICENSE file for more information.
311+
This software is Copyright (c) 2021 The SoftLayer Developer Network. See the bundled LICENSE file for more information.

examples/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>softlayer-api-client-examples</artifactId>
66
<packaging>jar</packaging>
77
<!-- Please keep version in sync with README -->
8-
<version>0.3.1</version>
8+
<version>0.3.2</version>
99
<name>softlayer-api-client-examples</name>
1010
<url>http://sldn.softlayer.com</url>
1111
<licenses>

examples/src/main/java/com/softlayer/api/example/Example.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ public void start(String[] args) throws Exception {
1717
baseUrl += '/';
1818
}
1919

20-
run(new RestApiClient(baseUrl).withCredentials(args[0], args[1]));
20+
RestApiClient client;
21+
// mvn -e -q compile exec:java -Dexec.args="QuickTest Bearer eyJraWQ.....
22+
if (args[0].trim().equals("Bearer")) {
23+
client = new RestApiClient(baseUrl).withBearerToken(args[1]);
24+
} else {
25+
client = new RestApiClient(baseUrl).withCredentials(args[0], args[1]);
26+
}
27+
run(client);
2128
}
2229

2330
/** Run the example with the given client */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.softlayer.api.example;
2+
3+
import com.softlayer.api.ApiClient;
4+
import com.softlayer.api.RestApiClient;
5+
import com.softlayer.api.service.Account;
6+
7+
8+
/** A quick example for testing if authentication works.
9+
10+
cd softlayer-java/examples
11+
mvn -e -q compile exec:java -Dexec.args="QuickTest Bearer eyJraWQ.....
12+
*/
13+
public class QuickTest extends Example {
14+
15+
@Override
16+
public void run(ApiClient client) throws Exception {
17+
client.withLoggingEnabled();
18+
System.out.format("Authorization: %s\n", client.getCredentials());
19+
Account.Service service = Account.service(client);
20+
21+
Account account = service.getObject();
22+
System.out.format("Account Name: %s\n", account.getCompanyName());
23+
}
24+
25+
public static void main(String[] args) throws Exception {
26+
new QuickTest().start(args);
27+
}
28+
}

gen/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<artifactId>softlayer-api-client-gen</artifactId>
66
<packaging>jar</packaging>
77
<!-- Please keep version in sync with README -->
8-
<version>0.3.1</version>
8+
<version>0.3.2</version>
99
<name>softlayer-api-client-gen</name>
1010
<url>http://sldn.softlayer.com</url>
1111
<licenses>

pom.xml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<artifactId>softlayer-api-client</artifactId>
55
<packaging>jar</packaging>
66
<!-- Please keep version in sync with README -->
7-
<version>0.3.1</version>
7+
<version>0.3.2</version>
88
<name>SoftLayer API Client for Java</name>
99
<description>API client for accessing the SoftLayer API</description>
1010
<url>http://sldn.softlayer.com</url>
@@ -40,7 +40,7 @@
4040
<connection>scm:git:git@github.com:softlayer/softlayer-java.git</connection>
4141
<developerConnection>scm:git:git@github.com:softlayer/softlayer-java.git</developerConnection>
4242
<url>git@github.com:softlayer/softlayer-java.git</url>
43-
<tag>0.3.1</tag>
43+
<tag>0.3.2</tag>
4444
</scm>
4545
<properties>
4646
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -161,6 +161,30 @@
161161
</execution>
162162
</executions>
163163
</plugin>
164+
<plugin>
165+
<groupId>org.jacoco</groupId>
166+
<artifactId>jacoco-maven-plugin</artifactId>
167+
<version>0.8.6</version>
168+
<configuration>
169+
<excludes>
170+
<exclude>**/*com/softlayer/api/service/**/*</exclude>
171+
</excludes>
172+
</configuration>
173+
<executions>
174+
<execution>
175+
<goals>
176+
<goal>prepare-agent</goal>
177+
</goals>
178+
</execution>
179+
<execution>
180+
<id>report</id>
181+
<phase>prepare-package</phase>
182+
<goals>
183+
<goal>report</goal>
184+
</goals>
185+
</execution>
186+
</executions>
187+
</plugin>
164188
</plugins>
165189
</build>
166190
</project>

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;

0 commit comments

Comments
 (0)