//
// ▄▄▄▄ ▄▄▄▄ ▄▄▄▄
// ▄▄▄▄▄▄█ █▄▄▄▄▄█ █▄▄▄▄▄█ █
// █__ --█ █__ --█ ◄█ - █
// █▄▄▄▄▄█▄▄█▄▄▄▄▄█▄▄█▄▄█▄▄▄▄▄█
// ┍━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ━━━━ ━ ━┉ ┉ ┉
// │ Copyright (c) JP Dillingham.
// │
// │ This program is free software: you can redistribute it and/or modify
// │ it under the terms of the GNU Affero General Public License as published
// │ by the Free Software Foundation, version 3.
// │
// │ This program is distributed in the hope that it will be useful,
// │ but WITHOUT ANY WARRANTY; without even the implied warranty of
// │ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// │ GNU Affero General Public License for more details.
// │
// │ You should have received a copy of the GNU Affero General Public License
// │ along with this program. If not, see https://www.gnu.org/licenses/.
// │
// │ This program is distributed with Additional Terms pursuant to Section 7
// │ of the AGPLv3. See the LICENSE file in the root directory of this
// │ project for the complete terms and conditions.
// │
// │ https://slskd.org
// │
// ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ╌ ╌╌╌╌ ╌
// │ SPDX-FileCopyrightText: JP Dillingham
// │ SPDX-License-Identifier: AGPL-3.0-only
// ╰───────────────────────────────────────────╶──── ─ ─── ─ ── ──┈ ┈
//
namespace slskd.Authentication
{
using System.Security.Principal;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
///
/// Passthrough authentication.
///
public static class PassthroughAuthentication
{
///
/// Gets the Passthrough authentication scheme name.
///
public static string AuthenticationScheme { get; } = "Passthrough";
}
///
/// Handles passthrough authentication.
///
public class PassthroughAuthenticationHandler : AuthenticationHandler
{
///
/// Initializes a new instance of the class.
///
/// An options monitor.
/// A logger factory.
/// A url encoder.
public PassthroughAuthenticationHandler(IOptionsMonitor optionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder)
: base(optionsMonitor, logger, urlEncoder)
{
}
///
/// Authenticates using the configured and .
///
/// A successful authentication result containing a default ticket.
protected override Task HandleAuthenticateAsync()
{
var identity = new GenericIdentity(Options.Username);
var principal = new GenericPrincipal(identity, new[] { Options.Role.ToString() });
var ticket = new AuthenticationTicket(principal, new AuthenticationProperties(), PassthroughAuthentication.AuthenticationScheme);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}
///
/// Passthrough authentication options.
///
public class PassthroughAuthenticationOptions : AuthenticationSchemeOptions
{
///
/// Initializes a new instance of the class.
///
public PassthroughAuthenticationOptions()
{
}
///
/// Gets or sets the username for the passed-through authentication ticket.
///
public string Username { get; set; } = "Anonymous";
///
/// Gets or sets the role for the passed-through authentication ticket.
///
public Role Role { get; set; } = Role.Administrator;
}
}