2021-11-13 17:17:13 +02:00
|
|
|
|
using IdentityServer.Application.Services.Abstractions;
|
|
|
|
|
using IdentityServer.Application.Stores;
|
2021-11-13 20:58:17 +02:00
|
|
|
|
using IdentityServer.Domain.Entities;
|
2021-11-13 17:17:13 +02:00
|
|
|
|
using IdentityServer.Domain.Models;
|
|
|
|
|
using IdentityServer.Domain.Repositories;
|
2021-11-13 20:58:17 +02:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
2021-11-13 17:17:13 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IdentityServer.Application.Services
|
|
|
|
|
{
|
|
|
|
|
internal class BehaviorService : IBehaviorService
|
|
|
|
|
{
|
2021-11-13 20:58:17 +02:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ILogger<BehaviorService> _logger;
|
2021-11-13 17:17:13 +02:00
|
|
|
|
private readonly ITokenStore _securityStore;
|
|
|
|
|
|
2021-11-13 20:58:17 +02:00
|
|
|
|
public BehaviorService(IServiceProvider serviceProvider, ILogger<BehaviorService> logger, ITokenStore securityStore)
|
2021-11-13 17:17:13 +02:00
|
|
|
|
{
|
2021-11-13 20:58:17 +02:00
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
_logger = logger;
|
2021-11-13 17:17:13 +02:00
|
|
|
|
_securityStore = securityStore;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FillTokenStore()
|
|
|
|
|
=> FillTokenStoreAsync().GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
|
|
public async Task FillTokenStoreAsync()
|
|
|
|
|
{
|
2021-11-13 20:58:17 +02:00
|
|
|
|
var activeTokens = Array.Empty<UserToken>();
|
|
|
|
|
using (var scope = _serviceProvider.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var _repository = scope.ServiceProvider.GetRequiredService<IIdentityRepository>();
|
|
|
|
|
activeTokens = await _repository.GetActiveTokens();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 17:17:13 +02:00
|
|
|
|
if (activeTokens.Length <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-11-13 20:58:17 +02:00
|
|
|
|
_logger.LogInformation($"BehaviorService: {activeTokens.Length} active tokens were found in database.");
|
2021-11-13 17:17:13 +02:00
|
|
|
|
foreach (var token in activeTokens)
|
|
|
|
|
{
|
|
|
|
|
var storeToken = new Token() { Raw = token.Token, ValidFrom = token.ValidFrom, ValidUntil = token.ValidUntil };
|
|
|
|
|
_securityStore.SetToken(storeToken, token.UserId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|