diff options
author | vnugent <public@vaughnnugent.com> | 2024-02-12 20:27:53 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-02-12 20:27:53 -0500 |
commit | 850a60e02bd083a797ffb5f48229fab502e8aede (patch) | |
tree | 6ba6f4041a7f5ec673d85a98346d5b6c98c96c90 /Libs | |
parent | c70a61bc1bf683c2097e3a2481d9dfb9adbd31ea (diff) |
refactor: integrate the latest sql library updates
Diffstat (limited to 'Libs')
3 files changed, 18 insertions, 23 deletions
diff --git a/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/ApplicationStore.cs b/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/ApplicationStore.cs index 67488f6..92ced51 100644 --- a/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/ApplicationStore.cs +++ b/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/ApplicationStore.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials.Oauth @@ -97,7 +97,7 @@ namespace VNLib.Plugins.Essentials.Oauth.Applications /// <param name="userId">The user-id of that owns the application</param> /// <param name="appId">The id of the application to update</param> /// <returns>A task that resolves to the raw secret that was used to generate the hash, or null if the operation failed</returns> - public async Task<PrivateString?> UpdateSecretAsync(string userId, string appId) + public async Task<PrivateString?> UpdateSecretAsync(string userId, string appId, CancellationToken cancellation) { /* * Delete open apps first, incase there are any issues, worse case @@ -107,33 +107,34 @@ namespace VNLib.Plugins.Essentials.Oauth.Applications * secret and may lose access to the updated app, not a big deal * but avoidable. */ - await TokenStore.RevokeTokensForAppAsync(appId, CancellationToken.None); + await TokenStore.RevokeTokensForAppAsync(appId, cancellation); + //Generate the new secret PrivateString secret = GenerateSecret(); //Hash the secret using PrivateString secretHash = SecretHashing.Hash(secret); //Open new db context await using UserAppContext Database = new(ConextOptions); - //Open transaction - await Database.OpenTransactionAsync(); + //Get the app to update the secret on UserApplication? app = await (from ap in Database.OAuthApps where ap.UserId == userId && ap.Id == appId select ap) - .SingleOrDefaultAsync(); - if (app == null) + .SingleOrDefaultAsync(cancellation); + if (app is null) { return null; } + //Store the new secret hash app.SecretHash = (string)secretHash; + //Save changes - if (await Database.SaveChangesAsync() <= 0) + if (await Database.SaveAndCloseAsync(true, cancellation) <= 0) { return null; } - //Commit transaction - await Database.CommitTransactionAsync(); + //return the raw secret return secret; } @@ -145,22 +146,21 @@ namespace VNLib.Plugins.Essentials.Oauth.Applications /// <param name="clientId">The clientid of the application to search</param> /// <param name="secret">The secret to compare against</param> /// <returns>True if the application was found and the secret matches the stored secret, false if the appliation was not found or the secret does not match</returns> - public async Task<UserApplication?> VerifyAppAsync(string clientId, PrivateString secret) + public async Task<UserApplication?> VerifyAppAsync(string clientId, PrivateString secret, CancellationToken cancellation) { UserApplication? app; //Open new db context await using (UserAppContext Database = new(ConextOptions)) { - //Open transaction - await Database.OpenTransactionAsync(); //Get the application with its secret app = await (from userApp in Database.OAuthApps where userApp.ClientId == clientId select userApp) - .FirstOrDefaultAsync(); + .FirstOrDefaultAsync(cancellation); + //commit the transaction - await Database.CommitTransactionAsync(); + await Database.SaveAndCloseAsync(true, cancellation); } //make sure app exists diff --git a/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/UserAppContext.cs b/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/UserAppContext.cs index e4d98e6..cc38800 100644 --- a/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/UserAppContext.cs +++ b/Libs/VNLib.Plugins.Essentials.Oauth/src/Applications/UserAppContext.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Plugins.Essentials.Oauth @@ -29,7 +29,7 @@ using VNLib.Plugins.Essentials.Oauth.Tokens; namespace VNLib.Plugins.Essentials.Oauth.Applications { - public class UserAppContext : TransactionalDbContext + public class UserAppContext : DBContextBase { public DbSet<UserApplication> OAuthApps { get; set; } diff --git a/Libs/VNLib.Plugins.Essentials.Oauth/src/Tokens/TokenStore.cs b/Libs/VNLib.Plugins.Essentials.Oauth/src/Tokens/TokenStore.cs index 2150350..56283c6 100644 --- a/Libs/VNLib.Plugins.Essentials.Oauth/src/Tokens/TokenStore.cs +++ b/Libs/VNLib.Plugins.Essentials.Oauth/src/Tokens/TokenStore.cs @@ -59,7 +59,6 @@ namespace VNLib.Plugins.Essentials.Oauth.Tokens public async Task<ERRNO> InsertTokenAsync(string token, string appId, string? refreshToken, int maxTokens, CancellationToken cancellation) { await using UserAppContext ctx = new (Options); - await ctx.OpenTransactionAsync(cancellation); //Check active token count int count = await (from t in ctx.OAuthTokens @@ -99,13 +98,12 @@ namespace VNLib.Plugins.Essentials.Oauth.Tokens public async Task RevokeTokenAsync(string token, CancellationToken cancellation) { await using UserAppContext ctx = new (Options); - await ctx.OpenTransactionAsync(cancellation); //Get the token from the db if it exists ActiveToken? at = await (from t in ctx.OAuthTokens where t.Id == token select t) .FirstOrDefaultAsync(cancellation); - if(at == null) + if(at is null) { return; } @@ -124,7 +122,6 @@ namespace VNLib.Plugins.Essentials.Oauth.Tokens public async Task<IReadOnlyCollection<ActiveToken>> CleanupExpiredTokensAsync(DateTime validAfter, CancellationToken cancellation) { await using UserAppContext ctx = new (Options); - await ctx.OpenTransactionAsync(cancellation); //Get the token from the db if it exists ActiveToken[] at = await (from t in ctx.OAuthTokens where t.Created < validAfter @@ -142,7 +139,6 @@ namespace VNLib.Plugins.Essentials.Oauth.Tokens public async Task RevokeTokensAsync(IReadOnlyCollection<string> tokens, CancellationToken cancellation = default) { await using UserAppContext ctx = new (Options); - await ctx.OpenTransactionAsync(cancellation); //Get all tokenes that are contained in the collection ActiveToken[] at = await (from t in ctx.OAuthTokens where tokens.Contains(t.Id) @@ -159,7 +155,6 @@ namespace VNLib.Plugins.Essentials.Oauth.Tokens async Task ITokenManager.RevokeTokensForAppAsync(string appId, CancellationToken cancellation) { await using UserAppContext ctx = new (Options); - await ctx.OpenTransactionAsync(cancellation); //Get the token from the db if it exists ActiveToken[] at = await (from t in ctx.OAuthTokens where t.ApplicationId == appId |