-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOAuthTokens.cs
More file actions
53 lines (40 loc) · 1.56 KB
/
OAuthTokens.cs
File metadata and controls
53 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
namespace Contentstack.Management.Core.Models
{
/// <summary>
/// Represents OAuth tokens stored in memory for cross-SDK access.
/// This class enables sharing OAuth tokens between the Management SDK and other SDKs
/// </summary>
public class OAuthTokens
{
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public DateTime ExpiresAt { get; set; }
public string OrganizationUid { get; set; }
public string UserUid { get; set; }
public string ClientId { get; set; }
public string AppId { get; set; }
public bool IsExpired => ExpiresAt == DateTime.MinValue || DateTime.UtcNow >= ExpiresAt;
public bool NeedsRefresh
{
get
{
// If ExpiresAt is not set or is MinValue, consider it expired
if (ExpiresAt == DateTime.MinValue)
return true;
try
{
// Check if we need to refresh (5 minutes before expiration)
var refreshTime = ExpiresAt.AddMinutes(-5);
return DateTime.UtcNow >= refreshTime || IsExpired;
}
catch (ArgumentOutOfRangeException)
{
// If the calculation results in an unrepresentable DateTime, consider it expired
return true;
}
}
}
public bool IsValid => !string.IsNullOrEmpty(AccessToken) && !IsExpired;
}
}