This commit is contained in:
max 2025-05-05 16:00:18 +02:00
parent 05b95c6050
commit 46dbd8c6f5
4 changed files with 38 additions and 11 deletions

View File

@ -1,33 +1,33 @@
using System.Security.Claims;
using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace DotBased.AspNet.Authority.Controllers; namespace DotBased.AspNet.Authority.Controllers;
[ApiController] [ApiController]
[Microsoft.AspNetCore.Mvc.Route("[controller]")] [Route("[controller]")]
public class AuthorityController : ControllerBase public class AuthorityController : ControllerBase
{ {
[Inject]
public IAuthenticationService AuthenticationService { get; set; }
[HttpGet("auth/login")] [HttpGet("auth/login")]
[AllowAnonymous] [AllowAnonymous]
public async Task<ActionResult> LoginFromSchemeAsync([FromQuery(Name = "s")] string? scheme) public async Task<ActionResult> LoginFromSchemeAsync([FromQuery(Name = "s")] string? scheme)
{ {
var authResult = await HttpContext.AuthenticateAsync(); var cPrincipal = new ClaimsPrincipal();
await HttpContext.SignInAsync(cPrincipal);
return Ok(); return Ok();
} }
[HttpGet("auth/logout")] [HttpGet("auth/logout")]
public async Task<ActionResult> LogoutAsync() public async Task<ActionResult> LogoutAsync()
{ {
await HttpContext.SignOutAsync();
return Ok(); return Ok();
} }
[HttpGet("info")] [HttpGet("info")]
[AllowAnonymous]
public async Task<ActionResult<JsonDocument>> GetAuthorityInfoAsync() public async Task<ActionResult<JsonDocument>> GetAuthorityInfoAsync()
{ {
return Ok(); return Ok();

View File

@ -14,20 +14,19 @@ namespace DotBased.AspNet.Authority.Handlers;
public class AuthorityLoginAuthenticationHandler(IOptionsMonitor<AuthorityLoginOptions> options, public class AuthorityLoginAuthenticationHandler(IOptionsMonitor<AuthorityLoginOptions> options,
ILoggerFactory logger, ILoggerFactory logger,
UrlEncoder encoder, UrlEncoder encoder,
ISystemClock clock, AuthorityManager manager) : SignInAuthenticationHandler<AuthorityLoginOptions>(options, logger, encoder)
AuthorityManager manager) : SignInAuthenticationHandler<AuthorityLoginOptions>(options, logger, encoder, clock)
{ {
protected override Task<AuthenticateResult> HandleAuthenticateAsync() protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
protected override Task HandleSignOutAsync(AuthenticationProperties properties) protected override Task HandleSignOutAsync(AuthenticationProperties? properties)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
protected override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) protected override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }

View File

@ -1,6 +1,8 @@
using System.Security.Claims;
using DotBased.AspNet.Authority.Models.Options.Auth; using DotBased.AspNet.Authority.Models.Options.Auth;
using DotBased.Logging; using DotBased.Logging;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
namespace DotBased.AspNet.Authority.Services; namespace DotBased.AspNet.Authority.Services;
@ -17,4 +19,30 @@ public class AuthorityAuthenticationService(
public IReadOnlyCollection<SchemeInfo> GetSchemeInfos(SchemeType schemeType) => _options.SchemeInfoMap.Where(s => s.Type == schemeType).ToList(); public IReadOnlyCollection<SchemeInfo> GetSchemeInfos(SchemeType schemeType) => _options.SchemeInfoMap.Where(s => s.Type == schemeType).ToList();
public IReadOnlyCollection<SchemeInfo> GetAllSchemeInfos() => _options.SchemeInfoMap; public IReadOnlyCollection<SchemeInfo> GetAllSchemeInfos() => _options.SchemeInfoMap;
public override Task<AuthenticateResult> AuthenticateAsync(HttpContext context, string? scheme)
{
return base.AuthenticateAsync(context, scheme);
}
public override Task ChallengeAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
{
return base.ChallengeAsync(context, scheme, properties);
}
public override Task SignInAsync(HttpContext context, string? scheme, ClaimsPrincipal principal, AuthenticationProperties? properties)
{
return base.SignInAsync(context, scheme, principal, properties);
}
public override Task SignOutAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
{
return base.SignOutAsync(context, scheme, properties);
}
public override Task ForbidAsync(HttpContext context, string? scheme, AuthenticationProperties? properties)
{
return base.ForbidAsync(context, scheme, properties);
}
} }

View File

@ -35,7 +35,7 @@ builder.Services.AddAuthority()
{ {
options.DefaultScheme = AuthorityDefaults.Scheme.Cookie.AuthenticationScheme; options.DefaultScheme = AuthorityDefaults.Scheme.Cookie.AuthenticationScheme;
options.DefaultSignInScheme = AuthorityDefaults.Scheme.Authority.AuthenticationScheme; options.DefaultSignInScheme = AuthorityDefaults.Scheme.Authority.AuthenticationScheme;
options.DefaultChallengeScheme = AuthorityDefaults.Scheme.Authority.AuthenticationScheme; options.DefaultSignOutScheme = AuthorityDefaults.Scheme.Authority.AuthenticationScheme;
options.SchemeInfoMap = [ options.SchemeInfoMap = [
new SchemeInfo new SchemeInfo
{ {