2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Net.Http.Json;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models.Api.Request.Accounts;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Identity;
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
namespace Bit.IntegrationTestCommon.Factories;
|
|
|
|
|
|
|
|
|
|
public class IdentityApplicationFactory : WebApplicationFactoryBase<Startup>
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{
|
2022-08-29 20:53:16 +02:00
|
|
|
|
public const string DefaultDeviceIdentifier = "92b9d953-b9b6-4eaf-9d3e-11d57144dfeb";
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
public async Task<HttpContext> RegisterAsync(RegisterRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
return await Server.PostAsync("/accounts/register", JsonContent.Create(model));
|
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
public async Task<(string Token, string RefreshToken)> TokenFromPasswordAsync(string username,
|
|
|
|
|
string password,
|
|
|
|
|
string deviceIdentifier = DefaultDeviceIdentifier,
|
|
|
|
|
string clientId = "web",
|
|
|
|
|
DeviceType deviceType = DeviceType.FirefoxBrowser,
|
|
|
|
|
string deviceName = "firefox")
|
|
|
|
|
{
|
|
|
|
|
var context = await Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{
|
2022-08-29 20:53:16 +02:00
|
|
|
|
{ "scope", "api offline_access" },
|
|
|
|
|
{ "client_id", clientId },
|
|
|
|
|
{ "deviceType", ((int)deviceType).ToString() },
|
|
|
|
|
{ "deviceIdentifier", deviceIdentifier },
|
|
|
|
|
{ "deviceName", deviceName },
|
|
|
|
|
{ "grant_type", "password" },
|
|
|
|
|
{ "username", username },
|
|
|
|
|
{ "password", password },
|
|
|
|
|
}), context => context.Request.Headers.Add("Auth-Email", CoreHelpers.Base64UrlEncodeString(username)));
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-08-29 20:53:16 +02:00
|
|
|
|
return (root.GetProperty("access_token").GetString(), root.GetProperty("refresh_token").GetString());
|
2022-05-20 21:24:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|