forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessTokenManager.cs
More file actions
82 lines (67 loc) · 2.41 KB
/
AccessTokenManager.cs
File metadata and controls
82 lines (67 loc) · 2.41 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
using System.Collections.Generic;
using SiteServer.CMS.Core;
using SiteServer.CMS.DataCache.Core;
using SiteServer.CMS.Model;
using SiteServer.Utils;
namespace SiteServer.CMS.DataCache
{
public static class AccessTokenManager
{
private static class AccessTokenManagerCache
{
private static readonly object LockObject = new object();
private static readonly string CacheKey = DataCacheManager.GetCacheKey(nameof(AccessTokenManager));
public static void Clear()
{
DataCacheManager.Remove(CacheKey);
}
public static Dictionary<string, AccessTokenInfo> GetAccessTokenDictionary()
{
var retval = DataCacheManager.Get<Dictionary<string, AccessTokenInfo>>(CacheKey);
if (retval != null) return retval;
lock (LockObject)
{
retval = DataCacheManager.Get<Dictionary<string, AccessTokenInfo>>(CacheKey);
if (retval == null)
{
retval = DataProvider.AccessTokenDao.GetAccessTokenInfoDictionary();
DataCacheManager.Insert(CacheKey, retval);
}
}
return retval;
}
}
public const string ScopeContents = "Contents";
public const string ScopeAdministrators = "Administrators";
public const string ScopeUsers = "Users";
public const string ScopeStl = "STL";
public static List<string> ScopeList => new List<string>
{
ScopeContents,
ScopeAdministrators,
ScopeUsers,
ScopeStl
};
public static void ClearCache()
{
AccessTokenManagerCache.Clear();
}
public static bool IsScope(string token, string scope)
{
if (string.IsNullOrEmpty(token)) return false;
var tokenInfo = GetAccessTokenInfo(token);
if (tokenInfo == null) return false;
return StringUtils.ContainsIgnoreCase(TranslateUtils.StringCollectionToStringList(tokenInfo.Scopes), scope);
}
public static AccessTokenInfo GetAccessTokenInfo(string token)
{
AccessTokenInfo tokenInfo = null;
var dict = AccessTokenManagerCache.GetAccessTokenDictionary();
if (dict != null && dict.ContainsKey(token))
{
tokenInfo = dict[token];
}
return tokenInfo;
}
}
}