using IdentityServer.Application.Services; using IdentityServer.Domain.Models; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; namespace IdentityServer.Application.Stores { internal class SecurityStore : ISecurityStore { private readonly ITokenService _tokenService; public SecurityStore(ITokenService tokenService) { _tokenService = tokenService; } private ConcurrentDictionary> Tokens { get; } public SecurityStore() { Tokens = new ConcurrentDictionary>(); } public void SetToken(Token token, int userId) { var registered = Tokens.TryGetValue(userId, out List list); if (registered) list.Add(token); else Tokens.TryAdd(userId, new List() { token }); } public TokenCore ValidateAndGetTokenCore(string token) { var tokenCore = _tokenService.ExtractTokenCore(token); if (tokenCore == null) return null; var registered = Tokens.TryGetValue(tokenCore.UserId, out List list); if (!registered) return null; var valid = list.FirstOrDefault(z => z.Raw == token); if (valid == null) return null; return tokenCore; } } }