-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuth.cs
56 lines (45 loc) · 2.16 KB
/
Auth.cs
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
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
namespace StickerManBot;
public class StickerManBotAuthenticationSchemeOptions : AuthenticationSchemeOptions
{
private string _token = null!;
[StringLength(44, MinimumLength = 44)]
[UsedImplicitly]
public string Token
{
get => _token;
set
{
TokenBytes = Encoding.UTF8.GetBytes(value);
_token = value;
}
}
public byte[] TokenBytes { get; private set; } = null!;
}
public class StickerManBotAuthenticationHandler : AuthenticationHandler<StickerManBotAuthenticationSchemeOptions>
{
private readonly IOptionsMonitor<StickerManBotAuthenticationSchemeOptions> _options;
private static readonly AuthenticationTicket SuccessAuthenticationTicket = new AuthenticationTicket(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>(), "auth")), "StickerManBotAuthentication");
public StickerManBotAuthenticationHandler(IOptionsMonitor<StickerManBotAuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder)
{
_options = options;
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!Request.Headers.TryGetValue("X-Telegram-Bot-Api-Secret-Token", out var token))
return Task.FromResult(AuthenticateResult.Fail("X-Telegram-Bot-Api-Secret-Token header absent"));
if (string.IsNullOrEmpty(token))
return Task.FromResult(AuthenticateResult.Fail("X-Telegram-Bot-Api-Secret-Token header present but null"));
if (!CryptographicOperations.FixedTimeEquals(new ReadOnlySpan<byte>(_options.CurrentValue.TokenBytes), new ReadOnlySpan<byte>(Encoding.UTF8.GetBytes(token!))))
return Task.FromResult(AuthenticateResult.Fail("X-Telegram-Bot-Api-Secret-Token header present but mismatch"));
return Task.FromResult(AuthenticateResult.Success(SuccessAuthenticationTicket));
}
}