-
Notifications
You must be signed in to change notification settings - Fork 28
/
UserHelpers.cs
70 lines (60 loc) · 3.48 KB
/
UserHelpers.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
using System.Net.Http.Json;
using Bogus;
using Fido2NetLib;
using OpenQA.Selenium;
using Passwordless.Api.Endpoints;
using Passwordless.Service.Models;
namespace Passwordless.Api.IntegrationTests.Helpers.User;
public static class UserHelpers
{
private static readonly Faker<RegisterToken> TokenGenerator = new Faker<RegisterToken>()
.RuleFor(x => x.UserId, Guid.NewGuid().ToString())
.RuleFor(x => x.DisplayName, x => x.Person.FullName)
.RuleFor(x => x.Username, x => x.Person.Email)
.RuleFor(x => x.Attestation, "None")
.RuleFor(x => x.Discoverable, true)
.RuleFor(x => x.UserVerification, "Preferred")
.RuleFor(x => x.Aliases, x => new HashSet<string> { x.Person.FirstName })
.RuleFor(x => x.AliasHashing, false)
.RuleFor(x => x.ExpiresAt, DateTime.UtcNow.AddDays(1))
.RuleFor(x => x.TokenId, Guid.Empty);
public static async Task<HttpResponseMessage> RegisterNewUser(this HttpClient httpClient, WebDriver driver)
{
if (!httpClient.HasPublicKey()) throw new Exception("ApiKey was not provided. Please add ApiKey to headers.");
if (!httpClient.HasSecretKey()) throw new Exception("ApiSecret was not provided. Please add ApiSecret to headers.");
var tokenRequest = TokenGenerator.Generate();
using var tokenResponse = await httpClient.PostAsJsonAsync("/register/token", tokenRequest);
var registerTokenResponse = await tokenResponse.Content.ReadFromJsonAsync<RegisterEndpoints.RegisterTokenResponse>();
var registrationBeginRequest = new FidoRegistrationBeginDTO
{
Token = registerTokenResponse!.Token,
Origin = PasswordlessApiFactory.OriginUrl,
RPID = PasswordlessApiFactory.RpId
};
var registrationBeginResponse = await httpClient.PostAsJsonAsync("/register/begin", registrationBeginRequest);
var sessionResponse = await registrationBeginResponse.Content.ReadFromJsonAsync<SessionResponse<CredentialCreateOptions>>();
var authenticatorAttestationRawResponse = await driver.CreateCredentialsAsync(sessionResponse!.Data);
return await httpClient.PostAsJsonAsync("/register/complete", new RegistrationCompleteDTO
{
Origin = PasswordlessApiFactory.OriginUrl,
RPID = PasswordlessApiFactory.RpId,
Session = sessionResponse.Session,
Response = authenticatorAttestationRawResponse
});
}
public static async Task<HttpResponseMessage> SignInUser(this HttpClient httpClient, WebDriver driver)
{
if (!httpClient.HasPublicKey()) throw new Exception("ApiKey was not provided. Please add ApiKey to headers.");
if (!httpClient.HasSecretKey()) throw new Exception("ApiSecret was not provided. Please add ApiSecret to headers.");
var signInBeginResponse = await httpClient.PostAsJsonAsync("/signin/begin", new SignInBeginDTO { Origin = PasswordlessApiFactory.OriginUrl, RPID = PasswordlessApiFactory.RpId });
var signInBegin = await signInBeginResponse.Content.ReadFromJsonAsync<SessionResponse<AssertionOptions>>();
var authenticatorAssertionRawResponse = await driver.GetCredentialsAsync(signInBegin!.Data);
return await httpClient.PostAsJsonAsync("/signin/complete", new SignInCompleteDTO
{
Origin = PasswordlessApiFactory.OriginUrl,
RPID = PasswordlessApiFactory.RpId,
Response = authenticatorAssertionRawResponse,
Session = signInBegin.Session
});
}
}