aboutsummaryrefslogtreecommitdiff
path: root/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation
diff options
context:
space:
mode:
authorLibravatar vman <public@vaughnnugent.com>2022-11-18 16:08:51 -0500
committerLibravatar vman <public@vaughnnugent.com>2022-11-18 16:08:51 -0500
commit526c2364b9ad685d1c000fc8a168bf1305aaa8b7 (patch)
treea2bc01607320a6a75e1a869d5bd34e79fd63c595 /VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation
parent2080400119be00bdc354f3121d84ec2f89606ac7 (diff)
Add project files.
Diffstat (limited to 'VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation')
-rw-r--r--VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevocationContext.cs14
-rw-r--r--VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedToken.cs19
-rw-r--r--VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedTokenStore.cs77
3 files changed, 110 insertions, 0 deletions
diff --git a/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevocationContext.cs b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevocationContext.cs
new file mode 100644
index 0000000..71921c2
--- /dev/null
+++ b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevocationContext.cs
@@ -0,0 +1,14 @@
+using Microsoft.EntityFrameworkCore;
+
+using VNLib.Plugins.Extensions.Data;
+
+namespace VNLib.Plugins.Essentials.Accounts.Registration.TokenRevocation
+{
+ internal class RevocationContext : TransactionalDbContext
+ {
+ public DbSet<RevokedToken> RevokedRegistrationTokens { get; set; }
+
+ public RevocationContext(DbContextOptions options) : base(options)
+ {}
+ }
+} \ No newline at end of file
diff --git a/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedToken.cs b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedToken.cs
new file mode 100644
index 0000000..ac0fc9a
--- /dev/null
+++ b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedToken.cs
@@ -0,0 +1,19 @@
+using System;
+using System.ComponentModel.DataAnnotations;
+
+namespace VNLib.Plugins.Essentials.Accounts.Registration.TokenRevocation
+{
+
+ internal class RevokedToken
+ {
+ /// <summary>
+ /// The time the token was revoked.
+ /// </summary>
+ public DateTime Created { get; set; }
+ /// <summary>
+ /// The token that was revoked.
+ /// </summary>
+ [Key]
+ public string? Token { get; set; }
+ }
+} \ No newline at end of file
diff --git a/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedTokenStore.cs b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedTokenStore.cs
new file mode 100644
index 0000000..ccc7b37
--- /dev/null
+++ b/VNLib.Plugins.Essentials.Accounts.Registration/src/TokenRevocation/RevokedTokenStore.cs
@@ -0,0 +1,77 @@
+using System.Collections;
+
+using Microsoft.EntityFrameworkCore;
+
+using VNLib.Utils;
+
+namespace VNLib.Plugins.Essentials.Accounts.Registration.TokenRevocation
+{
+ internal class RevokedTokenStore
+ {
+ private readonly DbContextOptions Options;
+
+ public RevokedTokenStore(DbContextOptions options)
+ {
+ Options = options;
+ }
+
+ public async Task<bool> IsRevokedAsync(string token, CancellationToken cancellation)
+ {
+ await using RevocationContext context = new (Options);
+ await context.OpenTransactionAsync(cancellation);
+
+ //Select any that match tokens
+ bool any = await (from t in context.RevokedRegistrationTokens
+ where t.Token == token
+ select t).AnyAsync(cancellation);
+
+ await context.CommitTransactionAsync(cancellation);
+ return any;
+ }
+
+ public async Task RevokeAsync(string token, CancellationToken cancellation)
+ {
+ await using RevocationContext context = new (Options);
+ await context.OpenTransactionAsync(cancellation);
+
+ //Add to table
+ context.RevokedRegistrationTokens.Add(new RevokedToken()
+ {
+ Created = DateTime.UtcNow,
+ Token = token
+ });
+
+ //Save changes and commit transaction
+ await context.SaveChangesAsync(cancellation);
+ await context.CommitTransactionAsync(cancellation);
+ }
+
+ /// <summary>
+ /// Removes expired records from the store
+ /// </summary>
+ /// <param name="validFor">The time a token is valid for</param>
+ /// <param name="cancellation">A token that cancels the async operation</param>
+ /// <returns>The number of records evicted from the store</returns>
+ public async Task<ERRNO> CleanTableAsync(TimeSpan validFor, CancellationToken cancellation)
+ {
+ DateTime expiredBefore = DateTime.UtcNow.Subtract(validFor);
+
+ await using RevocationContext context = new (Options);
+ await context.OpenTransactionAsync(cancellation);
+
+ //Select any that match tokens
+ RevokedToken[] expired = await context.RevokedRegistrationTokens.Where(t => t.Created < expiredBefore)
+ .Select(static t => t)
+ .ToArrayAsync(cancellation);
+
+
+ context.RevokedRegistrationTokens.RemoveRange(expired);
+
+ ERRNO count =await context.SaveChangesAsync(cancellation);
+
+ await context.CommitTransactionAsync(cancellation);
+
+ return count;
+ }
+ }
+} \ No newline at end of file