Skip to content

Check for bad characters in username, password, apiKey and service url #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions Scripts/Utilities/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class Credentials
private IamTokenData _iamTokenData;
private string _iamApiKey;
private string _userAcessToken;
private string url;
private string username;
private string password;
private const string APIKEY_AS_USERNAME = "apikey";
private const string ICP_PREFIX = "icp-";
#endregion
Expand All @@ -44,11 +47,39 @@ public class Credentials
/// <summary>
/// The user name.
/// </summary>
public string Username { get; set; }
public string Username
{
get { return username; }
set
{
if (!Utility.HasBadFirstOrLastCharacter(value))
{
username = value;
}
else
{
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.");
}
}
}
/// <summary>
/// The password.
/// </summary>
public string Password { get; set; }
public string Password
{
get { return password; }
set
{
if (!Utility.HasBadFirstOrLastCharacter(value))
{
password = value;
}
else
{
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.");
}
}
}
/// <summary>
/// The Api Key.
/// </summary>
Expand All @@ -65,7 +96,21 @@ public string WatsonAuthenticationToken
/// <summary>
/// The service endpoint.
/// </summary>
public string Url { get; set; }
public string Url
{
get { return url; }
set
{
if (!Utility.HasBadFirstOrLastCharacter(value))
{
url = value;
}
else
{
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.");
}
}
}

/// <summary>
/// The IAM access token.
Expand Down Expand Up @@ -645,8 +690,26 @@ public class Credential
[fsObject]
public class TokenOptions
{
private string iamApiKey;
[fsProperty("iamApiKey")]
public string IamApiKey { get; set; }
public string IamApiKey
{
get
{
return iamApiKey;
}
set
{
if (!Utility.HasBadFirstOrLastCharacter(value))
{
iamApiKey = value;
}
else
{
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");
}
}
}
[fsProperty("iamAcessToken")]
public string IamAccessToken { get; set; }
[fsProperty("iamUrl")]
Expand Down
9 changes: 8 additions & 1 deletion Scripts/Utilities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ private static void OnGetWatsonTokenResp(RESTConnector.Request req, RESTConnecto
}
#endregion

#region
#region CreateAuthorization
/// <summary>
/// Create basic authentication header data for REST requests.
/// </summary>
Expand All @@ -1266,6 +1266,13 @@ public static string CreateAuthorization(string username, string password)
return "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password));
}
#endregion

#region Has Bad First Character
public static bool HasBadFirstOrLastCharacter(string value)
{
return value.StartsWith("{") || value.StartsWith("\"") || value.EndsWith("}") || value.EndsWith("\"");
}
#endregion
}

/// <summary>
Expand Down