Skip to content

Commit

Permalink
refactor: add check for url
Browse files Browse the repository at this point in the history
  • Loading branch information
mamoonraja committed Sep 12, 2019
1 parent 4f468e6 commit 88b47b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
25 changes: 8 additions & 17 deletions Authentication/Iam/IamAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public IamAuthenticator(Dictionary<string, string> config)
config.TryGetValue(PropNameClientSecret, out string clientSecret);
config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString);
bool.TryParse(disableSslVerficiationString, out bool disableSslVerification);
Log.Debug("IamAuthenticator:{0} {1} {2} {3}", apikey);
Init(apikey, url, clientId, clientSecret, disableSslVerification);
}

Expand Down Expand Up @@ -168,19 +167,6 @@ bool RequestToken(Callback<IamTokenResponse> callback)
if (callback == null)
throw new ArgumentNullException("successCallback");

// Use bx:bx as default auth header creds.
var clientId = "bx";
var clientSecret = "bx";

// If both the clientId and secret were specified by the user, then use them.
if (!string.IsNullOrEmpty(ClientId) && !string.IsNullOrEmpty(ClientSecret))
{
Log.Debug("not null: {0}", ClientId);

clientId = ClientId;
clientSecret = ClientSecret;
}

RESTConnector connector = new RESTConnector();
connector.URL = Url;
if (connector == null)
Expand All @@ -190,7 +176,11 @@ bool RequestToken(Callback<IamTokenResponse> callback)
req.Callback = callback;
req.HttpMethod = UnityWebRequest.kHttpVerbGET;
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
req.Headers.Add("Authorization", Utility.CreateAuthorization(clientId, clientSecret));
// If both the clientId and secret were specified by the user, then use them.
if (!string.IsNullOrEmpty(ClientId) && !string.IsNullOrEmpty(ClientSecret))
{
req.Headers.Add("Authorization", Utility.CreateAuthorization(ClientId, ClientSecret));
}
req.OnResponse = OnRequestIamTokenResponse;
req.DisableSslVerification = DisableSslVerification;
req.Forms = new Dictionary<string, RESTConnector.Form>();
Expand Down Expand Up @@ -250,9 +240,10 @@ public override void Validate()
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "url"));
}

if (string.IsNullOrEmpty(ClientSecret) || string.IsNullOrEmpty(ClientId))
if (!string.IsNullOrEmpty(ClientSecret) && string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret) && !string.IsNullOrEmpty(ClientId))
{
Log.Warning("IamTokenManager():", "Warning: Client ID and Secret must BOTH be given, or the defaults will be used.");

throw new ArgumentException("Client ID and Secret must BOTH be provided.");
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Connection/RESTConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ public bool? DisableSslVerification
///
public static RESTConnector GetConnector(Authenticator authenticator, string function, string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("The Url must not be empty or null.");
}

if (Utility.HasBadFirstOrLastCharacter(url))
{
throw new ArgumentException("The Url property is invalid. Please remove any surrounding {{, }}, or \" characters.");
Expand Down
5 changes: 5 additions & 0 deletions Connection/WSConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ public static string FixupURL(string URL)
/// <returns>The WSConnector object or null or error.</returns>
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args, string url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentNullException("The Url must not be empty or null.");
}

if (Utility.HasBadFirstOrLastCharacter(url))
{
throw new ArgumentException("The Url property is invalid. Please remove any surrounding {{, }}, or \" characters.");
Expand Down

0 comments on commit 88b47b8

Please sign in to comment.