using System; using System.Collections.Concurrent; using System.Linq; namespace WingNotificationModule.Channel.WebSocket { public class ApplyWSToken : IApplyToken { private ConcurrentDictionary _tokenDic = new(); /// /// Generate token /// /// /// /// /// token public string GenerateToken(string clientIpAndPort, string clientDeviceId, string clientSource) { string token = GetTokenByDeviceId(clientDeviceId); if (string.IsNullOrWhiteSpace(token)) { token = Guid.NewGuid().ToString("N"); var tokenInfo = new WSTokenInfo { Token = token, ClientSource = clientSource, ClientDeviceId = clientDeviceId, ClientIpAndPort = clientIpAndPort }; _tokenDic.AddOrUpdate(token, tokenInfo, (k, v) => tokenInfo); } return token; } /// /// Get token by device id /// /// /// public string GetTokenByDeviceId(string clientDeviceId) { return _tokenDic.Values.FirstOrDefault(ws => ws.ClientDeviceId == clientDeviceId)?.Token; } /// /// Validate token /// /// /// public bool ValidateToken(string token) { return _tokenDic.TryGetValue(token, out _); } } }