aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Security/PassthroughAuthentication.cs
blob: 69b94df5ca3109a389aa8877416113be4c9b2aae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace slskd.Security
{
    using Microsoft.AspNetCore.Authentication;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using System.Security.Principal;
    using System.Text.Encodings.Web;
    using System.Threading.Tasks;

    public class PassthroughAuthentication
    {
        public static string AuthenticationScheme { get; } = "Passthrough";
    }

    public class PassthroughAuthenticationHandler : AuthenticationHandler<PassthroughAuthenticationOptions>
    {
        public PassthroughAuthenticationHandler(IOptionsMonitor<PassthroughAuthenticationOptions> optionsMonitor, ILoggerFactory logger, UrlEncoder urlEncoder, ISystemClock systemClock)
            : base(optionsMonitor, logger, urlEncoder, systemClock)
        {            
        }

        protected override Task<AuthenticateResult> HandleAuthenticateAsync()
        {
            var identity = new GenericIdentity(Options.Username);
            var principal = new GenericPrincipal(identity, new[] { "User" });
            var ticket = new AuthenticationTicket(principal , new AuthenticationProperties(), PassthroughAuthentication.AuthenticationScheme);

            return Task.FromResult(AuthenticateResult.Success(ticket));
        }
    }

    public class PassthroughAuthenticationOptions : AuthenticationSchemeOptions
    {
        public PassthroughAuthenticationOptions()
        {
        }

        public string Username { get; set; }
    }
}