-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenCleanupHost.cs
More file actions
129 lines (109 loc) · 4.35 KB
/
TokenCleanupHost.cs
File metadata and controls
129 lines (109 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using IdentityServer7.EntityFramework.Options;
using IdentityServer7.EntityFramework;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
using System;
using Microsoft.Extensions.Logging;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Helper to cleanup expired persisted grants.
/// </summary>
public class TokenCleanupHost : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private readonly OperationalStoreOptions _options;
private readonly ILogger<TokenCleanupHost> _logger;
private TimeSpan CleanupInterval => TimeSpan.FromSeconds(_options.TokenCleanupInterval);
private CancellationTokenSource? _source;
/// <summary>
/// Constructor for TokenCleanupHost.
/// </summary>
/// <param name="serviceProvider"></param>
/// <param name="options"></param>
/// <param name="logger"></param>
public TokenCleanupHost(IServiceProvider? serviceProvider, OperationalStoreOptions? options, ILogger<TokenCleanupHost> logger)
{
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = logger;
}
/// <summary>
/// Starts the token cleanup polling.
/// </summary>
public Task StartAsync(CancellationToken cancellationToken)
{
if (_options.EnableTokenCleanup)
{
if (_source != null) throw new InvalidOperationException("Already started. Call Stop first.");
_logger.LogDebug("Starting grant removal");
_source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Task.Factory.StartNew(() => StartInternalAsync(_source.Token));
}
return Task.CompletedTask;
}
/// <summary>
/// Stops the token cleanup polling.
/// </summary>
public Task StopAsync(CancellationToken cancellationToken)
{
if (_options.EnableTokenCleanup)
{
if (_source == null) throw new InvalidOperationException("Not started. Call Start first.");
_logger.LogDebug("Stopping grant removal");
_source.Cancel();
_source = null;
}
return Task.CompletedTask;
}
private async Task StartInternalAsync(CancellationToken cancellationToken)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("CancellationRequested. Exiting.");
break;
}
try
{
await Task.Delay(CleanupInterval, cancellationToken);
}
catch (TaskCanceledException)
{
_logger.LogDebug("TaskCanceledException. Exiting.");
break;
}
catch (Exception ex)
{
_logger.LogError("Task.Delay exception: {0}. Exiting.", ex.Message);
break;
}
if (cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("CancellationRequested. Exiting.");
break;
}
await RemoveExpiredGrantsAsync();
}
}
async Task RemoveExpiredGrantsAsync()
{
try
{
using (var serviceScope = _serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var tokenCleanupService = serviceScope.ServiceProvider.GetRequiredService<TokenCleanupService>();
await tokenCleanupService.RemoveExpiredGrantsAsync();
}
}
catch (Exception ex)
{
_logger.LogError("Exception removing expired grants: {exception}", ex.Message);
}
}
}
}