From c1b13163f1878d909a8e1b0fc2b3ac9820ebe927 Mon Sep 17 00:00:00 2001 From: Esam Date: Tue, 9 Jun 2020 10:57:02 +0300 Subject: [PATCH 1/2] feat (auth-ready): Added Authenticator.IsDoneInitializing to replace while (!auth.CanAuthenticate) --- Authentication/Authenticator.cs | 6 ++++++ .../CloudPakForDataAuthenticator.cs | 4 ++++ Authentication/Iam/IamAuthenticator.cs | 15 ++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Authentication/Authenticator.cs b/Authentication/Authenticator.cs index e4ca7f7..5df3785 100644 --- a/Authentication/Authenticator.cs +++ b/Authentication/Authenticator.cs @@ -54,6 +54,12 @@ public class Authenticator /// virtual public string AuthenticationType { get; } + /// + /// Returns true when the initialization phase is done, regardless whether it succeeded or not. + /// Override to indicate if the authenticator is still waiting for a token. + /// + virtual public bool IsDoneInitializing => true; + /// /// Check if authenticator has everything it needs to authenticate. Every child class overrides this method. /// diff --git a/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs b/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs index 453e615..b2e8448 100644 --- a/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs +++ b/Authentication/CloudPakForData/CloudPakForDataAuthenticator.cs @@ -42,6 +42,9 @@ public class CloudPakForDataAuthenticator : Authenticator // This field holds an access token and its expiration time. private CloudPakForDataToken tokenData; + private bool isDoneInitializing = false; + + public override bool IsDoneInitializing => isDoneInitializing; /// /// Constructs a CloudPakForDataAuthenticator with all properties. @@ -131,6 +134,7 @@ private void GetToken() private void OnGetToken(DetailedResponse response, IBMError error) { + isDoneInitializing = true; if (error != null) { Log.Error("Credentials.OnRequestCloudPakForDataTokenResponse()", "Exception: {0}", error.ToString()); diff --git a/Authentication/Iam/IamAuthenticator.cs b/Authentication/Iam/IamAuthenticator.cs index c7877f7..b407736 100644 --- a/Authentication/Iam/IamAuthenticator.cs +++ b/Authentication/Iam/IamAuthenticator.cs @@ -39,6 +39,7 @@ public class IamAuthenticator : Authenticator public Dictionary Headers { get; set; } // This field holds an access token and its expiration time. private IamToken tokenData; + private bool isDoneInitializing = false; private const string DefaultIamUrl = "https://iam.cloud.ibm.com/identity/token"; private const string GrantType = "grant_type"; @@ -111,6 +112,8 @@ public override string AuthenticationType get { return AuthTypeIam; } } + public override bool IsDoneInitializing => isDoneInitializing; + /// /// Do we have TokenData? /// @@ -139,6 +142,7 @@ public override void Authenticate(WSConnector connector) private void OnGetToken(DetailedResponse response, IBMError error) { + isDoneInitializing = true; if (error != null) { Log.Error("Credentials.OnRequestIamTokenResponse()", "Exception: {0}", error.ToString()); @@ -200,14 +204,15 @@ private void OnRequestIamTokenResponse(RESTConnector.Request req, RESTConnector. { DetailedResponse response = new DetailedResponse(); response.Result = new IamTokenResponse(); - foreach (KeyValuePair kvp in resp.Headers) - { - response.Headers.Add(kvp.Key, kvp.Value); - } - response.StatusCode = resp.HttpResponseCode; try { + foreach (KeyValuePair kvp in resp.Headers) + { + response.Headers.Add(kvp.Key, kvp.Value); + } + response.StatusCode = resp.HttpResponseCode; + string json = Encoding.UTF8.GetString(resp.Data); response.Result = JsonConvert.DeserializeObject(json); response.Response = json; From 63de687c128c903a16c296b1868cc17c0b0d821b Mon Sep 17 00:00:00 2001 From: Esam Date: Tue, 9 Jun 2020 11:01:09 +0300 Subject: [PATCH 2/2] fix: exception while parsing REST error message. --- Connection/RESTConnector.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Connection/RESTConnector.cs b/Connection/RESTConnector.cs index a7c713f..31a44eb 100644 --- a/Connection/RESTConnector.cs +++ b/Connection/RESTConnector.cs @@ -658,7 +658,16 @@ private IEnumerator ProcessRequestQueue() #region Get Error Message public string GetErrorMessage(string error) { - dynamic deserializedObject = Json.Deserialize(error); + dynamic deserializedObject; + try + { + deserializedObject = Json.Deserialize(error); + } + catch (Exception) + { + Log.Error("RESTConnector.GetErrorMessage()", "Non-JSON Error Received!"); + return "Unknown error"; + } if ((deserializedObject as Dictionary).ContainsKey("errors")) { return deserializedObject["errors"][0]["message"];