Skip to content

Commit

Permalink
Upgrade and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ustims committed Mar 13, 2024
1 parent a3c544d commit 2d5f2d3
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<VersionPrefix>1.0.3</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
12 changes: 11 additions & 1 deletion CloudFabric.Auth.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudFabric.Auth", "CloudFabric.Auth.csproj", "{55AB88B0-1446-45D9-B347-1B74260C15C7}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudFabric.Auth", "CloudFabric.Auth\CloudFabric.Auth.csproj", "{A876E3A3-2815-41C7-A7F8-B2005736EAFA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudFabric.Auth.Tests", "CloudFabric.Auth.Tests\CloudFabric.Auth.Tests.csproj", "{76CE2B15-9111-479E-BB63-E7A3EBC057DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,14 @@ Global
{55AB88B0-1446-45D9-B347-1B74260C15C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55AB88B0-1446-45D9-B347-1B74260C15C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55AB88B0-1446-45D9-B347-1B74260C15C7}.Release|Any CPU.Build.0 = Release|Any CPU
{A876E3A3-2815-41C7-A7F8-B2005736EAFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A876E3A3-2815-41C7-A7F8-B2005736EAFA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A876E3A3-2815-41C7-A7F8-B2005736EAFA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A876E3A3-2815-41C7-A7F8-B2005736EAFA}.Release|Any CPU.Build.0 = Release|Any CPU
{76CE2B15-9111-479E-BB63-E7A3EBC057DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76CE2B15-9111-479E-BB63-E7A3EBC057DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76CE2B15-9111-479E-BB63-E7A3EBC057DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76CE2B15-9111-479E-BB63-E7A3EBC057DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions CloudFabric.Auth/CloudFabric.Auth.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<VersionPrefix>1.0.3</VersionPrefix>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.Net.Http.Headers;

namespace CloudFabric.Auth
{
public static class ServiceCollectionExtensions
{
private static readonly string JWT_OR_COOKIE_SCHEME = "JWT_OR_COOKIE";

/// <summary>
/// Adds token validation services, policies and handlers
/// </summary>
Expand All @@ -26,15 +30,22 @@ public static IServiceCollection AddCloudFabricAuth(
IConfigurationSection authClientOptions
)
{
services.AddHttpClient();
services.Configure<AuthClientOptions>(authClientOptions);
services.AddScoped<IntrospectionHttpClientProvider>();

services.AddAuthentication(
options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultAuthenticateScheme = JWT_OR_COOKIE_SCHEME;
options.DefaultChallengeScheme = JWT_OR_COOKIE_SCHEME;
options.DefaultScheme = JWT_OR_COOKIE_SCHEME;
}
)
.AddCookie(
options =>
{
}
)
.AddJwtBearer(
Expand All @@ -44,9 +55,16 @@ IConfigurationSection authClientOptions
options.RequireHttpsMetadata = false;
var jwtHandler = new JwtSecurityTokenHandler();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
jwtHandler.InboundClaimTypeMap.Clear();
options.SecurityTokenValidators.Clear();
options.SecurityTokenValidators.Add(jwtHandler);
jwtHandler.OutboundClaimTypeMap.Clear();
//options.SecurityTokenValidators.Clear();
//options.SecurityTokenValidators.Add(jwtHandler);
options.TokenHandlers.Clear();
options.TokenHandlers.Add(jwtHandler);
var tokenOptions = authClientOptions.Get<AuthClientOptions>();
options.TokenValidationParameters =
Expand All @@ -64,7 +82,21 @@ IConfigurationSection authClientOptions
)
};
}
);
)
.AddPolicyScheme(JWT_OR_COOKIE_SCHEME, JWT_OR_COOKIE_SCHEME, options =>
{
// runs on each request
options.ForwardDefaultSelector = context =>
{
// filter by auth type
string authorization = context.Request.Headers[HeaderNames.Authorization];
if (!string.IsNullOrEmpty(authorization) && authorization.StartsWith("Bearer "))
return JwtBearerDefaults.AuthenticationScheme;
// otherwise always check for cookie auth
return CookieAuthenticationDefaults.AuthenticationScheme;
};
});

//Register the Permission policy handlers
services.AddSingleton<IAuthorizationMiddlewareResultHandler, PermissionResultHandler>();
Expand Down
3 changes: 1 addition & 2 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="CloudFabric" value="https://pkgs.dev.azure.com/CloudFabriccommerce/_packaging/CloudFabric/nuget/v3/index.json" />
</packageSources>
</configuration>
</configuration>

0 comments on commit 2d5f2d3

Please sign in to comment.