Skip to content

Commit 07d95aa

Browse files
authored
Merge pull request #984 from watson-developer-cloud/icp-apikey
Add support for ICP auth
2 parents 8458e10 + e3944e9 commit 07d95aa

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

core/src/main/java/com/ibm/watson/developer_cloud/service/WatsonService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public abstract class WatsonService {
7070
private static final String BASIC = "Basic ";
7171
private static final String BEARER = "Bearer ";
7272
private static final String APIKEY_AS_USERNAME = "apikey";
73+
private static final String ICP_PREFIX = "icp-";
7374
private static final Logger LOG = Logger.getLogger(WatsonService.class.getName());
7475
private static final String AUTH_HEADER_DEPRECATION_MESSAGE = "Authenticating with the X-Watson-Authorization-Token"
7576
+ "header is deprecated. The token continues to work with Cloud Foundry services, but is not supported for "
@@ -350,7 +351,8 @@ public void setEndPoint(final String endPoint) {
350351
* @param password the password
351352
*/
352353
public void setUsernameAndPassword(final String username, final String password) {
353-
if (username.equals(APIKEY_AS_USERNAME)) {
354+
// we'll perform the token exchange for users UNLESS they're on ICP
355+
if (username.equals(APIKEY_AS_USERNAME) && !password.startsWith(ICP_PREFIX)) {
354356
IamOptions iamOptions = new IamOptions.Builder()
355357
.apiKey(password)
356358
.build();

core/src/test/java/com/ibm/watson/developer_cloud/service/AuthenticationTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import org.junit.Test;
44

55
import static org.junit.Assert.assertTrue;
6+
import static org.junit.Assert.assertFalse;
67

78
public class AuthenticationTest {
9+
private static final String APIKEY_USERNAME = "apikey";
810
private static final String APIKEY = "12345";
11+
private static final String ICP_APIKEY = "icp-12345";
912

1013
public class TestService extends WatsonService {
1114
private static final String SERVICE_NAME = "test";
@@ -18,7 +21,14 @@ public TestService() {
1821
@Test
1922
public void authenticateWithApiKeyAsUsername() {
2023
TestService service = new TestService();
21-
service.setUsernameAndPassword("apikey", APIKEY);
24+
service.setUsernameAndPassword(APIKEY_USERNAME, APIKEY);
2225
assertTrue(service.isTokenManagerSet());
2326
}
27+
28+
@Test
29+
public void authenticateWithIcp() {
30+
TestService service = new TestService();
31+
service.setUsernameAndPassword(APIKEY_USERNAME, ICP_APIKEY);
32+
assertFalse(service.isTokenManagerSet());
33+
}
2434
}

0 commit comments

Comments
 (0)