Skip to content

Commit f475a76

Browse files
committed
refactor(tokenManager): add explicit token data checks
1 parent 66da17e commit f475a76

File tree

7 files changed

+72
-27
lines changed

7 files changed

+72
-27
lines changed

Authentication/IamTokenManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class IamTokenManager : JwtTokenManager
3838

3939
public IamTokenManager(IamTokenOptions options) : base(options)
4040
{
41+
tokenName = "access_token";
4142
if (string.IsNullOrEmpty(url))
4243
{
4344
if (!string.IsNullOrEmpty(options.IamUrl))
@@ -111,7 +112,7 @@ override protected bool RequestToken(Callback<TokenData> callback)
111112
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
112113
req.Headers.Add("Authorization", Utility.CreateAuthorization(iamClientId, iamClientSecret));
113114
req.OnResponse = OnRequestIamTokenResponse;
114-
req.DisableSslVerification = DisableSslVerification;
115+
req.DisableSslVerification = disableSslVerification;
115116
req.Forms = new Dictionary<string, RESTConnector.Form>();
116117
req.Forms["grant_type"] = new RESTConnector.Form("urn:ibm:params:oauth:grant-type:apikey");
117118
req.Forms["apikey"] = new RESTConnector.Form(iamApikey);

Authentication/Icp4dTokenManager.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public Icp4dTokenManager(Icp4dTokenOptions options) : base(options)
3737
{
3838
tokenName = "accessToken";
3939

40-
if (!string.IsNullOrEmpty(url))
40+
if (!string.IsNullOrEmpty(options.Url))
4141
{
42-
url = url + "/v1/preauth/validateAuth";
42+
url = options.Url + "/v1/preauth/validateAuth";
4343
}
4444
else if (string.IsNullOrEmpty(userAccessToken))
4545
{
@@ -56,6 +56,11 @@ public Icp4dTokenManager(Icp4dTokenOptions options) : base(options)
5656
{
5757
password = options.Password;
5858
}
59+
if (!string.IsNullOrEmpty(options.AccessToken))
60+
{
61+
userAccessToken = options.AccessToken;
62+
}
63+
disableSslVerification = options.DisableSslVerification;
5964
}
6065

6166
override protected bool RequestToken(Callback<TokenData> callback)
@@ -69,14 +74,12 @@ override protected bool RequestToken(Callback<TokenData> callback)
6974
return false;
7075

7176
RequestIcp4dTokenRequest req = new RequestIcp4dTokenRequest();
72-
req.Callback = callback;
7377
req.HttpMethod = UnityWebRequest.kHttpVerbGET;
78+
req.Callback = callback;
7479
req.Headers.Add("Content-type", "application/x-www-form-urlencoded");
75-
req.Headers.Add("Authorization", Utility.CreateAuthorization(username, password));
80+
req.Headers.Add("Authorization", Utility.CreateAuthorization(username, password));
7681
req.OnResponse = OnRequestIcp4dTokenResponse;
77-
req.DisableSslVerification = DisableSslVerification;
78-
req.Forms = new Dictionary<string, RESTConnector.Form>();
79-
82+
req.DisableSslVerification = disableSslVerification;
8083
return connector.Send(req);
8184
}
8285

@@ -147,6 +150,6 @@ public string Password
147150
}
148151
}
149152

150-
public bool disableSslVerification { get; set; }
153+
public bool DisableSslVerification { get; set; }
151154
}
152155
}

Authentication/JwtTokenManager.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JwtTokenManager
3131
protected string url;
3232
protected string tokenName;
3333
protected string userAccessToken;
34-
protected bool DisableSslVerification; // for icp4d only
34+
protected bool disableSslVerification; // for icp4d only
3535
public TokenData tokenData;
3636
private long? expireTime;
3737

@@ -71,7 +71,7 @@ public void GetToken()
7171
if (!string.IsNullOrEmpty(userAccessToken))
7272
{
7373
// 1. use user-managed token
74-
OnGetToken(new DetailedResponse<TokenData>() { Result = new TokenData() { AccessToken = userAccessToken } }, new IBMError());
74+
tokenData.AccessToken = userAccessToken;
7575
}
7676
else if (string.IsNullOrEmpty(tokenData.AccessToken) || IsTokenExpired())
7777
{
@@ -99,7 +99,7 @@ public string GetAccessToken()
9999
/// <returns></returns>
100100
public bool HasTokenData()
101101
{
102-
return tokenData.AccessToken != null;
102+
return tokenData.AccessToken != null;
103103
}
104104

105105
private void OnGetToken(DetailedResponse<TokenData> response, IBMError error)
@@ -108,6 +108,15 @@ private void OnGetToken(DetailedResponse<TokenData> response, IBMError error)
108108
}
109109
#endregion
110110

111+
#region Callback delegates
112+
/// <summary>
113+
/// Success callback delegate.
114+
/// </summary>
115+
/// <typeparam name="T">Type of the returned object.</typeparam>
116+
/// <param name="response">The returned DetailedResponse.</param>
117+
public delegate void Callback<T>(DetailedResponse<T> response, IBMError error);
118+
#endregion
119+
111120
/// <summary>
112121
/// Set a self-managed access token.
113122
/// The access token should be valid and not yet expired.
@@ -190,9 +199,9 @@ private long CalculateTimeForNewToken(string accessToken)
190199

191200
if (!string.IsNullOrEmpty(decodedResponse))
192201
{
193-
var o = JObject.Parse(decodedResponse);
194-
long exp = (long)o["exp"];
195-
long iat = (long)o["iat"];
202+
var token = JObject.Parse(decodedResponse);
203+
long exp = (long)token["exp"];
204+
long iat = (long)token["iat"];
196205

197206
double fractonOfTtl = 0.8d;
198207
long timeToLive = exp - iat;

BaseService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public BaseService(string serviceId)
8484
credentials.Url = url;
8585
}
8686
}
87-
88-
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
87+
else if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
8988
{
9089
credentials = new Credentials(Username, Password, url);
9190
}

Connection/RESTConnector.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,12 @@ public static RESTConnector GetConnector(Credentials credentials, string functio
269269
URL = credentials.Url + function,
270270
Authentication = credentials
271271
};
272-
if (connector.Authentication.iamTokenManager.HasTokenData())
272+
273+
if (connector.Authentication.HasIamTokenData())
273274
{
274275
connector.Authentication.iamTokenManager.GetToken();
275276
}
276-
else if (connector.Authentication.icp4dTokenManager.HasTokenData())
277+
else if (connector.Authentication.HasIcp4dTokenData())
277278
{
278279
connector.Authentication.icp4dTokenManager.GetToken();
279280
}
@@ -329,11 +330,11 @@ private void AddHeaders(Dictionary<string, string> headers)
329330
{
330331
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, Authentication.CreateAuthorization());
331332
}
332-
else if (Authentication.iamTokenManager.HasTokenData())
333+
else if (Authentication.HasIamTokenData())
333334
{
334335
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.iamTokenManager.GetAccessToken()));
335336
}
336-
else if (Authentication.icp4dTokenManager.HasTokenData())
337+
else if (Authentication.HasIcp4dTokenData())
337338
{
338339
headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", Authentication.icp4dTokenManager.GetAccessToken()));
339340
}

Connection/WSConnector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,12 +305,12 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
305305
{
306306
connector.Authentication = credentials;
307307
}
308-
else if (credentials.iamTokenManager.HasTokenData())
308+
else if (credentials.HasIamTokenData())
309309
{
310310
credentials.iamTokenManager.GetToken();
311311
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.iamTokenManager.GetAccessToken()));
312312
}
313-
else if (credentials.icp4dTokenManager.HasTokenData())
313+
else if (credentials.HasIcp4dTokenData())
314314
{
315315
credentials.icp4dTokenManager.GetToken();
316316
connector.Headers.Add(AUTHENTICATION_AUTHORIZATION_HEADER, string.Format("Bearer {0}", credentials.icp4dTokenManager.GetAccessToken()));

Utilities/Credentials.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public Credentials(Icp4dTokenOptions icp4dTokenOptions, string serviceUrl = null
165165
{
166166
if (!string.IsNullOrEmpty(serviceUrl))
167167
Url = serviceUrl;
168-
SetCredentials(icp4dTokenOptions);
168+
SetCredentials(icp4dTokenOptions, serviceUrl);
169169
}
170170
#endregion
171171

@@ -222,7 +222,7 @@ private void SetCredentials(TokenOptions tokenOptions, string serviceUrl = null)
222222
}
223223
}
224224

225-
private void SetCredentials(Icp4dTokenOptions icp4dTokenOptions)
225+
private void SetCredentials(Icp4dTokenOptions icp4dTokenOptions, string serviceUrl = null)
226226
{
227227
icp4dTokenManager = new Icp4dTokenManager(icp4dTokenOptions);
228228
icp4dTokenManager.GetToken();
@@ -248,12 +248,44 @@ public bool HasCredentials()
248248
}
249249

250250
/// <summary>
251-
/// Do we have credentials?
251+
/// Do we have Token Data?
252+
/// </summary>
253+
/// <returns>true if the class has a username and password.</returns>
254+
public bool HasTokenData()
255+
{
256+
return HasIamTokenData() || HasIcp4dTokenData();
257+
}
258+
259+
/// <summary>
260+
/// Do we have IAM token data?
252261
/// </summary>
253262
/// <returns>true if the class has a username and password.</returns>
254263
public bool HasIamTokenData()
255264
{
256-
return iamTokenManager.HasTokenData();
265+
if (iamTokenManager != null)
266+
{
267+
return iamTokenManager.HasTokenData();
268+
}
269+
else
270+
{
271+
return false;
272+
}
273+
}
274+
275+
/// <summary>
276+
/// Do we have ICP4D token data?
277+
/// </summary>
278+
/// <returns>true if the class has a username and password.</returns>
279+
public bool HasIcp4dTokenData()
280+
{
281+
if (icp4dTokenManager != null)
282+
{
283+
return icp4dTokenManager.HasTokenData();
284+
}
285+
else
286+
{
287+
return false;
288+
}
257289
}
258290
}
259291

0 commit comments

Comments
 (0)