Skip to content

Commit 5a6761b

Browse files
committed
refactor(core): Add exceptions for bad characters in credentials
1 parent 5b8b443 commit 5a6761b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ public WatsonService(final String name) {
121121
client = configureHttpClient();
122122
}
123123

124+
/**
125+
* Calls appropriate methods to set credential values based on parsed ServiceCredentials object.
126+
*
127+
* @param serviceCredentials object containing parsed credential values
128+
*/
124129
private void setCredentialFields(CredentialUtils.ServiceCredentials serviceCredentials) {
125130
setEndPoint(serviceCredentials.getUrl());
126131

@@ -341,6 +346,11 @@ public String getName() {
341346
* @param apiKey the new API key
342347
*/
343348
public void setApiKey(String apiKey) {
349+
if (CredentialUtils.hasBadStartOrEndChar(apiKey)) {
350+
throw new IllegalArgumentException("The API key shouldn't start or end with curly brackets or quotes. Please "
351+
+ "remove any surrounding {, }, or \" characters.");
352+
}
353+
344354
if (this.endPoint.equals(this.defaultEndPoint)) {
345355
this.endPoint = "https://gateway-a.watsonplatform.net/visual-recognition/api";
346356
}
@@ -376,6 +386,11 @@ protected void setAuthentication(final Builder builder) {
376386
* @param endPoint the new end point. Will be ignored if empty or null
377387
*/
378388
public void setEndPoint(final String endPoint) {
389+
if (CredentialUtils.hasBadStartOrEndChar(endPoint)) {
390+
throw new IllegalArgumentException("The URL shouldn't start or end with curly brackets or quotes. Please "
391+
+ "remove any surrounding {, }, or \" characters.");
392+
}
393+
379394
if ((endPoint != null) && !endPoint.isEmpty()) {
380395
String newEndPoint = endPoint.endsWith("/") ? endPoint.substring(0, endPoint.length() - 1) : endPoint;
381396
if (this.endPoint == null) {
@@ -392,6 +407,11 @@ public void setEndPoint(final String endPoint) {
392407
* @param password the password
393408
*/
394409
public void setUsernameAndPassword(final String username, final String password) {
410+
if (CredentialUtils.hasBadStartOrEndChar(username) || CredentialUtils.hasBadStartOrEndChar(password)) {
411+
throw new IllegalArgumentException("The username and password shouldn't start or end with curly brackets or "
412+
+ "quotes. Please remove any surrounding {, }, or \" characters.");
413+
}
414+
395415
// we'll perform the token exchange for users UNLESS they're on ICP
396416
if (username.equals(APIKEY_AS_USERNAME) && !password.startsWith(ICP_PREFIX)) {
397417
IamOptions iamOptions = new IamOptions.Builder()

core/src/main/java/com/ibm/watson/developer_cloud/service/security/IamTokenManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.ibm.watson.developer_cloud.http.HttpMediaType;
1818
import com.ibm.watson.developer_cloud.http.RequestBuilder;
1919
import com.ibm.watson.developer_cloud.http.ResponseConverter;
20+
import com.ibm.watson.developer_cloud.util.CredentialUtils;
2021
import com.ibm.watson.developer_cloud.util.ResponseConverterUtils;
2122
import okhttp3.Call;
2223
import okhttp3.FormBody;
@@ -44,6 +45,11 @@ public class IamTokenManager {
4445
private static final String REFRESH_TOKEN = "refresh_token";
4546

4647
public IamTokenManager(IamOptions options) {
48+
if (CredentialUtils.hasBadStartOrEndChar(options.getApiKey())) {
49+
throw new IllegalArgumentException("The IAM API key shouldn't start or end with curly brackets or quotes. "
50+
+ "Please remove any surrounding {, }, or \" characters.");
51+
}
52+
4753
this.apiKey = options.getApiKey();
4854
this.url = (options.getUrl() != null) ? options.getUrl() : DEFAULT_IAM_URL;
4955
this.userManagedAccessToken = options.getAccessToken();

0 commit comments

Comments
 (0)