Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions AuthorizationInterceptor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{02EA681E-C7D
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{200E9ECE-6ABD-4AB7-AA41-31FC95E618E7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor.Extensions.HybridCache", "src\AuthorizationInterceptor.Extensions.HybridCache\AuthorizationInterceptor.Extensions.HybridCache.csproj", "{4F7333E9-31FC-4D90-9D8F-2BBB075E2412}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor.Extensions.HybridCache.Tests", "tests\AuthorizationInterceptor.Extensions.HybridCache.Tests\AuthorizationInterceptor.Extensions.HybridCache.Tests.csproj", "{95360C34-CA3E-47F7-825E-69E00AF42C71}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -61,6 +65,14 @@ Global
{72A318F6-B2F7-B014-0657-071FE035DE96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{72A318F6-B2F7-B014-0657-071FE035DE96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{72A318F6-B2F7-B014-0657-071FE035DE96}.Release|Any CPU.Build.0 = Release|Any CPU
{4F7333E9-31FC-4D90-9D8F-2BBB075E2412}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F7333E9-31FC-4D90-9D8F-2BBB075E2412}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F7333E9-31FC-4D90-9D8F-2BBB075E2412}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F7333E9-31FC-4D90-9D8F-2BBB075E2412}.Release|Any CPU.Build.0 = Release|Any CPU
{95360C34-CA3E-47F7-825E-69E00AF42C71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95360C34-CA3E-47F7-825E-69E00AF42C71}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95360C34-CA3E-47F7-825E-69E00AF42C71}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95360C34-CA3E-47F7-825E-69E00AF42C71}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,6 +86,8 @@ Global
{458A0C0A-6D65-DCF5-6B21-30D8E1374EE6} = {200E9ECE-6ABD-4AB7-AA41-31FC95E618E7}
{F368BB6C-9EDD-4727-58BD-68799CF990B3} = {200E9ECE-6ABD-4AB7-AA41-31FC95E618E7}
{72A318F6-B2F7-B014-0657-071FE035DE96} = {200E9ECE-6ABD-4AB7-AA41-31FC95E618E7}
{4F7333E9-31FC-4D90-9D8F-2BBB075E2412} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
{95360C34-CA3E-47F7-825E-69E00AF42C71} = {200E9ECE-6ABD-4AB7-AA41-31FC95E618E7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {16C3D677-0CE4-4111-9BD9-EC1C11956A8C}
Expand Down
28 changes: 23 additions & 5 deletions samples/AuthorizationInterceptor.Sample.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TargetApi", "TargetApi\Targ
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceApi", "SourceApi\SourceApi.csproj", "{835CBED5-B68E-4DF4-9B29-9BAD55824F76}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AuthorizationInterceptor", "..\src\AuthorizationInterceptor.csproj", "{6A402E57-96D7-43E7-9B87-7514CBDF29B0}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor", "..\src\AuthorizationInterceptor\AuthorizationInterceptor.csproj", "{426A39AB-B325-3774-1961-2E0C961E10AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor.Extensions.MemoryCache", "..\src\AuthorizationInterceptor.Extensions.MemoryCache\AuthorizationInterceptor.Extensions.MemoryCache.csproj", "{8622B530-4867-3E69-414B-D0A1DB23EE5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor.Extensions.DistributedCache", "..\src\AuthorizationInterceptor.Extensions.DistributedCache\AuthorizationInterceptor.Extensions.DistributedCache.csproj", "{114EA1B9-F717-2EA4-F5D1-7C9E97E37CAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AuthorizationInterceptor.Extensions.HybridCache", "..\src\AuthorizationInterceptor.Extensions.HybridCache\AuthorizationInterceptor.Extensions.HybridCache.csproj", "{B6B934AB-B383-0E92-B44A-22EC3C0F3B20}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -23,10 +29,22 @@ Global
{835CBED5-B68E-4DF4-9B29-9BAD55824F76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{835CBED5-B68E-4DF4-9B29-9BAD55824F76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{835CBED5-B68E-4DF4-9B29-9BAD55824F76}.Release|Any CPU.Build.0 = Release|Any CPU
{6A402E57-96D7-43E7-9B87-7514CBDF29B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A402E57-96D7-43E7-9B87-7514CBDF29B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A402E57-96D7-43E7-9B87-7514CBDF29B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A402E57-96D7-43E7-9B87-7514CBDF29B0}.Release|Any CPU.Build.0 = Release|Any CPU
{426A39AB-B325-3774-1961-2E0C961E10AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{426A39AB-B325-3774-1961-2E0C961E10AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{426A39AB-B325-3774-1961-2E0C961E10AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{426A39AB-B325-3774-1961-2E0C961E10AC}.Release|Any CPU.Build.0 = Release|Any CPU
{8622B530-4867-3E69-414B-D0A1DB23EE5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8622B530-4867-3E69-414B-D0A1DB23EE5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8622B530-4867-3E69-414B-D0A1DB23EE5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8622B530-4867-3E69-414B-D0A1DB23EE5E}.Release|Any CPU.Build.0 = Release|Any CPU
{114EA1B9-F717-2EA4-F5D1-7C9E97E37CAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{114EA1B9-F717-2EA4-F5D1-7C9E97E37CAC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{114EA1B9-F717-2EA4-F5D1-7C9E97E37CAC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{114EA1B9-F717-2EA4-F5D1-7C9E97E37CAC}.Release|Any CPU.Build.0 = Release|Any CPU
{B6B934AB-B383-0E92-B44A-22EC3C0F3B20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6B934AB-B383-0E92-B44A-22EC3C0F3B20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6B934AB-B383-0E92-B44A-22EC3C0F3B20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6B934AB-B383-0E92-B44A-22EC3C0F3B20}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
70 changes: 67 additions & 3 deletions samples/SourceApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using AuthorizationInterceptor.Extensions.Abstractions.Handlers;
using AuthorizationInterceptor.Extensions.Abstractions.Headers;
using AuthorizationInterceptor.Extensions.Abstractions.Interceptors;
using AuthorizationInterceptor.Extensions.DistributedCache.Extensions;
using AuthorizationInterceptor.Extensions.HybridCache.Extensions;
using AuthorizationInterceptor.Extensions.MemoryCache.Extensions;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand All @@ -10,12 +13,49 @@
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Add the cache options
builder.Services.AddMemoryCache();
builder.Services.AddStackExchangeRedisCache(opt =>
{
opt.InstanceName = "SourceApi";
opt.Configuration = "localhost:6379";
});
builder.Services.AddHybridCache();

builder.Services.AddHttpClient("TargetApiAuth")
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5121"));

builder.Services.AddHttpClient("TargetApi")
builder.Services.AddHttpClient("TargetApiWithNoInterceptor")
.AddAuthorizationInterceptorHandler<TargetApiAuthClass>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5121"));

builder.Services.AddHttpClient("TargetApiWithMemoryCache")
.AddAuthorizationInterceptorHandler<TargetApiAuthClass>(opt =>
{
opt.UseMemoryCacheInterceptor();
})
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5121"));

builder.Services.AddHttpClient("TargetApiWithDistributedCache")
.AddAuthorizationInterceptorHandler<TargetApiAuthClass>(opt =>
{
opt.UseDistributedCacheInterceptor();
})
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5121"));

builder.Services.AddHttpClient("TargetApiWithHybridCache")
.AddAuthorizationInterceptorHandler<TargetApiAuthClass>(opt =>
{
opt.UseHybridCacheInterceptor();
})
.ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5121"));

builder.Services.AddHttpClient("TargetApiWithCustomInterceptors")
.AddAuthorizationInterceptorHandler<TargetApiAuthClass>(opt =>
{
opt.UseMemoryCacheInterceptor();
opt.UseDistributedCacheInterceptor();

opt.UseCustomInterceptor<CustomInterceptor1>();
opt.UseCustomInterceptor<CustomInterceptor2>();
opt.UseCustomInterceptor<CustomInterceptor3>();
Expand All @@ -30,9 +70,33 @@
app.UseSwaggerUI();
}

app.MapGet("/data", async (IHttpClientFactory factory) =>
app.MapGet("/test/TargetApiWithNoInterceptor", async (IHttpClientFactory factory) =>
{
var client = factory.CreateClient("TargetApiWithNoInterceptor");
return await client.GetAsync("/data");
});

app.MapGet("/test/TargetApiWithMemoryCache", async (IHttpClientFactory factory) =>
{
var client = factory.CreateClient("TargetApiWithMemoryCache");
return await client.GetAsync("/data");
});

app.MapGet("/test/TargetApiWithDistributedCache", async (IHttpClientFactory factory) =>
{
var client = factory.CreateClient("TargetApiWithDistributedCache");
return await client.GetAsync("/data");
});

app.MapGet("/test/TargetApiWithHybridCache", async (IHttpClientFactory factory) =>
{
var client = factory.CreateClient("TargetApiWithHybridCache");
return await client.GetAsync("/data");
});

app.MapGet("/test/TargetApiWithCustomInterceptors", async (IHttpClientFactory factory) =>
{
var client = factory.CreateClient("TargetApi");
var client = factory.CreateClient("TargetApiWithCustomInterceptors");
return await client.GetAsync("/data");
});

Expand Down
8 changes: 7 additions & 1 deletion samples/SourceApi/SourceApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Hybrid" Version="9.3.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="9.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AuthorizationInterceptor.csproj" />
<ProjectReference Include="..\..\src\AuthorizationInterceptor.Extensions.DistributedCache\AuthorizationInterceptor.Extensions.DistributedCache.csproj" />
<ProjectReference Include="..\..\src\AuthorizationInterceptor.Extensions.HybridCache\AuthorizationInterceptor.Extensions.HybridCache.csproj" />
<ProjectReference Include="..\..\src\AuthorizationInterceptor.Extensions.MemoryCache\AuthorizationInterceptor.Extensions.MemoryCache.csproj" />
<ProjectReference Include="..\..\src\AuthorizationInterceptor\AuthorizationInterceptor.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions samples/SourceApi/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
},
"Console": {
"FormatterName": "simple",
"FormatterOptions": {
"SingleLine": true,
"IncludeScopes": false,
"TimestampFormat": "dd/MM/yyyy-HH:mm:ss UTC ",
"UseUtcTimestamp": true
}
}
}
}
15 changes: 12 additions & 3 deletions samples/TargetApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Text.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -15,8 +16,10 @@
app.UseSwaggerUI();
}

app.MapPost("/auth", (UserContainer users) =>
app.MapPost("/auth", (UserContainer users, ILoggerFactory loggerFactory) =>
{
var logger = loggerFactory.CreateLogger("TargetApi");
logger.LogDebug("Received request on /auth endpoint");
var user = new User
{
AccessToken = Guid.NewGuid().ToString(),
Expand All @@ -33,8 +36,11 @@
.WithName("auth")
.WithOpenApi();

app.MapPost("/refresh", (UserContainer users, [FromQuery] string refresh) =>
app.MapPost("/refresh", (UserContainer users, [FromQuery] string refresh, ILoggerFactory loggerFactory) =>
{
var logger = loggerFactory.CreateLogger("TargetApi");
logger.LogDebug("Received request on /refresh endpoint");

if (string.IsNullOrEmpty(refresh))
return Results.Unauthorized();

Expand All @@ -58,8 +64,11 @@
.WithName("refresh")
.WithOpenApi();

app.MapGet("/data", (HttpRequest request, UserContainer users, [FromHeader(Name = "Authorization")] string? token = null) =>
app.MapGet("/data", (HttpRequest request, UserContainer users, ILoggerFactory loggerFactory, [FromHeader(Name = "Authorization")] string? token = null) =>
{
var logger = loggerFactory.CreateLogger("TargetApi");
logger.LogDebug("Received request on /data endpoint");

if (string.IsNullOrWhiteSpace(token))
return Results.Unauthorized();

Expand Down
11 changes: 10 additions & 1 deletion samples/TargetApi/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
},
"Console": {
"FormatterName": "simple",
"FormatterOptions": {
"SingleLine": true,
"IncludeScopes": false,
"TimestampFormat": "dd/MM/yyyy-HH:mm:ss UTC ",
"UseUtcTimestamp": true
}
}
}
}
2 changes: 1 addition & 1 deletion samples/TargetApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>AuthorizationInterceptor.Extensions.Abstractions.Tests</_Parameter1>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using AuthorizationInterceptor.Extensions.Abstractions.Headers;
using System.Threading;
using System.Threading.Tasks;

namespace AuthorizationInterceptor.Extensions.Abstractions.Handlers
namespace AuthorizationInterceptor.Extensions.Abstractions.Handlers;

/// <summary>
/// Defines an abstraction interface to handle the origin of the authorization headers.
/// </summary>
public interface IAuthenticationHandler
{
/// <summary>
/// Defines an abstraction interface to handle the origin of the authorization headers.
/// Implementation of the authentication method.
/// </summary>
public interface IAuthenticationHandler
{
/// <summary>
/// Implementation of the authentication method.
/// </summary>
/// <param name="expiredHeaders">If a previous authentication was made, the expiredHeaders will be passed; otherwise, it will be null.
/// The expiredHeaders are mostly used to refresh tokens or to re-authenticate with previous header information.
/// </param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>Returns a set of <see cref="AuthorizationHeaders"/> with authorized headers.</returns>
ValueTask<AuthorizationHeaders?> AuthenticateAsync(AuthorizationHeaders? expiredHeaders, CancellationToken cancellationToken);
}
/// <param name="expiredHeaders">If a previous authentication was made, the expiredHeaders will be passed; otherwise, it will be null.
/// The expiredHeaders are mostly used to refresh tokens or to re-authenticate with previous header information.
/// </param>
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
/// <returns>Returns a set of <see cref="AuthorizationHeaders"/> with authorized headers.</returns>
ValueTask<AuthorizationHeaders?> AuthenticateAsync(AuthorizationHeaders? expiredHeaders, CancellationToken cancellationToken);
}
Loading
Loading