tgstation-server  4.3.2
The /tg/station 13 server suite
IdentityCacheObject.cs
Go to the documentation of this file.
1 using System;
2 using System.Threading;
3 using System.Threading.Tasks;
5 
6 namespace Tgstation.Server.Host.Security
7 {
12  {
16  public ISystemIdentity SystemIdentity { get; }
17 
21  readonly CancellationTokenSource cancellationTokenSource;
22 
26  readonly Task task;
27 
35  public IdentityCacheObject(ISystemIdentity systemIdentity, IAsyncDelayer asyncDelayer, Action onExpiry, DateTimeOffset expiry)
36  {
37  SystemIdentity = systemIdentity ?? throw new ArgumentNullException(nameof(systemIdentity));
38 
39  if (asyncDelayer == null)
40  throw new ArgumentNullException(nameof(asyncDelayer));
41 
42  if (onExpiry == null)
43  throw new ArgumentNullException(nameof(onExpiry));
44  var now = DateTimeOffset.Now;
45  if (expiry < now)
46  throw new ArgumentOutOfRangeException(nameof(expiry), expiry, "expiry must be greater than DateTimeOffset.Now!");
47 
48  cancellationTokenSource = new CancellationTokenSource();
49 
50  async Task DisposeOnExipiry(CancellationToken cancellationToken)
51  {
52  using (SystemIdentity)
53  try
54  {
55  await asyncDelayer.Delay(expiry - now, cancellationToken).ConfigureAwait(false);
56  }
57  finally
58  {
59  onExpiry();
60  }
61  }
62 
63  task = DisposeOnExipiry(cancellationTokenSource.Token);
64  }
65 
67  public void Dispose()
68  {
69  cancellationTokenSource.Cancel();
70  try
71  {
72  task.GetAwaiter().GetResult();
73  }
74  catch (OperationCanceledException) { }
75  cancellationTokenSource.Dispose();
76  }
77  }
78 }
readonly Task task
The Task to clean up SystemIdentity
IdentityCacheObject(ISystemIdentity systemIdentity, IAsyncDelayer asyncDelayer, Action onExpiry, DateTimeOffset expiry)
Construct an IdentityCache
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform ...
Task Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan
readonly CancellationTokenSource cancellationTokenSource
The cancellationTokenSource for the IdentityCache
For keeping a specific ISystemIdentity alive for a period of time