-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathAuthStateManager.cs
More file actions
129 lines (114 loc) · 4.25 KB
/
AuthStateManager.cs
File metadata and controls
129 lines (114 loc) · 4.25 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using UnityEditor;
/// <summary>
/// Manages token lifecycle including storage, validation, and expiry checking
/// </summary>
public class AuthStateManager
{
private BentleyAuthManager parentAuth;
public AuthStateManager(BentleyAuthManager parentAuth)
{
this.parentAuth = parentAuth;
}
/// <summary>
/// Gets the current valid access token, returning null if expired/invalid.
/// Does NOT automatically trigger refresh or login.
/// </summary>
public string GetCurrentAccessToken()
{
if (!string.IsNullOrEmpty(parentAuth.accessToken) &&
parentAuth.expiryTimeUtc > DateTime.UtcNow.AddMinutes(AuthConstants.TOKEN_EXPIRY_BUFFER_MINUTES))
return parentAuth.accessToken;
return null;
}
/// <summary>
/// Determines if user is logged in based on valid tokens
/// </summary>
public bool IsLoggedIn()
{
return GetCurrentAccessToken() != null || !string.IsNullOrEmpty(parentAuth.refreshToken);
}
/// <summary>
/// Gets the token expiry time in UTC
/// </summary>
public DateTime GetExpiryTimeUtc()
{
return parentAuth.expiryTimeUtc;
}
/// <summary>
/// Processes token response and updates internal state
/// </summary>
public void ProcessTokenResponse(string json)
{
try
{
var tokenResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResponse>(json);
parentAuth.accessToken = tokenResponse.access_token;
parentAuth.refreshToken = tokenResponse.refresh_token;
parentAuth.expiryTimeUtc = DateTime.UtcNow.AddSeconds(tokenResponse.expires_in);
SaveTokensToPrefs();
}
catch (Exception ex)
{
UnityEngine.Debug.LogError($"BentleyAuthManager_Editor: Failed to process token response: {ex.Message}");
throw;
}
}
/// <summary>
/// Saves tokens to EditorPrefs for persistence
/// </summary>
public void SaveTokensToPrefs()
{
if (!string.IsNullOrEmpty(parentAuth.accessToken))
EditorPrefs.SetString(AuthConstants.PREF_ACCESS_TOKEN, parentAuth.accessToken);
if (!string.IsNullOrEmpty(parentAuth.refreshToken))
EditorPrefs.SetString(AuthConstants.PREF_REFRESH_TOKEN, parentAuth.refreshToken);
EditorPrefs.SetString(AuthConstants.PREF_EXPIRY, parentAuth.expiryTimeUtc.ToString("O"));
}
/// <summary>
/// Loads stored tokens from EditorPrefs
/// </summary>
public void LoadStoredTokens()
{
if (EditorPrefs.HasKey(AuthConstants.PREF_ACCESS_TOKEN) && EditorPrefs.HasKey(AuthConstants.PREF_EXPIRY))
{
parentAuth.accessToken = EditorPrefs.GetString(AuthConstants.PREF_ACCESS_TOKEN);
parentAuth.refreshToken = EditorPrefs.GetString(AuthConstants.PREF_REFRESH_TOKEN, "");
string storedExpiry = EditorPrefs.GetString(AuthConstants.PREF_EXPIRY);
if (DateTime.TryParse(storedExpiry, null, System.Globalization.DateTimeStyles.RoundtripKind, out var dt))
{
parentAuth.expiryTimeUtc = dt;
// Clear expired tokens immediately
if (parentAuth.expiryTimeUtc <= DateTime.UtcNow)
{
ClearTokens();
}
}
else
{
UnityEngine.Debug.LogWarning("BentleyAuthManager_Editor: Could not parse stored expiry time, clearing tokens.");
ClearTokens();
}
}
}
/// <summary>
/// Clears all stored tokens and state
/// </summary>
public void ClearTokens()
{
parentAuth.accessToken = null;
parentAuth.refreshToken = null;
parentAuth.expiryTimeUtc = DateTime.MinValue;
// Clear from EditorPrefs
EditorPrefs.DeleteKey(AuthConstants.PREF_ACCESS_TOKEN);
EditorPrefs.DeleteKey(AuthConstants.PREF_REFRESH_TOKEN);
EditorPrefs.DeleteKey(AuthConstants.PREF_EXPIRY);
}
[Serializable]
private class TokenResponse
{
public string access_token;
public string refresh_token;
public int expires_in;
}
}