Skip to content

Commit 684b901

Browse files
authored
Merge pull request #16 from IBM/feat/serServiceUrl
feat(setServiceUrl): use setServiceUrl instead of setServiceEndpoint
2 parents 2182f33 + c4587af commit 684b901

File tree

5 files changed

+77
-62
lines changed

5 files changed

+77
-62
lines changed

Authentication/ConfigBasedAuthenticatorFactory.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@
2222
using IBM.Cloud.SDK.Authentication.NoAuth;
2323
using IBM.Cloud.SDK.Utilities;
2424
using System.Collections.Generic;
25+
using System;
2526

2627
namespace IBM.Cloud.SDK.Authentication
2728
{
2829
public class ConfigBasedAuthenticatorFactory
2930
{
31+
public static string ErrorMessageAuthTypeUnknown = "Unrecognized authentication type: {0}";
32+
3033
public static Authenticator GetAuthenticator(string serviceName)
3134
{
3235
Authenticator authenticator = null;
@@ -77,29 +80,29 @@ private static Authenticator CreateAuthenticator(Dictionary<string, string> prop
7780
authType = Authenticator.AuthTypeIam;
7881
}
7982

80-
switch (authType)
83+
if (authType.Equals(Authenticator.AuthTypeNoAuth, StringComparison.InvariantCultureIgnoreCase))
8184
{
82-
case Authenticator.AuthTypeNoAuth:
83-
authenticator = new NoAuthAuthenticator(props);
84-
break;
85-
86-
case Authenticator.AuthTypeBasic:
87-
authenticator = new BasicAuthenticator(props);
88-
break;
89-
90-
case Authenticator.AuthTypeIam:
91-
authenticator = new IamAuthenticator(props);
92-
break;
93-
94-
case Authenticator.AuthTypeCp4d:
95-
authenticator = new CloudPakForDataAuthenticator(props);
96-
break;
97-
98-
case Authenticator.AuthTypeBearer:
99-
authenticator = new BearerTokenAuthenticator(props);
100-
break;
101-
default:
102-
break;
85+
authenticator = new NoAuthAuthenticator(props);
86+
}
87+
else if (authType.Equals(Authenticator.AuthTypeBasic, StringComparison.InvariantCultureIgnoreCase))
88+
{
89+
authenticator = new BasicAuthenticator(props);
90+
}
91+
else if (authType.Equals(Authenticator.AuthTypeIam, StringComparison.InvariantCultureIgnoreCase))
92+
{
93+
authenticator = new IamAuthenticator(props);
94+
}
95+
else if (authType.Equals(Authenticator.AuthTypeCp4d, StringComparison.InvariantCultureIgnoreCase))
96+
{
97+
authenticator = new CloudPakForDataAuthenticator(props);
98+
}
99+
else if (authType.Equals(Authenticator.AuthTypeBearer, StringComparison.InvariantCultureIgnoreCase))
100+
{
101+
authenticator = new BearerTokenAuthenticator(props);
102+
}
103+
else
104+
{
105+
throw new ArgumentException(string.Format(ErrorMessageAuthTypeUnknown, authType));
103106
}
104107

105108
return authenticator;

Authentication/Iam/IamAuthenticator.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public IamAuthenticator(Dictionary<string, string> config)
7373
config.TryGetValue(PropNameClientSecret, out string clientSecret);
7474
config.TryGetValue(PropNameDisableSslVerification, out string disableSslVerficiationString);
7575
bool.TryParse(disableSslVerficiationString, out bool disableSslVerification);
76-
Log.Debug("IamAuthenticator:{0} {1} {2} {3}", apikey);
7776
Init(apikey, url, clientId, clientSecret, disableSslVerification);
7877
}
7978

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

171-
// Use bx:bx as default auth header creds.
172-
var clientId = "bx";
173-
var clientSecret = "bx";
174-
175-
// If both the clientId and secret were specified by the user, then use them.
176-
if (!string.IsNullOrEmpty(ClientId) && !string.IsNullOrEmpty(ClientSecret))
177-
{
178-
Log.Debug("not null: {0}", ClientId);
179-
180-
clientId = ClientId;
181-
clientSecret = ClientSecret;
182-
}
183-
184170
RESTConnector connector = new RESTConnector();
185171
connector.URL = Url;
186172
if (connector == null)
@@ -190,7 +176,11 @@ bool RequestToken(Callback<IamTokenResponse> callback)
190176
req.Callback = callback;
191177
req.HttpMethod = UnityWebRequest.kHttpVerbGET;
192178
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
193-
req.Headers.Add("Authorization", Utility.CreateAuthorization(clientId, clientSecret));
179+
// If both the clientId and secret were specified by the user, then use them.
180+
if (!string.IsNullOrEmpty(ClientId) && !string.IsNullOrEmpty(ClientSecret))
181+
{
182+
req.Headers.Add("Authorization", Utility.CreateAuthorization(ClientId, ClientSecret));
183+
}
194184
req.OnResponse = OnRequestIamTokenResponse;
195185
req.DisableSslVerification = DisableSslVerification;
196186
req.Forms = new Dictionary<string, RESTConnector.Form>();
@@ -250,9 +240,10 @@ public override void Validate()
250240
throw new ArgumentException(string.Format(ErrorMessagePropInvalid, "url"));
251241
}
252242

253-
if (string.IsNullOrEmpty(ClientSecret) || string.IsNullOrEmpty(ClientId))
243+
if (!string.IsNullOrEmpty(ClientSecret) && string.IsNullOrEmpty(ClientId) || string.IsNullOrEmpty(ClientSecret) && !string.IsNullOrEmpty(ClientId))
254244
{
255-
Log.Warning("IamTokenManager():", "Warning: Client ID and Secret must BOTH be given, or the defaults will be used.");
245+
246+
throw new ArgumentException("Client ID and Secret must BOTH be provided.");
256247
}
257248
}
258249
}

BaseService.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ namespace IBM.Cloud.SDK
2626
{
2727
public class BaseService
2828
{
29-
protected Authenticator authenticator;
30-
protected string url;
29+
#region Authenticator
30+
/// <summary>
31+
/// Gets and sets the authenticator of the service.
32+
public Authenticator Authenticator { get; set; }
33+
#endregion
34+
protected string serviceUrl;
3135
public string ServiceId { get; set; }
3236
protected Dictionary<string, string> customRequestHeaders = new Dictionary<string, string>();
3337
public static string PropNameServiceUrl = "URL";
@@ -39,40 +43,32 @@ public BaseService(string versionDate, Authenticator authenticator, string servi
3943
public BaseService(Authenticator authenticator, string serviceId) {
4044
ServiceId = serviceId;
4145

42-
this.authenticator = authenticator ?? throw new ArgumentNullException(ErrorMessageNoAuthenticator);
43-
46+
Authenticator = authenticator ?? throw new ArgumentNullException(ErrorMessageNoAuthenticator);
4447
// Try to retrieve the service URL from either a credential file, environment, or VCAP_SERVICES.
4548
Dictionary<string, string> props = CredentialUtils.GetServiceProperties(serviceId);
4649
props.TryGetValue(PropNameServiceUrl, out string url);
4750
if (!string.IsNullOrEmpty(url))
4851
{
49-
SetEndpoint(url);
52+
SetServiceUrl(url);
5053
}
5154
}
5255

53-
protected void SetAuthentication(RESTConnector connector)
56+
public void SetServiceUrl(string url)
5457
{
55-
if (authenticator != null)
56-
{
57-
authenticator.Authenticate(connector);
58-
}
59-
else
60-
{
61-
throw new ArgumentException("Authentication information was not properly configured.");
62-
}
58+
serviceUrl = url;
6359
}
6460

65-
public void SetEndpoint(string endpoint)
61+
public string GetServiceUrl()
6662
{
67-
url = endpoint;
63+
return serviceUrl;
6864
}
6965

7066
/// <summary>
7167
/// Returns the authenticator for the service.
7268
/// </summary>
7369
public Authenticator GetAuthenticator()
7470
{
75-
return authenticator;
71+
return Authenticator;
7672
}
7773

7874
public void WithHeader(string name, string value)

Connection/RESTConnector.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,28 @@ public bool? DisableSslVerification
260260
/// </summary>
261261
/// <param name="authenticator">Authenticator used to authenticate service.</param>
262262
/// <param name="function">The name of the function.</param>
263+
/// <param name="serviceUrl">Service Url to connect to.</param>
263264
/// <returns>Returns a RESTConnector object or null on error.</returns>
264265
///
265-
public static RESTConnector GetConnector(Authenticator authenticator, string function)
266+
public static RESTConnector GetConnector(Authenticator authenticator, string function, string serviceUrl)
266267
{
268+
if (string.IsNullOrEmpty(serviceUrl))
269+
{
270+
throw new ArgumentNullException("The serviceUrl must not be empty or null.");
271+
}
272+
273+
if (Utility.HasBadFirstOrLastCharacter(serviceUrl))
274+
{
275+
throw new ArgumentException("The serviceUrl property is invalid. Please remove any surrounding {{, }}, or \" characters.");
276+
}
277+
267278
RESTConnector connector = new RESTConnector
268279
{
269-
URL = authenticator.Url + function,
280+
URL = serviceUrl + function,
270281
Authentication = authenticator
271282
};
283+
284+
authenticator.Authenticate(connector);
272285
return connector;
273286
}
274287

Connection/WSConnector.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
using System.Security.Authentication;
2626
using IBM.Cloud.SDK.Authentication;
2727
using System.Threading;
28+
using System;
2829
#if !NETFX_CORE
2930
using UnitySDK.WebSocketSharp;
3031
#else
31-
using System;
3232
using System.Threading.Tasks;
3333
using Windows.Networking.Sockets;
3434
using Windows.Security.Credentials;
@@ -322,18 +322,30 @@ public static string FixupURL(string URL)
322322
}
323323

324324
/// <summary>
325-
/// Create a WSConnector for the given service and function.
325+
/// Create a WSConnector for the given service and function.
326326
/// </summary>
327327
/// <param name="authenticator">The authenticator for the service.</param>
328328
/// <param name="function">The name of the function to connect.</param>
329329
/// <param name="args">Additional function arguments.</param>
330+
/// <param name="serviceUrl">Service Url to connect to.</param>
330331
/// <returns>The WSConnector object or null or error.</returns>
331-
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args)
332+
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args, string serviceUrl)
332333
{
334+
if (string.IsNullOrEmpty(serviceUrl))
335+
{
336+
throw new ArgumentNullException("The serviceUrl must not be empty or null.");
337+
}
338+
339+
if (Utility.HasBadFirstOrLastCharacter(serviceUrl))
340+
{
341+
throw new ArgumentException("The serviceUrl property is invalid. Please remove any surrounding {{, }}, or \" characters.");
342+
}
343+
333344
WSConnector connector = new WSConnector();
334345
connector.Authentication = authenticator;
335346

336-
connector.URL = FixupURL(authenticator.Url) + function + args;
347+
connector.URL = FixupURL(serviceUrl) + function + args;
348+
authenticator.Authenticate(connector);
337349

338350
return connector;
339351
}

0 commit comments

Comments
 (0)