48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
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;
|
|
private ConcurrentDictionary<int, List<Token>> Tokens { get; }
|
|
|
|
public SecurityStore(ITokenService tokenService)
|
|
{
|
|
_tokenService = tokenService;
|
|
Tokens = new ConcurrentDictionary<int, List<Token>>();
|
|
}
|
|
|
|
public void SetToken(Token token, int userId)
|
|
{
|
|
var registered = Tokens.TryGetValue(userId, out List<Token> list);
|
|
|
|
if (registered)
|
|
list.Add(token);
|
|
else
|
|
Tokens.TryAdd(userId, new List<Token>() { token });
|
|
}
|
|
|
|
public TokenCore ValidateAndGetTokenCore(string token)
|
|
{
|
|
var tokenCore = _tokenService.ExtractTokenCore(token);
|
|
if (tokenCore == null)
|
|
return null;
|
|
|
|
var registered = Tokens.TryGetValue(tokenCore.UserId, out List<Token> list);
|
|
if (!registered)
|
|
return null;
|
|
|
|
var valid = list.FirstOrDefault(z => z.Raw == token);
|
|
if (valid == null)
|
|
return null;
|
|
|
|
return tokenCore;
|
|
}
|
|
}
|
|
}
|