Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rainxh11 committed May 8, 2022
1 parent 933869c commit e803711
Show file tree
Hide file tree
Showing 21 changed files with 962 additions and 3 deletions.
63 changes: 63 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto

###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp

###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary

###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary

###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

.psd
assets/psd
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Ahmed Chakhoum
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
Expand Down
24 changes: 24 additions & 0 deletions Package.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>Chargily.EpayGateway.NET</id>
<version>1.0.2</version>
<authors>Ahmed Chakhoum (github.com/rainxh11)</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>https://github.com/rainxh11/Chargily.EpayGateway.NET</projectUrl>
<description>C# .NET Plugin for Chargily Epay Gateway</description>
<releaseNotes>First Release.</releaseNotes>
<readme>README.md</readme>
<icon>chargily.png</icon>
<copyright>$copyright$</copyright>
<tags>chargily epay ccp cib payment gateway aspnet xamarin maui plugin algerieposte</tags>
</metadata>
<files>
<file src=".\assets\chargily.png" target="\assets" />
<file src=".\README.md" target="\" />
<file src="**" target="\" exclude="Chargily.EpayGateway.NET\obj\**\*.*;.git;.git\**\*.*;;.vs\**\*.*;Chargily.EpayGateway.NET.AspNetCore.MinimalAPIExample\**\*.*:"/>
</files>
<dependencies>
<group targetFramework=".NETStandard2.0" />
</dependencies>
</package>
86 changes: 84 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,84 @@
# TokenRevoke.NET
Utility to blacklist tokens
<img src="https://raw.githubusercontent.com/rainxh11/Revoke.NET/master/assets/revoke.net.svg" width="300">

[![Latest version](https://img.shields.io/nuget/v/Revoke.NET.svg)](https://www.nuget.org/packages/Revoke.NET/)

# Revoke.NET
.NET Utility to revoke access based on some given criterias including but not limited to:
- Web Tokens like JWT Bearer token
- 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
```powershell
PM> Install-Package Revoke.NET
```

# How to use
simple create a new BlackList Store of type `IBlackListStore`
```csharp
using Revoke.NET;

var store = MemoryBlackListStore.CreateStore();
// Create a blacklist store, core package come with non-persistent in-memory store
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<SomeType>(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
```

# Usage with ASP.NET Core
```csharp
using Revoke.NET;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddRevokeStore(() => /* provide a BlackList Store */)
.AddHttpContextRevokeMiddleware(
context => { /* create custom key selector from HttpContext */ },
response => { /* create a custom response to be sent when a request is revoked */ }
);
```

### JWT Bearer Token Example
```csharp
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;
using Revoke.NET;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddRevokeInMemoryStore() // Register a Revoke Store
.AddJWTBearerTokenRevokeMiddleware(); // Register a Revoke Middleware
var app = builder.Build();

app.UseRevoke(); // Use Middleware before calling UseAuthorization()
app.UseAuthorization();
app.UseAuthentication();

app.MapGet("/logout", async ([FromServices] IBlackListStore store, HttpRequest request) =>
{
var token = AuthenticationHeaderValue.Parse(request.Headers.Authorization).Parameter;

await store.Revoke(token);

return true;
});

app.Run();
```

# Other BlackList Stores:
- Akavache Store ***_Coming..._***
- MonkeyCache Store ***_Coming..._***
- MongoDB Store ***_Coming..._***
- Redis Store ***_Coming..._***
27 changes: 27 additions & 0 deletions Revoke.NET.MinimalAPIExample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Mvc;
using Revoke.NET;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddRevokeInMemoryStore() // Register a Revoke Store
.AddJWTBearerTokenRevokeMiddleware(); // Register a Revoke Middleware

var app = builder.Build();

app.UseRevoke(); // Use Middleware before calling UseAuthorization()

app.UseAuthorization();
app.UseAuthentication();

app.MapGet("/logout", async ([FromServices] IBlackListStore store, HttpRequest request) =>
{
var token = AuthenticationHeaderValue.Parse(request.Headers.Authorization).Parameter;

await store.Revoke(token);

return true;
});

app.Run();
31 changes: 31 additions & 0 deletions Revoke.NET.MinimalAPIExample/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:3306",
"sslPort": 0
}
},
"profiles": {
"Revoke.NET.MinimalAPIExample": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
17 changes: 17 additions & 0 deletions Revoke.NET.MinimalAPIExample/Revoke.NET.MinimalAPIExample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Revoke.NET\Revoke.NET.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions Revoke.NET.MinimalAPIExample/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions Revoke.NET.MinimalAPIExample/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
31 changes: 31 additions & 0 deletions Revoke.NET.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32421.90
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Revoke.NET", "Revoke.NET\Revoke.NET.csproj", "{5830C4B1-9EC8-4393-9918-766A0CCA6F04}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Revoke.NET.MinimalAPIExample", "Revoke.NET.MinimalAPIExample\Revoke.NET.MinimalAPIExample.csproj", "{24F89710-1FE6-4D0F-9E63-4D6042D1A569}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5830C4B1-9EC8-4393-9918-766A0CCA6F04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5830C4B1-9EC8-4393-9918-766A0CCA6F04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5830C4B1-9EC8-4393-9918-766A0CCA6F04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5830C4B1-9EC8-4393-9918-766A0CCA6F04}.Release|Any CPU.Build.0 = Release|Any CPU
{24F89710-1FE6-4D0F-9E63-4D6042D1A569}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24F89710-1FE6-4D0F-9E63-4D6042D1A569}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24F89710-1FE6-4D0F-9E63-4D6042D1A569}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24F89710-1FE6-4D0F-9E63-4D6042D1A569}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D9C520A5-3E5C-4695-9738-795EA9090A55}
EndGlobalSection
EndGlobal
28 changes: 28 additions & 0 deletions Revoke.NET/IBlackListItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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; }
}
}
20 changes: 20 additions & 0 deletions Revoke.NET/IBlackListStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Revoke.NET
{
public interface IBlackListStore
{
Task<IEnumerable<T>> GetAll<T>() where T : IBlackListItem;
Task<T> Get<T>(string key) where T : IBlackListItem;
Task<bool> Delete(string key);
Task<bool> Revoke(string key);
Task<bool> Revoke(string key, TimeSpan expireAfter);
Task<bool> Revoke(string key, DateTimeOffset expireOn);
Task DeleteExpired();
Task DeleteAll();
Task<bool> IsRevoked(string key);
}
}
21 changes: 21 additions & 0 deletions Revoke.NET/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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.
Loading

0 comments on commit e803711

Please sign in to comment.