diff --git a/Revoke.NET.Akavache/AkavacheBlackList.cs b/Revoke.NET.Akavache/AkavacheBlackList.cs new file mode 100644 index 0000000..0bb827c --- /dev/null +++ b/Revoke.NET.Akavache/AkavacheBlackList.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Reactive.Linq; +using System.Threading.Tasks; +using Akavache; +using Registrations = Akavache.Registrations; + +namespace Revoke.NET.Akavache +{ + public class AkavacheBlackList : IBlackList + { + private readonly IBlobCache _blackList; + private static TimeSpan? _defaultTtl; + + private AkavacheBlackList(IBlobCache blobcache) + { + this._blackList = blobcache; + } + + public static async Task CreateStoreAsync(string cacheName, IBlobCache blobCache, + TimeSpan? defaultTtl = null) + { + _defaultTtl = defaultTtl; + Registrations.Start(cacheName); + await blobCache.Vacuum(); + return new AkavacheBlackList(blobCache); + } + + + public async Task Delete(string key) + { + try + { + await _blackList.Invalidate(key); + return true; + } + catch + { + return false; + } + } + + public async Task DeleteAll() + { + await _blackList.InvalidateAll(); + } + + public async Task IsRevoked(string key) + { + try + { + await _blackList.Vacuum(); + var exist = await _blackList.Get(key); + return exist.Length > 0; + } + catch + { + return false; + } + } + + public async Task Revoke(string key) + { + try + { + await _blackList.InsertObject(key, key, DateTimeOffset.Now.Add(_defaultTtl ?? TimeSpan.MaxValue)); + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key, TimeSpan expireAfter) + { + try + { + await _blackList.InsertObject(key, key, DateTimeOffset.Now.Add(expireAfter)); + + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key, DateTime expireOn) + { + try + { + await _blackList.InsertObject(key, key, expireOn); + + return true; + } + catch + { + return false; + } + } + } +} \ No newline at end of file diff --git a/Revoke.NET.Akavache/AkavacheBlackListStore.cs b/Revoke.NET.Akavache/AkavacheBlackListStore.cs deleted file mode 100644 index 2ebd0a0..0000000 --- a/Revoke.NET.Akavache/AkavacheBlackListStore.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reactive.Linq; -using System.Threading.Tasks; -using Akavache; -using Registrations = Akavache.Registrations; - -namespace Revoke.NET.Akavache -{ - public class AkavacheBlackListStore : IBlackListStore - { - private readonly IBlobCache blacklist; - - private AkavacheBlackListStore(IBlobCache blobcache) - { - this.blacklist = blobcache; - } - - public static async Task CreateStoreAsync(string cacheName, IBlobCache blobCache) - { - Registrations.Start(cacheName); - await blobCache.Vacuum(); - return new AkavacheBlackListStore(blobCache); - } - - public async Task Delete(string key) - { - try - { - await blacklist.Invalidate(key); - return true; - } - catch - { - return false; - } - } - - public async Task DeleteAll() - { - await blacklist.InvalidateAll(); - } - - - public async Task DeleteExpired() - { - await blacklist.Vacuum(); - ; - } - - public async Task Get(string key) where T : IBlackListItem - { - return await blacklist.GetObject(key); - } - - public async Task> GetAll() where T : IBlackListItem - { - return await blacklist.GetAllObjects(); - } - - public async Task IsRevoked(string key) - { - try - { - await blacklist.Vacuum(); - var exist = await blacklist.Get(key); - return exist.Length > 0; - } - catch - { - return false; - } - } - - public async Task Revoke(string key) - { - try - { - await blacklist.InsertObject(key, new BlackListItem(key, DateTimeOffset.MaxValue), - DateTimeOffset.MaxValue); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(string key, TimeSpan expireAfter) - { - try - { - await blacklist.InsertObject(key, new BlackListItem(key, DateTimeOffset.Now.Add(expireAfter)), - expireAfter); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(string key, DateTimeOffset expireOn) - { - try - { - await blacklist.InsertObject(key, new BlackListItem(key, expireOn), expireOn); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(T item) where T : IBlackListItem - { - try - { - await blacklist.InsertObject(item.Key, item, item.ExpireOn); - return true; - } - catch - { - return false; - } - } - } -} \ No newline at end of file diff --git a/Revoke.NET.Akavache/README.md b/Revoke.NET.Akavache/README.md index 9948c49..8c4c345 100644 --- a/Revoke.NET.Akavache/README.md +++ b/Revoke.NET.Akavache/README.md @@ -21,10 +21,6 @@ var key = "[ID String of something to be blacklisted]"; await store.Revoke(key, TimeSpan.FromHours(24)); // Revoke access to a key for 24 hours -await store.Revoke(model); // Revoke an item with custom type - -var item = store.Get(key); // Retrieve a blacklisted item, SomeType must implement interface 'IBlackListItem' - await store.IsRevoked(key); // Check if key is blacklisted await store.Delete(key); // Delete a key from blacklist diff --git a/Revoke.NET.Akavache/Revoke.NET.Akavache.csproj b/Revoke.NET.Akavache/Revoke.NET.Akavache.csproj index e33b8cc..2792eaf 100644 --- a/Revoke.NET.Akavache/Revoke.NET.Akavache.csproj +++ b/Revoke.NET.Akavache/Revoke.NET.Akavache.csproj @@ -7,7 +7,7 @@ readme.md LICENSE Library - 1.0.6 + 2.0.0 Chakhoum Ahmed (github.com/rainxh11) Revoke.NET Akavache Store Extension @@ -24,12 +24,12 @@ - + - - - - + + + + diff --git a/Revoke.NET.Akavache/RevokeService.cs b/Revoke.NET.Akavache/RevokeService.cs index 8186edf..1453872 100644 --- a/Revoke.NET.Akavache/RevokeService.cs +++ b/Revoke.NET.Akavache/RevokeService.cs @@ -11,7 +11,7 @@ public static class RevokeService public static IServiceCollection AddRevokeAkavacheSQLiteStore(this IServiceCollection services) { return services - .AddSingleton(provider => AkavacheBlackListStore + .AddSingleton(provider => AkavacheBlackList .CreateStoreAsync("RevokeStore", BlobCache.LocalMachine) .GetAwaiter() .GetResult()); @@ -20,7 +20,7 @@ public static IServiceCollection AddRevokeAkavacheSQLiteStore(this IServiceColle public static IServiceCollection AddRevokeAkavacheInMemoryStore(this IServiceCollection services) { return services - .AddSingleton(provider => AkavacheBlackListStore + .AddSingleton(provider => AkavacheBlackList .CreateStoreAsync("RevokeStore", BlobCache.InMemory) .GetAwaiter() .GetResult()); @@ -30,7 +30,7 @@ public static IServiceCollection AddRevokeAkavacheStore(this IServiceCollection Func configBlobCache) { return services - .AddSingleton(provider => AkavacheBlackListStore + .AddSingleton(provider => AkavacheBlackList .CreateStoreAsync("RevokeStore", configBlobCache(provider)) .GetAwaiter() .GetResult()); diff --git a/Revoke.NET.AspNetCore/Revoke.NET.AspNetCore.csproj b/Revoke.NET.AspNetCore/Revoke.NET.AspNetCore.csproj index 6bca796..f43ebe7 100644 --- a/Revoke.NET.AspNetCore/Revoke.NET.AspNetCore.csproj +++ b/Revoke.NET.AspNetCore/Revoke.NET.AspNetCore.csproj @@ -7,7 +7,7 @@ readme.md LICENSE Library - 1.0.7 + 2.0.0 Chakhoum Ahmed (github.com/rainxh11) Revoke.NET ASP.NET Core Extension @@ -27,10 +27,10 @@ - - - - + + + + diff --git a/Revoke.NET.AspNetCore/RevokeHttpMiddleware.cs b/Revoke.NET.AspNetCore/RevokeHttpMiddleware.cs index 036ac79..d858d4f 100644 --- a/Revoke.NET.AspNetCore/RevokeHttpMiddleware.cs +++ b/Revoke.NET.AspNetCore/RevokeHttpMiddleware.cs @@ -11,7 +11,7 @@ namespace Revoke.NET.AspNetCore { public class RevokeHttpMiddleware : IMiddleware { - private readonly IBlackListStore store; + private readonly IBlackList store; private readonly Func selector; #nullable enable @@ -19,7 +19,7 @@ public class RevokeHttpMiddleware : IMiddleware private Func>? responseFunc; #nullable disable - public RevokeHttpMiddleware(IBlackListStore store, ILogger logger, + public RevokeHttpMiddleware(IBlackList store, ILogger logger, Func selector) { this.store = store; @@ -27,13 +27,13 @@ public RevokeHttpMiddleware(IBlackListStore store, ILogger this.selector = selector; } - public RevokeHttpMiddleware(IBlackListStore store, Func selector) + public RevokeHttpMiddleware(IBlackList store, Func selector) { this.store = store; this.selector = selector; } - public RevokeHttpMiddleware(IBlackListStore store, ILogger logger, + public RevokeHttpMiddleware(IBlackList store, ILogger logger, Func selector, Func> responseFunc) { this.store = store; @@ -42,7 +42,7 @@ public RevokeHttpMiddleware(IBlackListStore store, ILogger this.responseFunc = responseFunc; } - public RevokeHttpMiddleware(IBlackListStore store, Func selector, + public RevokeHttpMiddleware(IBlackList store, Func selector, Func> responseFunc) { this.store = store; diff --git a/Revoke.NET.AspNetCore/RevokeService.cs b/Revoke.NET.AspNetCore/RevokeService.cs index 9012fb0..de743f5 100644 --- a/Revoke.NET.AspNetCore/RevokeService.cs +++ b/Revoke.NET.AspNetCore/RevokeService.cs @@ -18,7 +18,7 @@ public static IServiceCollection AddHttpContextRevokeMiddleware(this IServiceCol return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, selector); }); @@ -37,7 +37,7 @@ public static IServiceCollection AddHttpContextRevokeMiddleware(this IServiceCol return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, selector, responseFunc); }); @@ -64,7 +64,7 @@ public static IServiceCollection AddJWTBearerTokenRevokeMiddleware(this IService return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, bearerTokenSelector); }); @@ -93,7 +93,7 @@ public static IServiceCollection AddJWTBearerTokenRevokeMiddleware(this IService return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, bearerTokenSelector, responseFunc); }); @@ -111,7 +111,7 @@ public static IServiceCollection AddIPRevokeMiddleware(this IServiceCollection s return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, ipSelector); }); @@ -131,7 +131,7 @@ public static IServiceCollection AddIPRevokeMiddleware(this IServiceCollection s return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, ipSelector, responseFunc); }); @@ -149,7 +149,7 @@ public static IServiceCollection AddUserIdRevokeMiddleware(this IServiceCollecti return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, ipSelector); }); @@ -169,7 +169,7 @@ public static IServiceCollection AddUserIdRevokeMiddleware(this IServiceCollecti return services .AddSingleton(provider => { - var store = provider.GetService(); + var store = provider.GetService(); var logger = provider.GetService>(); return new RevokeHttpMiddleware(store, logger, ipSelector, responseFunc); }); diff --git a/Revoke.NET.EasyCaching/EasyCachingBlackList.cs b/Revoke.NET.EasyCaching/EasyCachingBlackList.cs new file mode 100644 index 0000000..bb07317 --- /dev/null +++ b/Revoke.NET.EasyCaching/EasyCachingBlackList.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using EasyCaching.Core; + +namespace Revoke.NET.EasyCaching; + +internal class EasyCachingBlackList : IBlackList +{ + private readonly IEasyCachingProvider _easyCaching; + private readonly TimeSpan? _defaultTtl; + + public EasyCachingBlackList(IEasyCachingProvider easyCaching, TimeSpan? defaultTtl = null) + { + _defaultTtl = defaultTtl; + _easyCaching = easyCaching; + } + + public async Task Revoke(string key, TimeSpan expireAfter) + { + try + { + await _easyCaching.SetAsync(key, key, expireAfter); + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key, DateTime expireOn) + { + try + { + await _easyCaching.SetAsync(key, key, expireOn - DateTime.Now); + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key) + { + try + { + await _easyCaching.SetAsync(key, key, _defaultTtl ?? TimeSpan.MaxValue); + return true; + } + catch + { + return false; + } + } + + public async Task Delete(string key) + { + try + { + await _easyCaching.RemoveAsync(key); + return true; + } + catch + { + return false; + } + } + + public async Task DeleteAll() + { + try + { + await _easyCaching.FlushAsync(); + } + catch + { + } + } + + public async Task IsRevoked(string key) + { + try + { + return await _easyCaching.ExistsAsync(key); + } + catch + { + return false; + } + } +} \ No newline at end of file diff --git a/Revoke.NET.EasyCaching/Revoke.NET.EasyCaching.csproj b/Revoke.NET.EasyCaching/Revoke.NET.EasyCaching.csproj new file mode 100644 index 0000000..db04b9e --- /dev/null +++ b/Revoke.NET.EasyCaching/Revoke.NET.EasyCaching.csproj @@ -0,0 +1,39 @@ + + + + netstandard2.0 + 10.0 + revoke.net.png + LICENSE + Library + 2.0.0 + + Chakhoum Ahmed (github.com/rainxh11) + Revoke.NET EasyCaching Store Extension + © 2022 Chakhoum Ahmed + LICENSE + https://github.com/rainxh11/Revoke.NET + https://github.com/rainxh11/Revoke.NET/tree/main/Revoke.NET.EasyCaching + github + true + revoke;easycaching;permission;deny;blacklist;expiration;invalidate;store + $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage + False + True + + + + + + + + + + + + + + + + diff --git a/Revoke.NET.EasyCaching/RevokeService.cs b/Revoke.NET.EasyCaching/RevokeService.cs new file mode 100644 index 0000000..a0f5256 --- /dev/null +++ b/Revoke.NET.EasyCaching/RevokeService.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text; +using EasyCaching.Core; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Revoke.NET.EasyCaching; + +namespace Revoke.NET.EasyCaching +{ + public static class RevokeService + { + public static IServiceCollection AddRevokeEasyCaching(this IServiceCollection services, + IEasyCachingProvider easyCachingProvider, TimeSpan? defaultTtl = null) + { + return services + .AddSingleton(provider => + new EasyCachingBlackList(easyCachingProvider, defaultTtl)); + } + + public static IServiceCollection AddRevokeEasyCaching(this IServiceCollection services, + Func easyCachingConfig, TimeSpan? defaultTtl = null) + { + return services + .AddSingleton(provider => + { + var factory = provider.GetService(); + return new EasyCachingBlackList(easyCachingConfig?.Invoke(factory), defaultTtl); + }); + } + + public static IServiceCollection AddRevokeEasyCaching(this IServiceCollection services, + TimeSpan? defaultTtl = null) + { + return services + .AddSingleton(provider => + { + var easyCachingProvider = provider.GetService(); + return new EasyCachingBlackList(easyCachingProvider, defaultTtl); + }); + } + } +} \ No newline at end of file diff --git a/Revoke.NET.MinimalAPIExample/Program.cs b/Revoke.NET.MinimalAPIExample/Program.cs index a01d5f5..635b70d 100644 --- a/Revoke.NET.MinimalAPIExample/Program.cs +++ b/Revoke.NET.MinimalAPIExample/Program.cs @@ -16,7 +16,7 @@ app.UseAuthorization(); app.UseAuthentication(); -app.MapGet("/logout", async ([FromServices] IBlackListStore store, HttpRequest request) => +app.MapGet("/logout", async ([FromServices] IBlackList store, HttpRequest request) => { var token = AuthenticationHeaderValue.Parse(request.Headers.Authorization).Parameter; diff --git a/Revoke.NET.MinimalAPIExample/Revoke.NET.MinimalAPIExample.csproj b/Revoke.NET.MinimalAPIExample/Revoke.NET.MinimalAPIExample.csproj index fcf810f..38b9831 100644 --- a/Revoke.NET.MinimalAPIExample/Revoke.NET.MinimalAPIExample.csproj +++ b/Revoke.NET.MinimalAPIExample/Revoke.NET.MinimalAPIExample.csproj @@ -9,13 +9,9 @@ - - - - - - - + + + diff --git a/Revoke.NET.MongoDB/MongoBlackList.cs b/Revoke.NET.MongoDB/MongoBlackList.cs new file mode 100644 index 0000000..9c46c40 --- /dev/null +++ b/Revoke.NET.MongoDB/MongoBlackList.cs @@ -0,0 +1,131 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Driver; +using Revoke.NET; + +namespace Revoke.NET.MongoDB +{ + public class MongoBlackListItem + { + [BsonId] public ObjectId Id { get; set; } = ObjectId.GenerateNewId(); + + public MongoBlackListItem(string key, DateTime expireOn) + { + Key = key; + ExpireOn = expireOn; + } + + public string Key { get; set; } + public DateTime ExpireOn { get; set; } + } + + public class MongoBlackList : IBlackList + { + private readonly IMongoCollection _blacklist; + + private MongoBlackList(IMongoCollection blacklist) + { + this._blacklist = blacklist; + } + + public static async Task CreateStoreAsync(string dbName, + MongoClientSettings clientSettings) + { + var client = new MongoClient(clientSettings); + + var db = client.GetDatabase(dbName); + + var keyIndex = Builders.IndexKeys.Ascending(x => x.Key); + var ttlIndex = Builders.IndexKeys.Ascending(x => x.ExpireOn); + + var collection = db.GetCollection(nameof(MongoBlackListItem)); + + await collection.Indexes.CreateOneAsync( + new CreateIndexModel(keyIndex, new CreateIndexOptions() { Unique = true })); + await collection.Indexes.CreateOneAsync( + new CreateIndexModel(ttlIndex, + new CreateIndexOptions() { ExpireAfter = TimeSpan.FromMinutes(1) })); + + return new MongoBlackList(collection); + } + + public async Task Revoke(string key, TimeSpan expireAfter) + { + try + { + await _blacklist.InsertOneAsync(new MongoBlackListItem(key, DateTime.Now.Add(expireAfter))); + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key, DateTime expireOn) + { + try + { + await _blacklist.InsertOneAsync(new MongoBlackListItem(key, expireOn)); + return true; + } + catch + { + return false; + } + } + + public async Task Revoke(string key) + { + try + { + await _blacklist.InsertOneAsync(new MongoBlackListItem(key, DateTime.MaxValue)); + return true; + } + catch + { + return false; + } + } + + public async Task Delete(string key) + { + try + { + var delete = await _blacklist.DeleteOneAsync(x => x.Key == key); + return delete.IsAcknowledged; + } + catch + { + return false; + } + } + + public async Task DeleteAll() + { + try + { + await _blacklist.DeleteManyAsync(x => true); + } + catch + { + } + } + + public async Task IsRevoked(string key) + { + try + { + var item = await _blacklist.Find(x => x.Key == key).SingleAsync(); + return item.ExpireOn > DateTime.Now; + } + catch + { + return false; + } + } + } +} \ No newline at end of file diff --git a/Revoke.NET.MongoDB/MongoBlackListStore.cs b/Revoke.NET.MongoDB/MongoBlackListStore.cs deleted file mode 100644 index a283a1c..0000000 --- a/Revoke.NET.MongoDB/MongoBlackListStore.cs +++ /dev/null @@ -1,127 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using MongoDB.Driver; -using Revoke.NET; - -namespace Revoke.NET.MongoDB -{ - public class MongoBlackListStore : IBlackListStore - { - private readonly IMongoCollection blacklist; - - private MongoBlackListStore(IMongoCollection blacklist) - { - this.blacklist = blacklist; - } - - public static async Task CreateStoreAsync(string dbName, - MongoClientSettings clientSettings) - { - var client = new MongoClient(clientSettings); - - var db = client.GetDatabase(dbName); - - var keyIndex = Builders.IndexKeys.Ascending(x => x.Key); - var ttlIndex = Builders.IndexKeys.Ascending(x => x.ExpireOn); - - var collection = db.GetCollection(nameof(BlackListItem)); - - await collection.Indexes.CreateOneAsync( - new CreateIndexModel(keyIndex, new CreateIndexOptions() { Unique = true })); - await collection.Indexes.CreateOneAsync( - new CreateIndexModel(ttlIndex, - new CreateIndexOptions() { ExpireAfter = TimeSpan.FromMinutes(1) })); - - return new MongoBlackListStore(collection); - } - - public async Task Delete(string key) - { - var result = await blacklist.DeleteOneAsync(x => x.Key == key); - return result.IsAcknowledged; - } - - public async Task DeleteAll() - { - await blacklist.DeleteManyAsync(x => true); - } - - - public async Task DeleteExpired() - { - await blacklist.DeleteManyAsync(x => x.ExpireOn < DateTimeOffset.Now); - } - - public async Task Get(string key) where T : IBlackListItem - { - return await blacklist.Database.GetCollection(nameof(IBlackListItem)).Find(x => x.Key == key) - .FirstAsync(); - } - - public async Task> GetAll() where T : IBlackListItem - { - return await blacklist.Database.GetCollection(nameof(IBlackListItem)).Find(x => true).ToListAsync(); - } - - public async Task IsRevoked(string key) - { - var result = await blacklist.Find(x => x.Key == key).CountDocumentsAsync(); - return result > 0; - } - - public async Task Revoke(string key) - { - try - { - await blacklist.InsertOneAsync(new BlackListItem(key, DateTimeOffset.MaxValue)); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(string key, TimeSpan expireAfter) - { - try - { - await blacklist.InsertOneAsync(new BlackListItem(key, DateTimeOffset.Now.Add(expireAfter))); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(string key, DateTimeOffset expireOn) - { - try - { - await blacklist.InsertOneAsync(new BlackListItem(key, expireOn)); - return true; - } - catch - { - return false; - } - } - - public async Task Revoke(T item) where T : IBlackListItem - { - try - { - await blacklist.Database - .GetCollection(nameof(IBlackListItem)) - .InsertOneAsync(item); - return true; - } - catch - { - return false; - } - } - } -} \ No newline at end of file diff --git a/Revoke.NET.MongoDB/README.md b/Revoke.NET.MongoDB/README.md index 989d884..e9a3041 100644 --- a/Revoke.NET.MongoDB/README.md +++ b/Revoke.NET.MongoDB/README.md @@ -20,10 +20,6 @@ var key = "[ID String of something to be blacklisted]"; await store.Revoke(key, TimeSpan.FromHours(24)); // Revoke access to a key for 24 hours -await store.Revoke(model); // Revoke an item with custom type - -var item = store.Get(key); // Retrieve a blacklisted item, SomeType must implement interface 'IBlackListItem' - await store.IsRevoked(key); // Check if key is blacklisted await store.Delete(key); // Delete a key from blacklist diff --git a/Revoke.NET.MongoDB/Revoke.NET.MongoDB.csproj b/Revoke.NET.MongoDB/Revoke.NET.MongoDB.csproj index 219fc6c..df0763d 100644 --- a/Revoke.NET.MongoDB/Revoke.NET.MongoDB.csproj +++ b/Revoke.NET.MongoDB/Revoke.NET.MongoDB.csproj @@ -7,7 +7,7 @@ readme.md LICENSE Library - 1.0.7 + 2.0.0 Chakhoum Ahmed (github.com/rainxh11) Revoke.NET MongoDB Store Extension @@ -26,8 +26,9 @@ - - + + + diff --git a/Revoke.NET.MongoDB/RevokeService.cs b/Revoke.NET.MongoDB/RevokeService.cs index 2584f3b..8330fc7 100644 --- a/Revoke.NET.MongoDB/RevokeService.cs +++ b/Revoke.NET.MongoDB/RevokeService.cs @@ -11,7 +11,7 @@ public static class RevokeService public static IServiceCollection AddRevokeMongoStore(this IServiceCollection services) { return services - .AddSingleton(provider => MongoBlackListStore.CreateStoreAsync( + .AddSingleton(provider => MongoBlackList.CreateStoreAsync( "RevokeStore", MongoClientSettings.FromConnectionString("mongodb://127.0.0.1:27017/RevokeStore")) .GetAwaiter() @@ -22,7 +22,7 @@ public static IServiceCollection AddRevokeMongoStore(this IServiceCollection ser MongoClientSettings settings) { return services - .AddSingleton(provider => MongoBlackListStore.CreateStoreAsync( + .AddSingleton(provider => MongoBlackList.CreateStoreAsync( dbName, settings) .GetAwaiter() diff --git a/Revoke.NET.MonkeyCache/Class1.cs b/Revoke.NET.MonkeyCache/Class1.cs deleted file mode 100644 index 5f0b6fa..0000000 --- a/Revoke.NET.MonkeyCache/Class1.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace Revoke.NET.MonkeyCache -{ - public class Class1 - { - - } -} diff --git a/Revoke.NET.MonkeyCache/Revoke.NET.EasyCaching.csproj b/Revoke.NET.MonkeyCache/Revoke.NET.EasyCaching.csproj deleted file mode 100644 index 6c58211..0000000 --- a/Revoke.NET.MonkeyCache/Revoke.NET.EasyCaching.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - netstandard2.0 - True - - - diff --git a/Revoke.NET.Redis/LICENSE b/Revoke.NET.Redis/LICENSE deleted file mode 100644 index 52130b0..0000000 --- a/Revoke.NET.Redis/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022-present, CHAKHOUM AHMED - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/Revoke.NET.Redis/README.md b/Revoke.NET.Redis/README.md index 92df46b..374dcf9 100644 --- a/Revoke.NET.Redis/README.md +++ b/Revoke.NET.Redis/README.md @@ -18,10 +18,6 @@ var key = "[ID String of something to be blacklisted]"; await store.Revoke(key, TimeSpan.FromHours(24)); // Revoke access to a key for 24 hours -await store.Revoke(model); // Revoke an item with custom type - -var item = store.Get(key); // Retrieve a blacklisted item, SomeType must implement interface 'IBlackListItem' - await store.IsRevoked(key); // Check if key is blacklisted await store.Delete(key); // Delete a key from blacklist diff --git a/Revoke.NET.Redis/RedisBlackList.cs b/Revoke.NET.Redis/RedisBlackList.cs new file mode 100644 index 0000000..9f5e5e8 --- /dev/null +++ b/Revoke.NET.Redis/RedisBlackList.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using StackExchange.Redis; +using System.Text.Json; + +namespace Revoke.NET.Redis +{ + public class RedisBlackList : IBlackList + { + private IDatabase _blackList; + private IEnumerable servers; + private static TimeSpan? _defaultTtl; + + private RedisBlackList(IDatabase blackList, IEnumerable servers) + { + this._blackList = blackList; + this.servers = servers; + } + + public static async Task CreateStoreAsync(string connectionString, TimeSpan? defaultTtl = null) + { + _defaultTtl = defaultTtl; + var options = ConfigurationOptions.Parse(connectionString); + options.AllowAdmin = true; + var redis = await ConnectionMultiplexer.ConnectAsync(options); + var blacklist = redis.GetDatabase(); + var servers = redis.GetEndPoints().Select(x => redis.GetServer(x)); + + return new RedisBlackList(blacklist, servers); + } + + public async Task Delete(string key) + { + return await _blackList.KeyDeleteAsync(key); + } + + public async Task DeleteAll() + { + foreach (var key in servers.SelectMany(x => x.Keys())) + { + await _blackList.KeyDeleteAsync(key); + } + } + + public async Task IsRevoked(string key) + { + var value = await _blackList.StringGetAsync(key); + return !value.HasValue; + } + + public async Task Revoke(string key) + { + var value = await _blackList.StringSetAndGetAsync(key, key, _defaultTtl ?? TimeSpan.MaxValue); + return value.HasValue; + } + + public async Task Revoke(string key, TimeSpan expireAfter) + { + var value = await _blackList.StringSetAndGetAsync(key, key, expireAfter); + return value.HasValue; + } + + public async Task Revoke(string key, DateTime expireOn) + { + var value = await _blackList.StringSetAndGetAsync(key, key, expireOn - DateTimeOffset.Now); + return value.HasValue; + } + } +} \ No newline at end of file diff --git a/Revoke.NET.Redis/RedisBlackListStore.cs b/Revoke.NET.Redis/RedisBlackListStore.cs deleted file mode 100644 index 1c54e64..0000000 --- a/Revoke.NET.Redis/RedisBlackListStore.cs +++ /dev/null @@ -1,103 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using StackExchange.Redis; -using System.Text.Json; - -namespace Revoke.NET.Redis -{ - public class RedisBlackListStore : IBlackListStore - { - private IDatabase blacklist; - private IEnumerable servers; - - private RedisBlackListStore(IDatabase blacklist, IEnumerable servers) - { - this.blacklist = blacklist; - this.servers = servers; - } - - public static async Task CreateStoreAsync(string connectionString) - { - var options = ConfigurationOptions.Parse(connectionString); - options.AllowAdmin = true; - var redis = await ConnectionMultiplexer.ConnectAsync(options); - var blacklist = redis.GetDatabase(); - var servers = redis.GetEndPoints().Select(x => redis.GetServer(x)); - - return new RedisBlackListStore(blacklist, servers); - } - - public async Task Delete(string key) - { - return await blacklist.KeyDeleteAsync(key); - } - - public async Task DeleteAll() - { - foreach (var key in servers.SelectMany(x => x.Keys())) - { - await blacklist.KeyDeleteAsync(key); - } - } - - - public Task DeleteExpired() - { - return Task.CompletedTask; - } - - public async Task Get(string key) where T : IBlackListItem - { - var value = await blacklist.StringGetAsync(key); - return JsonSerializer.Deserialize((string)value.Box()); - } - - public async Task> GetAll() where T : IBlackListItem - { - var temp = new List(); - foreach (var key in servers.SelectMany(x => x.Keys())) - { - var value = await blacklist.StringGetAsync(key); - temp.Add(JsonSerializer.Deserialize((string)value.Box())); - } - - return temp; - } - - public async Task IsRevoked(string key) - { - var value = await blacklist.StringGetAsync(key); - return !value.HasValue; - } - - public async Task Revoke(string key) - { - var value = await blacklist.StringSetAndGetAsync(key, - JsonSerializer.Serialize(new BlackListItem(key, DateTimeOffset.MaxValue))); - return value.HasValue; - } - - public async Task Revoke(string key, TimeSpan expireAfter) - { - var value = await blacklist.StringSetAndGetAsync(key, - JsonSerializer.Serialize(new BlackListItem(key, DateTimeOffset.MaxValue)), expireAfter); - return value.HasValue; - } - - public async Task Revoke(string key, DateTimeOffset expireOn) - { - var value = await blacklist.StringSetAndGetAsync(key, - JsonSerializer.Serialize(new BlackListItem(key, DateTimeOffset.MaxValue)), - expireOn - DateTimeOffset.Now); - return value.HasValue; - } - - public async Task Revoke(T item) where T : IBlackListItem - { - var value = await blacklist.StringSetAndGetAsync(item.Key, JsonSerializer.Serialize(item)); - return value.HasValue; - } - } -} \ No newline at end of file diff --git a/Revoke.NET.Redis/Revoke.NET.Redis.csproj b/Revoke.NET.Redis/Revoke.NET.Redis.csproj index 5c858a7..aca0ca2 100644 --- a/Revoke.NET.Redis/Revoke.NET.Redis.csproj +++ b/Revoke.NET.Redis/Revoke.NET.Redis.csproj @@ -7,7 +7,7 @@ readme.md LICENSE Library - 1.0.6 + 2.0.0 Chakhoum Ahmed (github.com/rainxh11) Revoke.NET Redis Store Extension @@ -23,15 +23,16 @@ - + - - - - - - - + + + + + + + + diff --git a/Revoke.NET.Redis/RevokeService.cs b/Revoke.NET.Redis/RevokeService.cs index 574720a..4238429 100644 --- a/Revoke.NET.Redis/RevokeService.cs +++ b/Revoke.NET.Redis/RevokeService.cs @@ -7,7 +7,7 @@ public static class RevokeService public static IServiceCollection AddRevokeRedisStore(this IServiceCollection services) { return services - .AddSingleton(provider => RedisBlackListStore.CreateStoreAsync("127.0.0.1:6379") + .AddSingleton(provider => RedisBlackList.CreateStoreAsync("127.0.0.1:6379") .GetAwaiter() .GetResult()); } @@ -15,7 +15,7 @@ public static IServiceCollection AddRevokeRedisStore(this IServiceCollection ser public static IServiceCollection AddRevokeRedisStore(this IServiceCollection services, string connectionString) { return services - .AddSingleton(provider => RedisBlackListStore.CreateStoreAsync(connectionString) + .AddSingleton(provider => RedisBlackList.CreateStoreAsync(connectionString) .GetAwaiter() .GetResult()); } diff --git a/Revoke.NET.Redis/assets/revoke.net.png b/Revoke.NET.Redis/assets/revoke.net.png deleted file mode 100644 index 16774c1..0000000 Binary files a/Revoke.NET.Redis/assets/revoke.net.png and /dev/null differ diff --git a/Revoke.NET.sln b/Revoke.NET.sln index 2e91123..b4e126f 100644 --- a/Revoke.NET.sln +++ b/Revoke.NET.sln @@ -11,7 +11,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET.MongoDB", "Revok EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET.Akavache", "Revoke.NET.Akavache\Revoke.NET.Akavache.csproj", "{E3CECBB3-985E-40B1-A5B2-E312463A3A72}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET.EasyCaching", "Revoke.NET.MonkeyCache\Revoke.NET.EasyCaching.csproj", "{01B7F2DA-A733-4025-8B38-4C8E698D4C92}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET.EasyCaching", "Revoke.NET.EasyCaching\Revoke.NET.EasyCaching.csproj", "{01B7F2DA-A733-4025-8B38-4C8E698D4C92}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET.Redis", "Revoke.NET.Redis\Revoke.NET.Redis.csproj", "{A5F3BC02-9E8F-444F-BDF8-27C3815C2362}" EndProject diff --git a/Revoke.NET/IBlackListStore.cs b/Revoke.NET/IBlackList.cs similarity index 51% rename from Revoke.NET/IBlackListStore.cs rename to Revoke.NET/IBlackList.cs index 0436529..aad12cf 100644 --- a/Revoke.NET/IBlackListStore.cs +++ b/Revoke.NET/IBlackList.cs @@ -5,16 +5,12 @@ namespace Revoke.NET { - public interface IBlackListStore + public interface IBlackList { - Task> GetAll() where T : IBlackListItem; - Task Get(string key) where T : IBlackListItem; - Task Delete(string key); - Task Revoke(string key); Task Revoke(string key, TimeSpan expireAfter); - Task Revoke(string key, DateTimeOffset expireOn); - Task Revoke(T item) where T : IBlackListItem; - Task DeleteExpired(); + Task Revoke(string key, DateTime expireOn); + Task Revoke(string key); + Task Delete(string key); Task DeleteAll(); Task IsRevoked(string key); } diff --git a/Revoke.NET/IBlackListItem.cs b/Revoke.NET/IBlackListItem.cs deleted file mode 100644 index 750aff2..0000000 --- a/Revoke.NET/IBlackListItem.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; - -namespace Revoke.NET -{ - public class BlackListItem : IBlackListItem - { - public BlackListItem(string key, DateTimeOffset expireOn) - { - this.Key = key; - this.ExpireOn = expireOn; - } - - public BlackListItem(string key, TimeSpan expireAfter) - { - this.Key = key; - this.ExpireOn = DateTimeOffset.Now.Add(expireAfter); - } - - public string Key { get; } - public DateTimeOffset ExpireOn { get; } - } - - public interface IBlackListItem - { - string Key { get; } - DateTimeOffset ExpireOn { get; } - } -} \ No newline at end of file diff --git a/Revoke.NET/MemoryBlackList.cs b/Revoke.NET/MemoryBlackList.cs new file mode 100644 index 0000000..52c3b21 --- /dev/null +++ b/Revoke.NET/MemoryBlackList.cs @@ -0,0 +1,163 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Primitives; + +namespace Revoke.NET +{ + public class MemoryCacheBlackList : IBlackList + { + private IMemoryCache _memoryCache; + private TimeSpan? _defaultTtl; + private static CancellationTokenSource _resetCacheToken = new CancellationTokenSource(); + + public MemoryCacheBlackList(IMemoryCache memoryCache, TimeSpan? defaultTtl = null) + { + _defaultTtl = defaultTtl; + _memoryCache = memoryCache; + } + + public Task Revoke(string key) + { + var options = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.Normal) + .SetAbsoluteExpiration(_defaultTtl ?? TimeSpan.MaxValue); + options.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token)); + try + { + _memoryCache.Set(key, key, options); + return Task.FromResult(true); + } + catch + { + return Task.FromResult(false); + } + } + + public Task Delete(string key) + { + try + { + _memoryCache.Remove(key); + return Task.FromResult(true); + } + catch + { + return Task.FromResult(false); + } + } + + public Task DeleteAll() + { + if (_resetCacheToken != null && !_resetCacheToken.IsCancellationRequested && + _resetCacheToken.Token.CanBeCanceled) + { + _resetCacheToken.Cancel(); + _resetCacheToken.Dispose(); + } + + _resetCacheToken = new CancellationTokenSource(); + return Task.CompletedTask; + } + + + public Task IsRevoked(string key) + { + return Task.FromResult(_memoryCache.TryGetValue(key, out _)); + } + + public Task Revoke(string key, TimeSpan expireAfter) + { + var options = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.Normal) + .SetAbsoluteExpiration(expireAfter); + options.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token)); + try + { + _memoryCache.Set(key, key, options); + return Task.FromResult(true); + } + catch + { + return Task.FromResult(false); + } + } + + public Task Revoke(string key, DateTime expireOn) + { + var options = new MemoryCacheEntryOptions().SetPriority(CacheItemPriority.Normal) + .SetAbsoluteExpiration(expireOn); + options.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token)); + + try + { + _memoryCache.Set(key, key, options); + return Task.FromResult(true); + } + catch + { + return Task.FromResult(false); + } + } + } + + + public class MemoryBlackList : IBlackList + { + private ConcurrentDictionary _blackList; + private readonly TimeSpan? defaultTtl; + + private MemoryBlackList(TimeSpan? defaultTtl) + { + this.defaultTtl = defaultTtl; + _blackList = new ConcurrentDictionary(); + } + + public static MemoryBlackList CreateStore(TimeSpan? defaultExpirationDuration = null) + { + return new MemoryBlackList(defaultExpirationDuration); + } + + public Task Delete(string key) + { + return Task.FromResult(_blackList.TryRemove(key, out _)); + } + + public Task Revoke(string key, TimeSpan expireAfter) + { + return Task.FromResult(_blackList.TryAdd(key, DateTime.Now.Add(expireAfter))); + } + + public Task Revoke(string key, DateTime expireOn) + { + if (expireOn < DateTimeOffset.Now) return Task.FromResult(false); + return Task.FromResult(_blackList.TryAdd(key, expireOn)); + } + + public Task DeleteAll() + { + _blackList.Clear(); + return Task.CompletedTask; + } + + public Task IsRevoked(string key) + { + if (_blackList.TryGetValue(key, out var item)) + { + return Task.FromResult(item >= DateTime.Now); + } + + return Task.FromResult(false); + } + + public Task Revoke(string key) + { + if (DateTime.Now.Add(defaultTtl ?? TimeSpan.MaxValue) < DateTime.Now) + return Task.FromResult(false); + return Task.FromResult(_blackList.TryAdd(key, DateTime.Now.Add(defaultTtl ?? TimeSpan.MaxValue))); + } + } +} \ No newline at end of file diff --git a/Revoke.NET/MemoryBlackListStore.cs b/Revoke.NET/MemoryBlackListStore.cs deleted file mode 100644 index 8668056..0000000 --- a/Revoke.NET/MemoryBlackListStore.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Revoke.NET -{ - public class MemoryBlackListStore : IBlackListStore - { - private ConcurrentDictionary blackList; -#nullable enable - private TimeSpan? defaultTtl; -#nullable disable - - private MemoryBlackListStore(TimeSpan? defaultTtl) - { - this.defaultTtl = defaultTtl; - blackList = new ConcurrentDictionary(); - } - - public static MemoryBlackListStore CreateStore(TimeSpan? defaultExpirationDuration = null) - { - return new MemoryBlackListStore(defaultExpirationDuration); - } - - public Task Delete(string key) - { - return Task.FromResult(blackList.TryRemove(key, out _)); - } - - public Task DeleteAll() - { - blackList.Clear(); - return Task.CompletedTask; - } - - public Task DeleteExpired() - { - blackList = new ConcurrentDictionary( - blackList.Where(x => DateTime.Now > x.Value.ExpireOn)); - return Task.CompletedTask; - } - - public Task Get(string key) where T : IBlackListItem - { - if (blackList.TryGetValue(key, out var item)) - { - return Task.FromResult((T)item); - } - - return null; - } - - public Task> GetAll() where T : IBlackListItem - { - var items = blackList.Select(item => item.Value); - return Task.FromResult(items.Cast()); - } - - public Task IsRevoked(string key) - { - if (blackList.TryGetValue(key, out var item)) - { - return Task.FromResult(item.ExpireOn >= DateTimeOffset.Now); - } - - return Task.FromResult(false); - } - - public Task Revoke(string key) - { - var item = new BlackListItem(key, - defaultTtl is null ? DateTimeOffset.Now.Add(defaultTtl ?? TimeSpan.Zero) : DateTimeOffset.MaxValue); - - if (blackList.ContainsKey(key)) - { - return Task.FromResult(blackList.TryUpdate(key, item, item)); - } - else - { - return Task.FromResult(blackList.TryAdd(key, item)); - } - } - - public Task Revoke(string key, TimeSpan expireAfter) - { - var item = new BlackListItem(key, DateTimeOffset.Now.Add(defaultTtl ?? expireAfter)); - - if (blackList.ContainsKey(key)) - { - return Task.FromResult(blackList.TryUpdate(key, item, item)); - } - else - { - return Task.FromResult(blackList.TryAdd(key, item)); - } - } - - public Task Revoke(string key, DateTimeOffset expireOn) - { - var item = new BlackListItem(key, - defaultTtl is null ? DateTimeOffset.Now.Add(defaultTtl ?? TimeSpan.Zero) : expireOn); - - if (blackList.ContainsKey(key)) - { - return Task.FromResult(blackList.TryUpdate(key, item, item)); - } - else - { - return Task.FromResult(blackList.TryAdd(key, item)); - } - } - - public Task Revoke(T item) where T : IBlackListItem - { - return Task.FromResult(blackList.TryAdd(item.Key, item)); - } - } -} \ No newline at end of file diff --git a/Revoke.NET/README.md b/Revoke.NET/README.md index ea3beab..fcf4338 100644 --- a/Revoke.NET/README.md +++ b/Revoke.NET/README.md @@ -4,11 +4,14 @@ - HTTP Request Header Paramters, Query, URL, Host, IP, Cookies, Body, FormData, Claims...etc # Installation -**First**, install the `Revoke.NET` [NuGet package](https://www.nuget.org/packages/Revoke.NET) into your app +**First**, install the [`Revoke.NET`](https://www.nuget.org/packages/Revoke.NET) into your app ```powershell -PM> Install-Package Revoke.NET +Install-Package Revoke.NET +``` +or with dotnet cli: +```powershell +dotnet add package Revoke.NET ``` - # How to use simple create a new BlackList Store of type `IBlackListStore` ```csharp @@ -21,16 +24,22 @@ var key = "[ID String of something to be blacklisted]"; await store.Revoke(key, TimeSpan.FromHours(24)); // Revoke access to a key for 24 hours -var item = store.Get(key); // Retrieve a blacklisted item, SomeType must implement interface 'IBlackListItem' - -await store.Revoke(model); // Revoke an item with custom type +await store.Revoke(key); // Revoke access indefinetly or with the defaulTtl expiration -await store.IsRevoked(key); // Check if key is blacklisted +var revoked = await store.IsRevoked(key); // Check if key is blacklisted await store.Delete(key); // Delete a key from blacklist ``` # Usage with ASP.NET Core +Install the [`Revoke.NET.AspNetCore`](https://www.nuget.org/packages/Revoke.NET.AspNetCore) into your app +```powershell +Install-Package Revoke.NET.AspNetCore +``` +or with dotnet cli: +```powershell +dotnet add package Revoke.NET.AspNetCore +``` ```csharp using Revoke.NET; diff --git a/Revoke.NET/Revoke.NET.csproj b/Revoke.NET/Revoke.NET.csproj index d5fc0f6..9427edb 100644 --- a/Revoke.NET/Revoke.NET.csproj +++ b/Revoke.NET/Revoke.NET.csproj @@ -7,7 +7,7 @@ readme.md LICENSE Library - 1.0.5 + 2.0.1 Chakhoum Ahmed (github.com/rainxh11) .NET Utility to blacklist and revoke access to stuff @@ -25,9 +25,10 @@ - - - + + + + diff --git a/Revoke.NET/RevokeService.cs b/Revoke.NET/RevokeService.cs index e3081cd..4dcedff 100644 --- a/Revoke.NET/RevokeService.cs +++ b/Revoke.NET/RevokeService.cs @@ -3,13 +3,23 @@ using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Revoke.NET { public static class RevokeService { + public static IServiceCollection AddRevokeMemoryCacheStore(this IServiceCollection services, + TimeSpan? defaultTtl = null) + { + services.TryAddSingleton(provider => + new MemoryCacheBlackList(provider.GetService(), defaultTtl)); + return services; + } + /// /// Register default InMemory BlackList Store Service /// @@ -19,8 +29,9 @@ public static class RevokeService public static IServiceCollection AddRevokeInMemoryStore(this IServiceCollection services, TimeSpan? defaultTtl = null) { - return services - .AddSingleton(MemoryBlackListStore.CreateStore(defaultTtl)); + services + .TryAddSingleton(MemoryBlackList.CreateStore(defaultTtl)); + return services; } /// @@ -30,10 +41,11 @@ public static IServiceCollection AddRevokeInMemoryStore(this IServiceCollection /// /// public static IServiceCollection AddRevokeStore(this IServiceCollection services, - Func configure) + Func configure) { - return services - .AddSingleton(configure()); + services + .TryAddSingleton(configure()); + return services; } /// @@ -43,10 +55,11 @@ public static IServiceCollection AddRevokeStore(this IServiceCollection services /// /// public static IServiceCollection AddRevokeStore(this IServiceCollection services, - Func configure) + Func configure) { - return services - .AddSingleton(configure); + services + .TryAddSingleton(configure); + return services; } } } \ No newline at end of file diff --git a/Test/Program.cs b/Test/Program.cs index 4d9067d..57ba8b7 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -1,12 +1,12 @@ using Revoke.NET; using Revoke.NET.Redis; -var store = await RedisBlackListStore.CreateStoreAsync("localhost"); +var store = await RedisBlackList.CreateStoreAsync("localhost"); -await store.Revoke("Ahmed", DateTimeOffset.MaxValue); +await store.Revoke("Ahmed", DateTime.MaxValue); -var item = await store.Get("Ahmed"); +var revoked = await store.IsRevoked("Ahmed"); -Console.WriteLine(item.Key); +Console.WriteLine(revoked); Console.ReadKey(); \ No newline at end of file diff --git a/Test/Test.csproj b/Test/Test.csproj index b61d5a8..9adfe28 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/Revoke.NET.Akavache/assets/revoke.net.png b/assets/revoke.net.png similarity index 100% rename from Revoke.NET.Akavache/assets/revoke.net.png rename to assets/revoke.net.png