Skip to content

Commit 87b0758

Browse files
committed
feat(IAM): set client id/secret via IamOptions; corrected terminology
1 parent 22f71f5 commit 87b0758

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

src/main/java/com/ibm/cloud/sdk/core/service/security/IamOptions.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class IamOptions {
1919
private String apiKey;
2020
private String accessToken;
2121
private String url;
22+
private String clientId;
23+
private String clientSecret;
24+
2225

2326
public String getApiKey() {
2427
return apiKey;
@@ -32,10 +35,20 @@ public String getUrl() {
3235
return url;
3336
}
3437

38+
public String getClientId() {
39+
return clientId;
40+
}
41+
42+
public String getClientSecret() {
43+
return clientSecret;
44+
}
45+
3546
public static class Builder {
3647
private String apiKey;
3748
private String accessToken;
3849
private String url;
50+
private String clientId;
51+
private String clientSecret;
3952

4053
public IamOptions build() {
4154
return new IamOptions(this);
@@ -55,11 +68,23 @@ public Builder url(String url) {
5568
this.url = url;
5669
return this;
5770
}
71+
72+
public Builder clientId(String clientId) {
73+
this.clientId = clientId;
74+
return this;
75+
}
76+
77+
public Builder clientSecret(String clientSecret) {
78+
this.clientSecret = clientSecret;
79+
return this;
80+
}
5881
}
5982

6083
private IamOptions(Builder builder) {
6184
this.apiKey = builder.apiKey;
6285
this.accessToken = builder.accessToken;
6386
this.url = builder.url;
87+
this.clientId = builder.clientId;
88+
this.clientSecret = builder.clientSecret;
6489
}
6590
}

src/main/java/com/ibm/cloud/sdk/core/service/security/IamTokenManager.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
* Retrieves, stores, and refreshes IAM tokens.
3333
*/
3434
public class IamTokenManager {
35-
private static String iamClientId = null;
36-
private static String iamSecret = null;
37-
3835
private String userManagedAccessToken;
3936
private String apiKey;
4037
private String url;
38+
private String clientId;
39+
private String clientSecret;
40+
4141
private IamToken tokenData;
4242

4343
private static final Logger LOG = Logger.getLogger(IamTokenManager.class.getName());
@@ -62,6 +62,8 @@ public IamTokenManager(IamOptions options) {
6262
}
6363
this.url = (options.getUrl() != null) ? options.getUrl() : DEFAULT_IAM_URL;
6464
this.userManagedAccessToken = options.getAccessToken();
65+
this.clientId = options.getClientId();
66+
this.clientSecret = options.getClientSecret();
6567
tokenData = new IamToken();
6668
}
6769

@@ -223,26 +225,26 @@ public void run() {
223225
return returnToken[0];
224226
}
225227

226-
public static String getIamClientId() {
227-
return iamClientId;
228+
public String getClientId() {
229+
return this.clientId;
228230
}
229231

230-
public static void setIamClientId(String iamClientId) {
231-
IamTokenManager.iamClientId = iamClientId;
232+
public void setClientId(String clientId) {
233+
this.clientId = clientId;
232234
}
233235

234-
public static String getIamSecret() {
235-
return iamSecret;
236+
public String getClientSecret() {
237+
return this.clientSecret;
236238
}
237239

238-
public static void setIamSecret(String iamSecret) {
239-
IamTokenManager.iamSecret = iamSecret;
240+
public void setClientSecret(String clientSecret) {
241+
this.clientSecret = clientSecret;
240242
}
241243

242-
public static String getAuthorizationHeaderValue() {
244+
public String getAuthorizationHeaderValue() {
243245
String result;
244-
if (getIamClientId() != null && getIamSecret() != null) {
245-
String s = getIamClientId() + ":" + getIamSecret();
246+
if (getClientId() != null && getClientSecret() != null) {
247+
String s = getClientId() + ":" + getClientSecret();
246248
result = "Basic " + Base64.getEncoder().encodeToString(s.getBytes());
247249
} else {
248250
result = DEFAULT_AUTHORIZATION;

src/test/java/com/ibm/cloud/sdk/core/test/service/IamManagerTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,29 @@ public void setUp() throws Exception {
4545
@Test
4646
public void testAuthorizationHeader() {
4747
// Make sure the default header value is correct.
48-
String header1 = IamTokenManager.getAuthorizationHeaderValue();
48+
IamOptions options = new IamOptions.Builder()
49+
.apiKey(API_KEY)
50+
.url(url)
51+
.build();
52+
IamTokenManager manager = new IamTokenManager(options);
53+
String header1 = manager.getAuthorizationHeaderValue();
4954
assertEquals(header1, "Basic Yng6Yng=");
5055

5156
// Now make sure different clientid/secret combinations yield different header values
5257

53-
IamTokenManager.setIamClientId("myuser");
54-
IamTokenManager.setIamSecret("mysecret");
55-
String header2 = IamTokenManager.getAuthorizationHeaderValue();
58+
manager.setClientId("myuser");
59+
manager.setClientSecret("mysecret");
60+
String header2 = manager.getAuthorizationHeaderValue();
5661
assertNotEquals(header1, header2);
5762

58-
IamTokenManager.setIamClientId("123j10iii38918-afde3");
59-
IamTokenManager.setIamSecret("aU4RyzIZdFgZWxEroo1");
60-
String header3 = IamTokenManager.getAuthorizationHeaderValue();
63+
options = new IamOptions.Builder()
64+
.apiKey(API_KEY)
65+
.url(url)
66+
.clientId("123j10iii38918-afde3")
67+
.clientSecret("aU4RyzIZdFgZWxEroo1")
68+
.build();
69+
manager = new IamTokenManager(options);
70+
String header3 = manager.getAuthorizationHeaderValue();
6171
assertNotEquals(header1, header3);
6272
assertNotEquals(header2, header3);
6373
}

0 commit comments

Comments
 (0)