Skip to content

Commit

Permalink
reorganize test cases
Browse files Browse the repository at this point in the history
- remove policy with multiple schemes sample tests and codes
- remove mode
  • Loading branch information
dncsvr committed Apr 26, 2024
1 parent 3699bd7 commit 93404cd
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,89 @@
using Microsoft.AspNetCore.Mvc;

namespace MultiAuthentication.Controllers;


[Authorize]
[ApiController]
[Route("authentication")]
public class PolicyAndSchemesController(IHttpContextAccessor httpContextAccessor) : ControllerBase
{
[HttpGet]
[Authorize]
[HttpGet]
[AllowAnonymous]
[Produces("application/json")]
[Route("default-scheme")]
public object DefaultScheme()
[Route("anonymous")]
public object Anonymous()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToDictionary();
return principal.ToIdentityList();
}

[HttpGet]
[Authorize(AuthenticationSchemes = "OrganizationId")]
[HttpGet]
[Produces("application/json")]
[Route("single-scheme")]
public object SingleScheme()
[Route("default")]
public object Default()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToDictionary();
return principal.ToIdentityList();
}

[HttpGet]
[Authorize(AuthenticationSchemes = "ApiKey,BearerToken")]
[Authorize(Policy = "RequireClaim")]
[Produces("application/json")]
[Route("multi-authentication-scheme")]
public object MultiAuthenticationScheme()
[Route("default/claim")]
public object DefaultWithClaim()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToDictionary();
return principal.ToIdentityList();
}

[HttpGet]
[Authorize(Policy = "Backend")]
[Authorize(AuthenticationSchemes = "Alternative")]
[Produces("application/json")]
[Route("alternative")]
public object Alternative()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToIdentityList();
}

[HttpGet]
[Authorize(AuthenticationSchemes = "Alternative")]
[Authorize(Policy = "RequireClaim")]
[Produces("application/json")]
[Route("alternative/claim")]
public object AlternativeWithClaim()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToIdentityList();
}

[HttpGet]
[Authorize(AuthenticationSchemes = "Alternative")]
[Authorize(AuthenticationSchemes = "Default")]
[Produces("application/json")]
[Route("backend-policy")]
public object BackendPolicy()
[Route("any")]
public object Any()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToDictionary();
return principal.ToIdentityList();
}

[HttpGet]
[Authorize(Policy = "ExternalSystem")]
[Authorize(AuthenticationSchemes = "Alternative")]
[Authorize(AuthenticationSchemes = "Default")]
[Authorize(Policy = "RequireClaim")]
[Produces("application/json")]
[Route("external-system-policy")]
public object ExternalSystemPolicy()
[Route("any/claim")]
public object AnyWithClaim()
{
var principal = httpContextAccessor.HttpContext?.User ?? throw new();

return principal.ToDictionary();
return principal.ToIdentityList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Text.Encodings.Web;

namespace MultiAuthentication.Handlers;

public class AlternativeAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Context.Request.Headers.TryGetValue("X-Alternative", out var value))
{
return Task.FromResult(AuthenticateResult.NoResult());
}

if (value.ToString() != "Alternative")
{
return Task.FromResult(AuthenticateResult.Fail("Invalid token value"));
}

var principal = new ClaimsPrincipal(new ClaimsIdentity("Alternative"));
if (Context.Request.Headers.TryGetValue("X-Claim", out var claim))
{
((ClaimsIdentity?)principal.Identity)?.AddClaim(new($"{claim}", $"{claim}"));
}

return Task.FromResult(AuthenticateResult.Success(new(principal, "Alternative")));
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using System.Security.Claims;
using System.Text.Encodings.Web;

namespace MultiAuthentication.Handlers;

public class ApiKeyAuthenticationHandler(
public class AnonymousAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (Context.Request.Headers.TryGetValue("X-API-KEY", out StringValues key))
{
var claim = new Claim("Token", $"{key}");
var principal = new ClaimsPrincipal(new ClaimsIdentity([claim], "ApiKey"));

return Task.FromResult(AuthenticateResult.Success(new(principal, "ApiKey")));
}

{
return Task.FromResult(AuthenticateResult.NoResult());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using System.Security.Claims;
using System.Text.Encodings.Web;

namespace MultiAuthentication.Handlers;

public class DefaultAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder
) : AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Context.Request.Headers.TryGetValue("X-Default", out var value))
{
return Task.FromResult(AuthenticateResult.NoResult());
}

if (value.ToString() != "Default")
{
return Task.FromResult(AuthenticateResult.Fail("Invalid token value"));
}

var principal = new ClaimsPrincipal(new ClaimsIdentity("Default"));
if (Context.Request.Headers.TryGetValue("X-Claim", out var claim))
{
((ClaimsIdentity?)principal.Identity)?.AddClaim(new($"{claim}", $"{claim}"));
}

return Task.FromResult(AuthenticateResult.Success(new(principal, "Default")));
}
}

This file was deleted.

Loading

0 comments on commit 93404cd

Please sign in to comment.