mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-10-16 19:40:33 -04:00
49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Extensions;
|
|
using API.Services;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace API.Controllers;
|
|
|
|
[Route("[controller]")]
|
|
public class OidcController: ControllerBase
|
|
{
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("login")]
|
|
public IActionResult Login(string returnUrl = "/")
|
|
{
|
|
var properties = new AuthenticationProperties { RedirectUri = returnUrl };
|
|
return Challenge(properties, IdentityServiceExtensions.OpenIdConnect);
|
|
}
|
|
|
|
[HttpGet("logout")]
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
|
|
if (!Request.Cookies.ContainsKey(OidcService.CookieName))
|
|
{
|
|
return Redirect("/");
|
|
}
|
|
|
|
var res = await Request.HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
if (!res.Succeeded || res.Properties == null || string.IsNullOrEmpty(res.Properties.GetString(OidcService.IdToken)))
|
|
{
|
|
HttpContext.Response.Cookies.Delete(OidcService.CookieName);
|
|
return Redirect("/");
|
|
}
|
|
|
|
return SignOut(
|
|
new AuthenticationProperties { RedirectUri = "/login" },
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
IdentityServiceExtensions.OpenIdConnect);
|
|
}
|
|
|
|
}
|