using IdentityServer.Domain.Models; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; namespace IdentityServer.Application.Stores { public class SecurityStore : ISecurityStore { private ConcurrentDictionary> Tokens { get; } public SecurityStore() { Tokens = new ConcurrentDictionary>(); } public void SetToken(string token, int userId) { var registered = Tokens.TryGetValue(userId, out List list); if (registered) list.Add(new Token() { Raw = token }); else Tokens.TryAdd(userId, new List() { new Token() { Raw = token } }); } public TokenValidation ValidateToken(string token) { var lastIndexOfSeparator = token.LastIndexOf('-') + 1; var userIdString = token.Substring(lastIndexOfSeparator, token.Length - lastIndexOfSeparator); if (!int.TryParse(userIdString, out int userId)) return InvalidToken; var registered = Tokens.TryGetValue(userId, out List list); if (!registered) return InvalidToken; var valid = list.FirstOrDefault(z => z.Raw == token); if (valid != null) return new TokenValidation() { Success = true, UserId = userId }; return InvalidToken; } private TokenValidation InvalidToken => new TokenValidation() { Success = false }; } }