Skip to content

Commit 78584a2

Browse files
committed
refactor(authentication): authenticate inside connectors
1 parent 9665758 commit 78584a2

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

BaseService.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ namespace IBM.Cloud.SDK
2626
{
2727
public class BaseService
2828
{
29-
protected Authenticator authenticator;
29+
#region Authenticator
30+
/// <summary>
31+
/// Gets and sets the authenticator of the service.
32+
public Authenticator Authenticator { get; set; }
33+
#endregion
3034
protected string serviceUrl;
3135
public string ServiceId { get; set; }
3236
protected Dictionary<string, string> customRequestHeaders = new Dictionary<string, string>();
@@ -39,8 +43,7 @@ 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);
@@ -50,18 +53,6 @@ public BaseService(Authenticator authenticator, string serviceId) {
5053
}
5154
}
5255

53-
protected void SetAuthentication(RESTConnector connector)
54-
{
55-
if (authenticator != null)
56-
{
57-
authenticator.Authenticate(connector);
58-
}
59-
else
60-
{
61-
throw new ArgumentException("Authentication information was not properly configured.");
62-
}
63-
}
64-
6556
public void SetServiceUrl(string url)
6657
{
6758
serviceUrl = url;
@@ -72,7 +63,7 @@ public void SetServiceUrl(string url)
7263
/// </summary>
7364
public Authenticator GetAuthenticator()
7465
{
75-
return authenticator;
66+
return Authenticator;
7667
}
7768

7869
public void WithHeader(string name, string value)

Connection/RESTConnector.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ public static RESTConnector GetConnector(Authenticator authenticator, string fun
274274
URL = url + function,
275275
Authentication = authenticator
276276
};
277+
278+
authenticator.Authenticate(connector);
277279
return connector;
278280
}
279281

Connection/WSConnector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ public static WSConnector CreateConnector(Authenticator authenticator, string fu
339339
connector.Authentication = authenticator;
340340

341341
connector.URL = FixupURL(url) + function + args;
342+
authenticator.Authenticate(connector);
342343

343344
return connector;
344345
}

0 commit comments

Comments
 (0)