-
Notifications
You must be signed in to change notification settings - Fork 8
/
Index.razor.cs
102 lines (94 loc) · 3.19 KB
/
Index.razor.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
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
using KristofferStrube.Blazor.CredentialManagement;
using KristofferStrube.Blazor.WebIDL.Exceptions;
using Microsoft.AspNetCore.Components;
using System.Security.Cryptography;
using System.Text;
namespace KristofferStrube.Blazor.WebAuthentication.WasmExample.Pages;
public partial class Index : ComponentBase
{
private CredentialsContainer container = default!;
private PublicKeyCredential? credential;
private PublicKeyCredential? validatedCredential;
private bool? successfulGettingCredential = null;
private string? type;
private string? id;
private string? errorMessage;
protected override async Task OnInitializedAsync()
{
container = await CredentialsService.GetCredentialsAsync();
}
private async Task CreateCredential()
{
byte[] challenge = RandomNumberGenerator.GetBytes(32);
byte[] userId = Encoding.ASCII.GetBytes("bob");
CredentialCreationOptions options = new()
{
PublicKey = new PublicKeyCredentialCreationOptions()
{
Rp = new PublicKeyCredentialRpEntity
{
Name = "Kristoffer Strube Consulting"
},
User = new PublicKeyCredentialUserEntity()
{
Name = "bob",
Id = userId,
DisplayName = "Bob"
},
Challenge = challenge,
PubKeyCredParams =
[
new PublicKeyCredentialParameters()
{
Type = PublicKeyCredentialType.PublicKey,
Alg = -257
}
],
Timeout = 360000,
}
};
try
{
credential = await container.CreateAsync(options) is { } c ? new PublicKeyCredential(c) : null;
}
catch (DOMException exception)
{
errorMessage = $"{exception.Name}: \"{exception.Message}\"";
credential = null;
}
if (credential is not null)
{
type = await credential.GetTypeAsync();
id = await credential.GetIdAsync();
}
}
private async Task GetCredential()
{
byte[] challenge = RandomNumberGenerator.GetBytes(32);
CredentialRequestOptions options = new()
{
PublicKey = new PublicKeyCredentialRequestOptions()
{
Challenge = challenge,
Timeout = 360000,
AllowCredentials = [
new PublicKeyCredentialDescriptor()
{
Type = PublicKeyCredentialType.PublicKey,
Id = await credential!.GetRawIdAsync()
}
]
}
};
try
{
validatedCredential = await container.GetAsync(options) is { } c ? new PublicKeyCredential(c) : null;
}
catch (DOMException exception)
{
errorMessage = $"{exception.Name}: \"{exception.Message}\"";
validatedCredential = null;
}
successfulGettingCredential = validatedCredential is not null;
}
}