Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Auth Ready, resolves IBM/unity-sdk-core#32 #33

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class Authenticator
/// </summary>
virtual public string AuthenticationType { get; }

/// <summary>
/// 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.
/// </summary>
virtual public bool IsDoneInitializing => true;

/// <summary>
/// Check if authenticator has everything it needs to authenticate. Every child class overrides this method.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Constructs a CloudPakForDataAuthenticator with all properties.
Expand Down Expand Up @@ -131,6 +134,7 @@ private void GetToken()

private void OnGetToken(DetailedResponse<CloudPakForDataTokenResponse> response, IBMError error)
{
isDoneInitializing = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should isDoneInitializing be called after we throw the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, yes.

If we move the new line a few lines down (inside else statement) so it's not called when an error happens, then there would be no way left for a consumer class to know if an error happened.

If you think this is not clear enough, maybe we should have an IsWaiting field instead, it would have the opposite value to IsDoneInitializing, and maybe makes thing more clear?

if (error != null)
{
Log.Error("Credentials.OnRequestCloudPakForDataTokenResponse()", "Exception: {0}", error.ToString());
Expand Down
15 changes: 10 additions & 5 deletions Authentication/Iam/IamAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class IamAuthenticator : Authenticator
public Dictionary<string, string> 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";
Expand Down Expand Up @@ -111,6 +112,8 @@ public override string AuthenticationType
get { return AuthTypeIam; }
}

public override bool IsDoneInitializing => isDoneInitializing;

/// <summary>
/// Do we have TokenData?
/// </summary>
Expand Down Expand Up @@ -139,6 +142,7 @@ public override void Authenticate(WSConnector connector)

private void OnGetToken(DetailedResponse<IamTokenResponse> response, IBMError error)
{
isDoneInitializing = true;
if (error != null)
{
Log.Error("Credentials.OnRequestIamTokenResponse()", "Exception: {0}", error.ToString());
Expand Down Expand Up @@ -200,14 +204,15 @@ private void OnRequestIamTokenResponse(RESTConnector.Request req, RESTConnector.
{
DetailedResponse<IamTokenResponse> response = new DetailedResponse<IamTokenResponse>();
response.Result = new IamTokenResponse();
foreach (KeyValuePair<string, string> kvp in resp.Headers)
{
response.Headers.Add(kvp.Key, kvp.Value);
}
response.StatusCode = resp.HttpResponseCode;

try
{
foreach (KeyValuePair<string, string> 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<IamTokenResponse>(json);
response.Response = json;
Expand Down
11 changes: 10 additions & 1 deletion Connection/RESTConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>).ContainsKey("errors"))
{
return deserializedObject["errors"][0]["message"];
Expand Down