Skip to content
Merged
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
13 changes: 9 additions & 4 deletions Acumatica.RESTClient/AuthApi/AuthApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public async static Task RefreshAccessTokenAsync(this ApiClient client, string c
{
if (client == null || string.IsNullOrEmpty(client.Token?.Refresh_token))
ThrowMissingParameter(nameof(RefreshAccessToken), "Refresh_Token");

var time = DateTime.UtcNow;
HttpResponseMessage response = await client!.CallApiAsync(
resourcePath: "/identity/connect/token",
method: HttpMethod.Post,
Expand All @@ -51,6 +51,7 @@ public async static Task RefreshAccessTokenAsync(this ApiClient client, string c
response.EnsureSuccessStatusCode();

client.Token = await DeserializeAsync<Token>(response).ConfigureAwait(false);
client.Token?.SetTokenObtainedDT(time);
}
/// <summary>
/// Receives access token for OAuth 2.0 authentication (Resource owner password credentials flow)
Expand Down Expand Up @@ -84,6 +85,7 @@ public async static Task ReceiveAccessTokenAsync(
OAuthScope scope,
CancellationToken cancellationToken = default)
{
var time = DateTime.UtcNow;
HttpResponseMessage response = await client.CallApiAsync(
resourcePath: "identity/connect/token",
method: HttpMethod.Post,
Expand All @@ -103,7 +105,8 @@ public async static Task ReceiveAccessTokenAsync(

await VerifyResponseAsync(client, response, nameof(ReceiveAccessTokenAsync)).ConfigureAwait(false);

client.Token = await DeserializeAsync<Token>(response).ConfigureAwait(false);
client.Token = await DeserializeAsync<Token>(response).ConfigureAwait(false);
client.Token?.SetTokenObtainedDT(time);
}

/// <summary>
Expand Down Expand Up @@ -193,6 +196,7 @@ public static async Task ReceiveAccessTokenAuthCodeAsync(
string code,
CancellationToken cancellationToken = default)
{
var time = DateTime.UtcNow;
HttpResponseMessage response = await client.CallApiAsync(
resourcePath: "/identity/connect/token",
method: HttpMethod.Post,
Expand All @@ -212,7 +216,8 @@ public static async Task ReceiveAccessTokenAuthCodeAsync(

await VerifyResponseAsync(client, response, "RequestToken").ConfigureAwait(false);

client.Token = await DeserializeAsync<Token>(response).ConfigureAwait(false);
client.Token = await DeserializeAsync<Token>(response).ConfigureAwait(false);
client.Token?.SetTokenObtainedDT(time);
}
#endregion

Expand Down Expand Up @@ -398,4 +403,4 @@ private static string PrepareScopeParameter(OAuthScope scope)

#endregion
}
}
}
18 changes: 16 additions & 2 deletions Acumatica.RESTClient/AuthApi/Model/Token.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Runtime.Serialization;

using Newtonsoft.Json;
using System;
using System.Runtime.Serialization;

namespace Acumatica.RESTClient.AuthApi.Model
{
Expand All @@ -22,6 +22,7 @@ public Token(
Refresh_token = refreshToken;
Scope = scope;
Token_type = token_type;
SetTokenObtainedDT();
}

[DataMember(Name = "access_token", EmitDefaultValue = false)]
Expand All @@ -39,6 +40,9 @@ public Token(
[DataMember(Name = "token_type", EmitDefaultValue = false)]
public string? Token_type { get; set; }

public DateTime ObtainedAtUTC{ get; private set; }

public bool IsValid { get => isValid(); }
/// <summary>
/// Returns the JSON string presentation of the object
/// </summary>
Expand All @@ -47,6 +51,16 @@ public virtual string ToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

public virtual void SetTokenObtainedDT(DateTime? dt = null)
{
ObtainedAtUTC = dt ?? DateTime.UtcNow;
}

private bool isValid()
{
return !string.IsNullOrEmpty(Access_token) && (DateTime.UtcNow - ObtainedAtUTC).TotalSeconds < Convert.ToUInt32(Expires_in);
}
}

}
2 changes: 1 addition & 1 deletion Acumatica.RESTClient/Client/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public async Task<HttpResponseMessage> CallApiAsync(

public bool HasToken()
{
return Token != null;
return Token?.IsValid == true;
}
public void Dispose()
{
Expand Down