Skip to content

Commit

Permalink
fix token authentication with null value
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Sep 19, 2024
1 parent 9815793 commit a6c3db4
Show file tree
Hide file tree
Showing 5 changed files with 716 additions and 10 deletions.
4 changes: 2 additions & 2 deletions APSToolkit/APSToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<EnableWindowsTargeting>true</EnableWindowsTargeting>
<PackageType>Dependency</PackageType>
<PackageId>APSToolkit</PackageId>
<AssemblyVersion>1.1.5</AssemblyVersion>
<PackageVersion>1.1.5</PackageVersion>
<AssemblyVersion>1.1.6</AssemblyVersion>
<PackageVersion>1.1.6</PackageVersion>
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
<Deterministic>false</Deterministic>
<PackageTags>revit;bim360;acc;adsk;forgetoolkit;forge;autodesk;aps;</PackageTags>
Expand Down
17 changes: 13 additions & 4 deletions APSToolkit/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public async Task<Token> Get2LeggedToken()
}).ConfigureAwait(false);
this.Token.AccessToken = token.access_token;
this.Token.TokenType = token.token_type;
this.Token.ExpiresIn = token.expires_in;
long expiresIn = token.expires_in;
// convert expiresIn to linux time
this.Token.ExpiresIn = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds() + expiresIn;
if (string.IsNullOrEmpty(this.Token.AccessToken))
{
throw new Exception("can't get access_token, please check again value APS_CLIENT_ID and APS_CLIENT_SECRET");
Expand Down Expand Up @@ -205,7 +207,14 @@ public async Task<Token> Get3LeggedToken(string? callbackUrl = null, string? sco
}

var jsonResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Token>(jsonResponse);
JObject? token = JsonConvert.DeserializeObject<JObject>(jsonResponse);
var accessToken = token!["access_token"]!.Value<string>();
var tokenType = token["token_type"]!.Value<string>();
var expiresIn = token["expires_in"]!.Value<int>();
// convert expiresIn to linux time
expiresIn = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds() + expiresIn;
var refreshToken = token["refresh_token"]!.Value<string>();
return new Token(accessToken, tokenType, expiresIn, refreshToken);
}

private void OpenDefaultBrowser(string url)
Expand Down Expand Up @@ -361,7 +370,7 @@ public async Task<Token> Refresh3LeggedToken(string refreshToken,
Environment.SetEnvironmentVariable("APS_REFRESH_TOKEN", newRefreshToken, EnvironmentVariableTarget.User);
this.Token.AccessToken = accessToken;
this.Token.TokenType = token.token_type;
this.Token.ExpiresIn = token.expires_in;
this.Token.ExpiresIn = token.expires_in + (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
this.Token.RefreshToken = newRefreshToken;
return Token;
}
Expand Down Expand Up @@ -393,7 +402,7 @@ public async Task<Token> Refresh3LeggedToken(string clientId, string clientSecre
{
AccessToken = accessToken,
TokenType = token.token_type,
ExpiresIn = token.expires_in,
ExpiresIn = token.expires_in + (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
RefreshToken = newRefreshToken
};
return newToken;
Expand Down
22 changes: 18 additions & 4 deletions APSToolkit/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ public Token()
{

}

// map with access_token
[System.Text.Json.Serialization.JsonPropertyName("access_token")]
public string? AccessToken { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("token_type")]
public string? TokenType { get; set; }
[System.Text.Json.Serialization.JsonPropertyName("expires_in")]
public long? ExpiresIn { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("refresh_token")]
public string? RefreshToken { get; set; }

/// <summary>
Expand Down Expand Up @@ -58,9 +63,18 @@ public Token Refresh3LegToken()
this.RefreshToken = token.RefreshToken;
return this;
}
public bool IsExpired()
/// <summary>
/// Checks if the token has expired.
/// </summary>
/// <param name="bufferMinutes"></param>
/// <returns></returns>
public bool IsExpired(double bufferMinutes=0)
{
bool b = this.ExpiresIn <= 60;
return b;
var currentUnixTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (currentUnixTime + bufferMinutes*60 >= this.ExpiresIn)
{
return true;
}
return false;
}
}
9 changes: 9 additions & 0 deletions APSToolkitUnit/AuthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public void TestAuthentication2Leg()
Assert.IsNotEmpty(Token.AccessToken);
}
[Test]
public void TestExpireTime()
{
Token = Auth.Get2LeggedToken().Result;
bool isExpired = Token.IsExpired();
Assert.IsFalse(isExpired);
isExpired = Token.IsExpired(70);
Assert.IsTrue(isExpired);
}
[Test]
public void TestAuthentication3Leg()
{
Token = Auth.Get3LeggedToken().Result;
Expand Down
Loading

0 comments on commit a6c3db4

Please sign in to comment.