-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPasskeyHelpers.cs
More file actions
86 lines (76 loc) · 2.5 KB
/
Copy pathPasskeyHelpers.cs
File metadata and controls
86 lines (76 loc) · 2.5 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
using System.Net;
using System.Text.Json;
using Npgsql;
using NpgsqlRest;
namespace NpgsqlRestClient.Fido2;
internal static class PasskeyHelpers
{
public static async Task WriteErrorResponseAsync(
HttpContext context,
HttpStatusCode statusCode,
string error,
string? errorDescription)
{
await Results.Problem(
type: error,
statusCode: (int)statusCode,
title: errorDescription).ExecuteAsync(context);
}
public static async Task WriteSuccessResponseAsync(
HttpContext context,
string? credentialId)
{
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/json";
await using var writer = new Utf8JsonWriter(context.Response.Body);
writer.WriteStartObject();
writer.WriteBoolean("success", true);
if (credentialId != null)
{
writer.WriteString("credentialId", credentialId);
}
writer.WriteEndObject();
await writer.FlushAsync(context.RequestAborted);
}
public static string GetOriginFromRequest(HttpRequest request)
{
var scheme = request.Scheme;
var host = request.Host.Host;
var port = request.Host.Port;
// Standard ports don't need to be included
if ((scheme == "https" && port == 443) ||
(scheme == "http" && port == 80) ||
port == null)
{
return $"{scheme}://{host}";
}
return $"{scheme}://{host}:{port}";
}
public static async Task<NpgsqlConnection> OpenConnectionAsync(
PasskeyEndpointContext ctx,
CancellationToken cancellationToken)
{
return await ConnectionHelper.OpenConnectionAsync(
ctx.Options,
ctx.Config.ConnectionName,
ctx.LoggingMode,
cancellationToken,
ctx.Logger);
}
public static async Task ExecuteTransactionCommandAsync(
NpgsqlConnection connection,
string command,
CancellationToken cancellationToken = default)
{
await using var cmd = connection.CreateCommand();
cmd.CommandText = command;
await cmd.ExecuteNonQueryAsync(cancellationToken);
}
public static async Task<ReadOnlyMemory<byte>> ReadRequestBodyAsync(
HttpContext context)
{
using var ms = new MemoryStream();
await context.Request.Body.CopyToAsync(ms, context.RequestAborted);
return ms.ToArray();
}
}