2021-11-12 01:37:10 +02:00
|
|
|
|
using IdentityServer.Application.Services;
|
|
|
|
|
using IdentityServer.Domain.Models;
|
2020-12-20 03:06:43 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace IdentityServer.Application.Stores
|
|
|
|
|
{
|
2021-11-12 01:37:10 +02:00
|
|
|
|
internal class SecurityStore : ISecurityStore
|
2020-12-20 03:06:43 +02:00
|
|
|
|
{
|
2021-11-12 01:37:10 +02:00
|
|
|
|
private readonly ITokenService _tokenService;
|
|
|
|
|
|
|
|
|
|
public SecurityStore(ITokenService tokenService)
|
|
|
|
|
{
|
|
|
|
|
_tokenService = tokenService;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 03:06:43 +02:00
|
|
|
|
private ConcurrentDictionary<int, List<Token>> Tokens { get; }
|
|
|
|
|
|
|
|
|
|
public SecurityStore()
|
|
|
|
|
{
|
|
|
|
|
Tokens = new ConcurrentDictionary<int, List<Token>>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 04:55:45 +02:00
|
|
|
|
public void SetToken(Token token, int userId)
|
2020-12-20 03:06:43 +02:00
|
|
|
|
{
|
|
|
|
|
var registered = Tokens.TryGetValue(userId, out List<Token> list);
|
|
|
|
|
|
|
|
|
|
if (registered)
|
2020-12-24 04:55:45 +02:00
|
|
|
|
list.Add(token);
|
2020-12-20 03:06:43 +02:00
|
|
|
|
else
|
2020-12-24 04:55:45 +02:00
|
|
|
|
Tokens.TryAdd(userId, new List<Token>() { token });
|
2020-12-20 03:06:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 01:37:10 +02:00
|
|
|
|
public TokenCore ValidateAndGetTokenCore(string token)
|
2020-12-20 03:06:43 +02:00
|
|
|
|
{
|
2021-11-12 01:37:10 +02:00
|
|
|
|
var tokenCore = _tokenService.ExtractTokenCore(token);
|
|
|
|
|
if (tokenCore == null)
|
|
|
|
|
return null;
|
2020-12-20 03:06:43 +02:00
|
|
|
|
|
2021-11-12 01:37:10 +02:00
|
|
|
|
var registered = Tokens.TryGetValue(tokenCore.UserId, out List<Token> list);
|
2020-12-20 03:06:43 +02:00
|
|
|
|
if (!registered)
|
2021-11-12 01:37:10 +02:00
|
|
|
|
return null;
|
2020-12-20 03:06:43 +02:00
|
|
|
|
|
|
|
|
|
var valid = list.FirstOrDefault(z => z.Raw == token);
|
2021-11-12 01:37:10 +02:00
|
|
|
|
if (valid == null)
|
|
|
|
|
return null;
|
2020-12-20 03:06:43 +02:00
|
|
|
|
|
2021-11-12 01:37:10 +02:00
|
|
|
|
return tokenCore;
|
2020-12-20 03:06:43 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|