tuitio/IdentityServer.Application/Stores/TokenStore.cs

48 lines
1.4 KiB
C#
Raw Normal View History

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-13 17:17:13 +02:00
internal class TokenStore : ITokenStore
2020-12-20 03:06:43 +02:00
{
private readonly ITokenService _tokenService;
2021-11-12 02:22:51 +02:00
private ConcurrentDictionary<int, List<Token>> Tokens { get; }
2021-11-13 17:17:13 +02:00
public TokenStore(ITokenService tokenService)
{
_tokenService = tokenService;
2020-12-20 03:06:43 +02:00
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
}
public TokenCore ValidateAndGetTokenCore(string token)
2020-12-20 03:06:43 +02:00
{
var tokenCore = _tokenService.ExtractTokenCore(token);
if (tokenCore == null)
return null;
2020-12-20 03:06:43 +02:00
var registered = Tokens.TryGetValue(tokenCore.UserId, out List<Token> list);
2020-12-20 03:06:43 +02:00
if (!registered)
return null;
2020-12-20 03:06:43 +02:00
var valid = list.FirstOrDefault(z => z.Raw == token);
if (valid == null)
return null;
2020-12-20 03:06:43 +02:00
return tokenCore;
2020-12-20 03:06:43 +02:00
}
}
}