Skip to content

Commit

Permalink
Merge pull request #16 from IBM/feat/serServiceUrl
Browse files Browse the repository at this point in the history
feat(setServiceUrl): use setServiceUrl instead of setServiceEndpoint
  • Loading branch information
mamoonraja authored Sep 12, 2019
2 parents 2182f33 + c4587af commit 684b901
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 62 deletions.
47 changes: 25 additions & 22 deletions Authentication/ConfigBasedAuthenticatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
using IBM.Cloud.SDK.Authentication.NoAuth;
using IBM.Cloud.SDK.Utilities;
using System.Collections.Generic;
using System;

namespace IBM.Cloud.SDK.Authentication
{
public class ConfigBasedAuthenticatorFactory
{
public static string ErrorMessageAuthTypeUnknown = "Unrecognized authentication type: {0}";

public static Authenticator GetAuthenticator(string serviceName)
{
Authenticator authenticator = null;
Expand Down Expand Up @@ -77,29 +80,29 @@ private static Authenticator CreateAuthenticator(Dictionary<string, string> prop
authType = Authenticator.AuthTypeIam;
}

switch (authType)
if (authType.Equals(Authenticator.AuthTypeNoAuth, StringComparison.InvariantCultureIgnoreCase))
{
case Authenticator.AuthTypeNoAuth:
authenticator = new NoAuthAuthenticator(props);
break;

case Authenticator.AuthTypeBasic:
authenticator = new BasicAuthenticator(props);
break;

case Authenticator.AuthTypeIam:
authenticator = new IamAuthenticator(props);
break;

case Authenticator.AuthTypeCp4d:
authenticator = new CloudPakForDataAuthenticator(props);
break;

case Authenticator.AuthTypeBearer:
authenticator = new BearerTokenAuthenticator(props);
break;
default:
break;
authenticator = new NoAuthAuthenticator(props);
}
else if (authType.Equals(Authenticator.AuthTypeBasic, StringComparison.InvariantCultureIgnoreCase))
{
authenticator = new BasicAuthenticator(props);
}
else if (authType.Equals(Authenticator.AuthTypeIam, StringComparison.InvariantCultureIgnoreCase))
{
authenticator = new IamAuthenticator(props);
}
else if (authType.Equals(Authenticator.AuthTypeCp4d, StringComparison.InvariantCultureIgnoreCase))
{
authenticator = new CloudPakForDataAuthenticator(props);
}
else if (authType.Equals(Authenticator.AuthTypeBearer, StringComparison.InvariantCultureIgnoreCase))
{
authenticator = new BearerTokenAuthenticator(props);
}
else
{
throw new ArgumentException(string.Format(ErrorMessageAuthTypeUnknown, authType));
}

return authenticator;
Expand Down
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
30 changes: 13 additions & 17 deletions BaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ namespace IBM.Cloud.SDK
{
public class BaseService
{
protected Authenticator authenticator;
protected string url;
#region Authenticator
/// <summary>
/// Gets and sets the authenticator of the service.
public Authenticator Authenticator { get; set; }
#endregion
protected string serviceUrl;
public string ServiceId { get; set; }
protected Dictionary<string, string> customRequestHeaders = new Dictionary<string, string>();
public static string PropNameServiceUrl = "URL";
Expand All @@ -39,40 +43,32 @@ public BaseService(string versionDate, Authenticator authenticator, string servi
public BaseService(Authenticator authenticator, string serviceId) {
ServiceId = serviceId;

this.authenticator = authenticator ?? throw new ArgumentNullException(ErrorMessageNoAuthenticator);

Authenticator = authenticator ?? throw new ArgumentNullException(ErrorMessageNoAuthenticator);
// Try to retrieve the service URL from either a credential file, environment, or VCAP_SERVICES.
Dictionary<string, string> props = CredentialUtils.GetServiceProperties(serviceId);
props.TryGetValue(PropNameServiceUrl, out string url);
if (!string.IsNullOrEmpty(url))
{
SetEndpoint(url);
SetServiceUrl(url);
}
}

protected void SetAuthentication(RESTConnector connector)
public void SetServiceUrl(string url)
{
if (authenticator != null)
{
authenticator.Authenticate(connector);
}
else
{
throw new ArgumentException("Authentication information was not properly configured.");
}
serviceUrl = url;
}

public void SetEndpoint(string endpoint)
public string GetServiceUrl()
{
url = endpoint;
return serviceUrl;
}

/// <summary>
/// Returns the authenticator for the service.
/// </summary>
public Authenticator GetAuthenticator()
{
return authenticator;
return Authenticator;
}

public void WithHeader(string name, string value)
Expand Down
17 changes: 15 additions & 2 deletions Connection/RESTConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,28 @@ public bool? DisableSslVerification
/// </summary>
/// <param name="authenticator">Authenticator used to authenticate service.</param>
/// <param name="function">The name of the function.</param>
/// <param name="serviceUrl">Service Url to connect to.</param>
/// <returns>Returns a RESTConnector object or null on error.</returns>
///
public static RESTConnector GetConnector(Authenticator authenticator, string function)
public static RESTConnector GetConnector(Authenticator authenticator, string function, string serviceUrl)
{
if (string.IsNullOrEmpty(serviceUrl))
{
throw new ArgumentNullException("The serviceUrl must not be empty or null.");
}

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

RESTConnector connector = new RESTConnector
{
URL = authenticator.Url + function,
URL = serviceUrl + function,
Authentication = authenticator
};

authenticator.Authenticate(connector);
return connector;
}

Expand Down
20 changes: 16 additions & 4 deletions Connection/WSConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
using System.Security.Authentication;
using IBM.Cloud.SDK.Authentication;
using System.Threading;
using System;
#if !NETFX_CORE
using UnitySDK.WebSocketSharp;
#else
using System;
using System.Threading.Tasks;
using Windows.Networking.Sockets;
using Windows.Security.Credentials;
Expand Down Expand Up @@ -322,18 +322,30 @@ public static string FixupURL(string URL)
}

/// <summary>
/// Create a WSConnector for the given service and function.
/// Create a WSConnector for the given service and function.
/// </summary>
/// <param name="authenticator">The authenticator for the service.</param>
/// <param name="function">The name of the function to connect.</param>
/// <param name="args">Additional function arguments.</param>
/// <param name="serviceUrl">Service Url to connect to.</param>
/// <returns>The WSConnector object or null or error.</returns>
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args)
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args, string serviceUrl)
{
if (string.IsNullOrEmpty(serviceUrl))
{
throw new ArgumentNullException("The serviceUrl must not be empty or null.");
}

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

WSConnector connector = new WSConnector();
connector.Authentication = authenticator;

connector.URL = FixupURL(authenticator.Url) + function + args;
connector.URL = FixupURL(serviceUrl) + function + args;
authenticator.Authenticate(connector);

return connector;
}
Expand Down

0 comments on commit 684b901

Please sign in to comment.