Skip to content

Commit d709062

Browse files
authored
Merge pull request #492 from watson-developer-cloud/5865-curly-brackets-url-credentials
Check for bad characters in username, password, apiKey and service url
2 parents 9e07476 + 6a65118 commit d709062

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

Scripts/Utilities/Credentials.cs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class Credentials
3636
private IamTokenData _iamTokenData;
3737
private string _iamApiKey;
3838
private string _userAcessToken;
39+
private string url;
40+
private string username;
41+
private string password;
3942
private const string APIKEY_AS_USERNAME = "apikey";
4043
private const string ICP_PREFIX = "icp-";
4144
#endregion
@@ -44,11 +47,39 @@ public class Credentials
4447
/// <summary>
4548
/// The user name.
4649
/// </summary>
47-
public string Username { get; set; }
50+
public string Username
51+
{
52+
get { return username; }
53+
set
54+
{
55+
if (!Utility.HasBadFirstOrLastCharacter(value))
56+
{
57+
username = value;
58+
}
59+
else
60+
{
61+
throw new WatsonException("The username shouldn't start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username.");
62+
}
63+
}
64+
}
4865
/// <summary>
4966
/// The password.
5067
/// </summary>
51-
public string Password { get; set; }
68+
public string Password
69+
{
70+
get { return password; }
71+
set
72+
{
73+
if (!Utility.HasBadFirstOrLastCharacter(value))
74+
{
75+
password = value;
76+
}
77+
else
78+
{
79+
throw new WatsonException("The password shouldn't start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your password.");
80+
}
81+
}
82+
}
5283
/// <summary>
5384
/// The Api Key.
5485
/// </summary>
@@ -65,7 +96,21 @@ public string WatsonAuthenticationToken
6596
/// <summary>
6697
/// The service endpoint.
6798
/// </summary>
68-
public string Url { get; set; }
99+
public string Url
100+
{
101+
get { return url; }
102+
set
103+
{
104+
if (!Utility.HasBadFirstOrLastCharacter(value))
105+
{
106+
url = value;
107+
}
108+
else
109+
{
110+
throw new WatsonException("The service URL shouldn't start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your service url.");
111+
}
112+
}
113+
}
69114

70115
/// <summary>
71116
/// The IAM access token.
@@ -645,8 +690,26 @@ public class Credential
645690
[fsObject]
646691
public class TokenOptions
647692
{
693+
private string iamApiKey;
648694
[fsProperty("iamApiKey")]
649-
public string IamApiKey { get; set; }
695+
public string IamApiKey
696+
{
697+
get
698+
{
699+
return iamApiKey;
700+
}
701+
set
702+
{
703+
if (!Utility.HasBadFirstOrLastCharacter(value))
704+
{
705+
iamApiKey = value;
706+
}
707+
else
708+
{
709+
throw new WatsonException("The credentials shouldn't start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your credentials");
710+
}
711+
}
712+
}
650713
[fsProperty("iamAcessToken")]
651714
public string IamAccessToken { get; set; }
652715
[fsProperty("iamUrl")]

Scripts/Utilities/Utility.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ private static void OnGetWatsonTokenResp(RESTConnector.Request req, RESTConnecto
12541254
}
12551255
#endregion
12561256

1257-
#region
1257+
#region CreateAuthorization
12581258
/// <summary>
12591259
/// Create basic authentication header data for REST requests.
12601260
/// </summary>
@@ -1266,6 +1266,13 @@ public static string CreateAuthorization(string username, string password)
12661266
return "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
12671267
}
12681268
#endregion
1269+
1270+
#region Has Bad First Character
1271+
public static bool HasBadFirstOrLastCharacter(string value)
1272+
{
1273+
return value.StartsWith("{") || value.StartsWith("\"") || value.EndsWith("}") || value.EndsWith("\"");
1274+
}
1275+
#endregion
12691276
}
12701277

12711278
/// <summary>

0 commit comments

Comments
 (0)