-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathSetup2FA.cshtml.cs
More file actions
155 lines (128 loc) · 4.71 KB
/
Copy pathSetup2FA.cshtml.cs
File metadata and controls
155 lines (128 loc) · 4.71 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.ComponentModel.DataAnnotations;
using System.Text;
using Duende.IdentityModel;
using Duende.UserManagement;
using Duende.UserManagement.Authentication;
using Duende.UserManagement.Authentication.RecoveryCodes;
using Duende.UserManagement.Authentication.Totp;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace UserManagementSample.Pages.Manage;
[Authorize]
public sealed class Setup2FAModel(
IUserAuthenticatorsSelfService authenticatorsSelfService) : PageModel
{
public string SharedKey { get; set; } = string.Empty;
public string AuthenticatorUri { get; set; } = string.Empty;
[BindProperty]
[Required]
[StringLength(7, MinimumLength = 6, ErrorMessage = "Code must be 6 or 7 characters.")]
public string Code { get; set; } = string.Empty;
[TempData]
public string? TotpKeyBase32 { get; set; }
public string? ErrorMessage { get; set; }
public IActionResult OnGet()
{
if (GetUserId() is not { } userId)
{
return RedirectToPage("/Account/Login");
}
var key = PlainBytesTotpKey.New();
TotpKeyBase32 = key.EncodeToBase32();
LoadSharedKeyAndQrCodeUri(key);
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (GetUserId() is not { } userId)
{
return RedirectToPage("/Account/Login");
}
if (!ModelState.IsValid)
{
RestoreKeyFromTempData();
return Page();
}
var keyBase32 = TotpKeyBase32;
if (string.IsNullOrEmpty(keyBase32))
{
ErrorMessage = "Session expired. Please start over.";
return RedirectToPage();
}
var key = PlainBytesTotpKey.DecodeFromBase32(keyBase32);
var cleanCode = Code.Replace(" ", string.Empty).Replace("-", string.Empty);
if (!PlainTextTotp.TryCreate(cleanCode, out var totp))
{
ErrorMessage = "Invalid code format.";
TotpKeyBase32 = keyBase32;
LoadSharedKeyAndQrCodeUri(key);
return Page();
}
var added = await authenticatorsSelfService.TryAddTotpDeviceAsync(
userId,
TotpDeviceName.Default,
key,
totp,
HttpContext.RequestAborted);
if (!added)
{
ErrorMessage = "Verification code is invalid. Please try again.";
TotpKeyBase32 = keyBase32;
LoadSharedKeyAndQrCodeUri(key);
return Page();
}
var recoveryCodes = await authenticatorsSelfService.TryCreateRecoveryCodesAsync(userId, HttpContext.RequestAborted);
if (recoveryCodes is { Count: > 0 })
{
TempData["RecoveryCodes"] = recoveryCodes.Select(FormatRecoveryCode).ToArray();
return RedirectToPage("/Manage/ShowRecoveryCodes");
}
TempData["StatusMessage"] = "Two-factor authentication has been enabled.";
return RedirectToPage("/Manage/Setup2FA");
}
private void LoadSharedKeyAndQrCodeUri(PlainBytesTotpKey key)
{
SharedKey = FormatKeyToHumanReadable(key.EncodeToBase32());
var accountName = User.FindFirst(JwtClaimTypes.Name)?.Value
?? User.FindFirst(JwtClaimTypes.Email)?.Value
?? User.Identity?.Name
?? "user";
AuthenticatorUri = TotpAuthenticatorUri.Generate("User Management Sample", accountName, key);
}
private void RestoreKeyFromTempData()
{
var keyBase32 = TotpKeyBase32;
if (!string.IsNullOrEmpty(keyBase32))
{
LoadSharedKeyAndQrCodeUri(PlainBytesTotpKey.DecodeFromBase32(keyBase32));
}
}
private static string FormatKeyToHumanReadable(string unformattedKey)
{
var result = new StringBuilder();
var currentPosition = 0;
while (currentPosition + 4 < unformattedKey.Length)
{
result.Append(unformattedKey.AsSpan(currentPosition, 4)).Append(' ');
currentPosition += 4;
}
if (currentPosition < unformattedKey.Length)
{
result.Append(unformattedKey.AsSpan(currentPosition));
}
return result.ToString().ToLowerInvariant();
}
private static string FormatRecoveryCode(PlainTextRecoveryCode code) =>
string.Join("-", code.ToTextGroups());
private UserSubjectId? GetUserId()
{
if (User.FindFirst(JwtClaimTypes.Subject)?.Value is not { } sub)
{
return null;
}
return UserSubjectId.Create(sub);
}
}