forked from loic-sharma/BaGet
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathNugetBasicAuthenticationHandler.cs
More file actions
114 lines (94 loc) · 4 KB
/
Copy pathNugetBasicAuthenticationHandler.cs
File metadata and controls
114 lines (94 loc) · 4 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text.Encodings.Web;
using System.Text;
using System.Threading.Tasks;
using System;
using BaGetter.Core;
using System.Linq;
namespace BaGetter.Web.Authentication;
public class NugetBasicAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
private readonly IOptions<BaGetterOptions> bagetterOptions;
public NugetBasicAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
IOptions<BaGetterOptions> bagetterOptions)
: base(options, logger, encoder)
{
this.bagetterOptions = bagetterOptions;
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (IsAnonymousAllowed())
{
return CreateAnonymousAuthenticatonResult();
}
if (!Request.Headers.TryGetValue("Authorization", out var auth))
return Task.FromResult(AuthenticateResult.NoResult());
string username = null;
string password = null;
try
{
var authHeader = AuthenticationHeaderValue.Parse(auth);
var credentialBytes = Convert.FromBase64String(authHeader.Parameter);
var credentials = Encoding.UTF8.GetString(credentialBytes).Split([':'], 2);
username = credentials[0];
password = credentials[1];
}
catch
{
return Task.FromResult(AuthenticateResult.Fail("Invalid Authorization Header"));
}
if (!ValidateCredentials(username, password))
return Task.FromResult(AuthenticateResult.Fail("Invalid Username or Password"));
return CreateUserAuthenticatonResult(username);
}
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
Response.Headers.WWWAuthenticate = "Basic realm=\"NuGet Server\"";
await base.HandleChallengeAsync(properties);
}
private Task<AuthenticateResult> CreateAnonymousAuthenticatonResult()
{
Claim[] claims = [new Claim(ClaimTypes.Anonymous, string.Empty)];
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
private Task<AuthenticateResult> CreateUserAuthenticatonResult(string username)
{
Claim[] claims = [new Claim(ClaimTypes.Name, username)];
var identity = new ClaimsIdentity(claims, Scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
private bool IsAnonymousAllowed()
{
return bagetterOptions.Value.Authentication is null ||
bagetterOptions.Value.Authentication.Credentials is null ||
bagetterOptions.Value.Authentication.Credentials.Length == 0 ||
bagetterOptions.Value.Authentication.Credentials.All(a => string.IsNullOrWhiteSpace(a.Username) && string.IsNullOrWhiteSpace(a.Password));
}
private bool ValidateCredentials(string username, string password)
{
return bagetterOptions.Value.Authentication.Credentials.Any(a =>
a.Username.Equals(username, StringComparison.OrdinalIgnoreCase) &&
FixedTimeEquals(a.Password, password));
}
private static bool FixedTimeEquals(string expected, string actual)
{
if (expected == null || actual == null)
return false;
var expectedBytes = Encoding.UTF8.GetBytes(expected);
var actualBytes = Encoding.UTF8.GetBytes(actual);
return CryptographicOperations.FixedTimeEquals(expectedBytes, actualBytes);
}
}