Skip to content

Commit f0da1d0

Browse files
committed
refactor(Auth): modify headers in connector
1 parent 2b913e4 commit f0da1d0

File tree

9 files changed

+118
-94
lines changed

9 files changed

+118
-94
lines changed

Authentication/Authenticator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public class Authenticator
5757
virtual public string AuthenticationType { get; }
5858

5959
/// <summary>
60-
/// Check if token data is available.
60+
/// Check if authenticator has everything it needs to authenticate. Every child class overrides this method.
6161
/// </summary>
62-
virtual public bool HasTokenData() {
62+
virtual public bool CanAuthenticate() {
6363
return false;
6464
}
6565

Authentication/BasicAuthenticator.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,23 @@ public override string AuthenticationType
7474
get { return AuthTypeBasic; }
7575
}
7676

77+
/// <summary>
78+
/// BasicAuthenticator is not waiting for token data so always return true.
79+
/// </summary>
80+
/// <returns></returns>
81+
public override bool CanAuthenticate()
82+
{
83+
return true;
84+
}
85+
7786
/// <summary>
7887
/// This method is called to authenticate an outgoing REST API request.
7988
/// Here, we'll just set the Authorization header to provide the necessary authentication info.
8089
/// </summary>
8190
/// <param name="connector"></param>
8291
public override void Authenticate(RESTConnector connector)
8392
{
84-
if (connector.Headers == null)
85-
{
86-
connector.Headers = new Dictionary<string,string>();;
87-
}
88-
connector.Headers.Add("Authorization", Utility.CreateAuthorization(Username, Password));
93+
connector.WithAuthentication(Username, Password);
8994
}
9095

9196
/// <summary>

Authentication/BearerTokenAuthenticator.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ private void Init(string bearerToken)
6262
Validate();
6363
}
6464

65+
/// <summary>
66+
/// Do we have BearerToken?
67+
/// </summary>
68+
/// <returns></returns>
69+
public override bool CanAuthenticate()
70+
{
71+
return BearerToken != null;
72+
}
73+
6574
public override string AuthenticationType
6675
{
6776
get { return AuthTypeBearer; }
@@ -74,11 +83,7 @@ public override string AuthenticationType
7483
/// <param name="connector"></param>
7584
public override void Authenticate(RESTConnector connector)
7685
{
77-
if (connector.Headers == null)
78-
{
79-
connector.Headers = new Dictionary<string,string>();;
80-
}
81-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", BearerToken));
86+
connector.WithAuthentication(BearerToken);
8287
}
8388

8489
/// <summary>
@@ -88,11 +93,7 @@ public override void Authenticate(RESTConnector connector)
8893
/// <param name="connector"></param>
8994
public override void Authenticate(WSConnector connector)
9095
{
91-
if (connector.Headers == null)
92-
{
93-
connector.Headers = new Dictionary<string,string>();;
94-
}
95-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", BearerToken));
96+
connector.WithAuthentication(BearerToken);
9697
}
9798

9899
public override void Validate()

Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public override string AuthenticationType
9999
/// Do we have TokenData?
100100
/// </summary>
101101
/// <returns></returns>
102-
public override bool HasTokenData()
102+
public override bool CanAuthenticate()
103103
{
104104
if (tokenData != null)
105105
{
@@ -111,32 +111,24 @@ public override bool HasTokenData()
111111
}
112112
}
113113

114-
private void GetToken()
114+
public override void Authenticate(RESTConnector connector)
115115
{
116-
if (tokenData == null || !tokenData.IsTokenValid())
117-
{
118-
RequestToken(OnGetToken);
119-
}
116+
connector.WithAuthentication(tokenData.AccessToken);
120117
}
121118

122-
public override void Authenticate(RESTConnector connector)
119+
public override void Authenticate(WSConnector connector)
123120
{
124-
if (connector.Headers == null)
125-
{
126-
connector.Headers = new Dictionary<string,string>();;
127-
}
128-
129-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", tokenData.AccessToken));
121+
connector.WithAuthentication(tokenData.AccessToken);
130122
}
131123

132-
public override void Authenticate(WSConnector connector)
124+
private void GetToken()
133125
{
134-
if (connector.Headers == null)
126+
if (tokenData == null || !tokenData.IsTokenValid())
135127
{
136-
connector.Headers = new Dictionary<string,string>();;
128+
RequestToken(OnGetToken);
137129
}
138-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", tokenData.AccessToken));
139130
}
131+
140132
private void OnGetToken(DetailedResponse<CloudPakForDataTokenResponse> response, IBMError error)
141133
{
142134
if (error != null)

Authentication/Iam/IamAuthenticator.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ public class IamAuthenticator : Authenticator
3737
public string ClientId { get; set; }
3838
public string ClientSecret { get; set; }
3939
public Dictionary<string, string> Headers { get; set; }
40-
private readonly string defaultUrl = "https://iam.cloud.ibm.com/identity/token";
41-
4240
// This field holds an access token and its expiration time.
4341
private IamToken tokenData;
4442

@@ -85,7 +83,7 @@ private void Init(string apikey, string url = null, string clientId = null, stri
8583

8684
if (string.IsNullOrEmpty(url))
8785
{
88-
url = defaultUrl;
86+
url = DefaultIamUrl;
8987
}
9088
this.Url = url;
9189
if (!string.IsNullOrEmpty(clientId))
@@ -118,7 +116,7 @@ public override string AuthenticationType
118116
/// Do we have TokenData?
119117
/// </summary>
120118
/// <returns></returns>
121-
public override bool HasTokenData()
119+
public override bool CanAuthenticate()
122120
{
123121
if (tokenData != null)
124122
{
@@ -130,6 +128,16 @@ public override bool HasTokenData()
130128
}
131129
}
132130

131+
public override void Authenticate(RESTConnector connector)
132+
{
133+
connector.WithAuthentication(tokenData.AccessToken);
134+
}
135+
136+
public override void Authenticate(WSConnector connector)
137+
{
138+
connector.WithAuthentication(tokenData.AccessToken);
139+
}
140+
133141
private void OnGetToken(DetailedResponse<IamTokenResponse> response, IBMError error)
134142
{
135143
if (error != null)
@@ -149,25 +157,6 @@ private void GetToken()
149157
}
150158
}
151159

152-
public override void Authenticate(RESTConnector connector)
153-
{
154-
if (connector.Headers == null)
155-
{
156-
connector.Headers = new Dictionary<string,string>();;
157-
}
158-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", tokenData.AccessToken));
159-
}
160-
161-
public override void Authenticate(WSConnector connector)
162-
{
163-
if (connector.Headers == null)
164-
{
165-
connector.Headers = new Dictionary<string,string>();;
166-
}
167-
connector.Headers.Add("Authorization", string.Format("Bearer {0}", tokenData.AccessToken));
168-
}
169-
170-
171160
#region Request Token
172161
/// <summary>
173162
/// Request an IAM token using an API key.

Authentication/NoAuthAuthenticator.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public NoAuthAuthenticator() { }
2828

2929
public NoAuthAuthenticator(Dictionary<string, string> config) { }
3030

31+
/// <summary>
32+
/// Always return true
33+
/// </summary>
34+
/// <returns></returns>
35+
public override bool CanAuthenticate()
36+
{
37+
return true;
38+
}
39+
3140
public override string AuthenticationType
3241
{
3342
get { return AuthTypeNoAuth; }

BaseService.cs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,12 @@ namespace IBM.Cloud.SDK
2727
public class BaseService
2828
{
2929
protected Authenticator authenticator;
30-
protected string Url;
30+
protected string url;
3131
public string ServiceId { get; set; }
3232
protected Dictionary<string, string> customRequestHeaders = new Dictionary<string, string>();
3333
public static string PropNameServiceUrl = "URL";
34-
public static string propnameDisableSsl = "DISABLE_SSL";
34+
public static string PropNameServiceDisableSslVerification = "DISABLE_SSL";
3535
private const string ErrorMessageNoAuthenticator = "Authentication information was not properly configured.";
36-
public BaseService(string serviceId)
37-
{
38-
authenticator = ConfigBasedAuthenticatorFactory.GetAuthenticator(serviceId);
39-
}
40-
41-
public BaseService(string versionDate, string serviceId) : this(serviceId) { }
4236

4337
public BaseService(string versionDate, Authenticator authenticator, string serviceId) : this(authenticator, serviceId) { }
4438

@@ -54,25 +48,6 @@ public BaseService(Authenticator authenticator, string serviceId) {
5448
{
5549
SetEndpoint(url);
5650
}
57-
58-
// Check to see if "disable ssl" was set in the service properties.
59-
// bool disableSsl = false;
60-
// props.TryGetValue(PropNameServiceDisableSslVerification, out string tempDisableSsl);
61-
// if (!string.IsNullOrEmpty(tempDisableSsl))
62-
// {
63-
// bool.TryParse(tempDisableSsl, out disableSsl);
64-
// }
65-
66-
// DisableSslVerification(disableSsl);
67-
}
68-
69-
protected BaseService(string versionDate, string serviceId, string url)
70-
{
71-
ServiceId = serviceId;
72-
authenticator = new NoAuthAuthenticator();
73-
74-
if (!string.IsNullOrEmpty(url))
75-
Url = url;
7651
}
7752

7853
protected void SetAuthentication(RESTConnector connector)
@@ -87,9 +62,9 @@ protected void SetAuthentication(RESTConnector connector)
8762
}
8863
}
8964

90-
public void SetEndpoint(string url)
65+
public void SetEndpoint(string endpoint)
9166
{
92-
Url = url;
67+
url = endpoint;
9368
}
9469

9570
/// <summary>

Connection/RESTConnector.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ public bool Send(Request request)
302302
}
303303
#endregion
304304

305+
#region Authenticate request
306+
/// <summary>
307+
/// Authenticate based on bearer token
308+
/// </summary>
309+
/// <param name="request">The request object.</param>
310+
/// <returns>true is returned on success, false is returned if the Request can't be sent.</returns>
311+
public void WithAuthentication(string bearerToken)
312+
{
313+
if (Headers == null)
314+
{
315+
Headers = new Dictionary<string,string>();;
316+
}
317+
Headers.Add("Authorization", string.Format("Bearer {0}", bearerToken));
318+
}
319+
320+
/// <summary>
321+
/// Authenticate based on bearer token
322+
/// </summary>
323+
/// <param name="request">The request object.</param>
324+
/// <returns>true is returned on success, false is returned if the Request can't be sent.</returns>
325+
public void WithAuthentication(string username, string password)
326+
{
327+
if (Headers == null)
328+
{
329+
Headers = new Dictionary<string,string>();;
330+
}
331+
Headers.Add("Authorization", Utility.CreateAuthorization(username, password));
332+
}
333+
#endregion
334+
305335
#region Private Data
306336
private int _activeConnections = 0;
307337
private Queue<Request> _requests = new Queue<Request>();

Connection/WSConnector.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ public TextMessage(string text, Dictionary<string, string> headers = null)
140140
};
141141
#endregion
142142

143+
#region Authenticate request
144+
/// <summary>
145+
/// Authenticate based on bearer token
146+
/// </summary>
147+
/// <param name="request">The request object.</param>
148+
/// <returns>true is returned on success, false is returned if the Request can't be sent.</returns>
149+
public void WithAuthentication(string bearerToken)
150+
{
151+
if (Headers == null)
152+
{
153+
Headers = new Dictionary<string,string>();;
154+
}
155+
Headers.Add("Authorization", string.Format("Bearer {0}", bearerToken));
156+
}
157+
158+
/// <summary>
159+
/// Authenticate based on bearer token
160+
/// </summary>
161+
/// <param name="request">The request object.</param>
162+
/// <returns>true is returned on success, false is returned if the Request can't be sent.</returns>
163+
public void WithAuthentication(string username, string password)
164+
{
165+
if (Headers == null)
166+
{
167+
Headers = new Dictionary<string,string>();;
168+
}
169+
Headers.Add("Authorization", Utility.CreateAuthorization(username, password));
170+
}
171+
#endregion
172+
143173
#region Public Properties
144174
/// <summary>
145175
/// This delegate is invoked when the connection is closed.
@@ -302,14 +332,7 @@ public static string FixupURL(string URL)
302332
public static WSConnector CreateConnector(Authenticator authenticator, string function, string args)
303333
{
304334
WSConnector connector = new WSConnector();
305-
if (authenticator.AuthenticationType == "basic")
306-
{
307-
connector.Authentication = authenticator;
308-
}
309-
else if (authenticator.AuthenticationType == "iam" || authenticator.AuthenticationType == "cp4d")
310-
{
311-
authenticator.Authenticate(connector);
312-
}
335+
connector.Authentication = authenticator;
313336

314337
connector.URL = FixupURL(authenticator.Url) + function + args;
315338

0 commit comments

Comments
 (0)