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;
|
2023-04-14 19:25:56 +02:00
|
|
|
|
using Bit.Core.Auth.Models.Api.Request.Accounts;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Bit.Identity;
|
2024-06-19 21:11:24 +02:00
|
|
|
|
using Bit.Identity.Models.Request.Accounts;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Bit.Test.Common.Helpers;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
|
|
|
|
namespace Bit.IntegrationTestCommon.Factories;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public class IdentityApplicationFactory : WebApplicationFactoryBase<Startup>
|
|
|
|
|
{
|
|
|
|
|
public const string DefaultDeviceIdentifier = "92b9d953-b9b6-4eaf-9d3e-11d57144dfeb";
|
|
|
|
|
|
|
|
|
|
public async Task<HttpContext> RegisterAsync(RegisterRequestModel model)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
return await Server.PostAsync("/accounts/register", JsonContent.Create(model));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-19 19:54:20 +02:00
|
|
|
|
public async Task<HttpContext> PostRegisterSendEmailVerificationAsync(RegisterSendVerificationEmailRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
return await Server.PostAsync("/accounts/register/send-verification-email", JsonContent.Create(model));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-02 23:03:36 +02:00
|
|
|
|
public async Task<HttpContext> PostRegisterFinishAsync(RegisterFinishRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
return await Server.PostAsync("/accounts/register/finish", JsonContent.Create(model));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:24:59 +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")
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "scope", "api offline_access" },
|
|
|
|
|
{ "client_id", clientId },
|
|
|
|
|
{ "deviceType", ((int)deviceType).ToString() },
|
|
|
|
|
{ "deviceIdentifier", deviceIdentifier },
|
|
|
|
|
{ "deviceName", deviceName },
|
|
|
|
|
{ "grant_type", "password" },
|
|
|
|
|
{ "username", username },
|
|
|
|
|
{ "password", password },
|
2024-02-05 19:03:42 +01:00
|
|
|
|
}), context => context.Request.Headers.Append("Auth-Email", CoreHelpers.Base64UrlEncodeString(username)));
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
|
|
|
|
|
return (root.GetProperty("access_token").GetString(), root.GetProperty("refresh_token").GetString());
|
|
|
|
|
}
|
2024-03-29 17:00:30 +01:00
|
|
|
|
|
|
|
|
|
public async Task<string> TokenFromAccessTokenAsync(Guid clientId, string clientSecret,
|
|
|
|
|
DeviceType deviceType = DeviceType.SDK)
|
|
|
|
|
{
|
|
|
|
|
var context = await Server.PostAsync("/connect/token",
|
|
|
|
|
new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "scope", "api.secrets" },
|
|
|
|
|
{ "client_id", clientId.ToString() },
|
|
|
|
|
{ "client_secret", clientSecret },
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "deviceType", ((int)deviceType).ToString() }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
|
|
|
|
|
return root.GetProperty("access_token").GetString();
|
|
|
|
|
}
|
2024-05-31 01:23:31 +02:00
|
|
|
|
|
|
|
|
|
public async Task<string> TokenFromOrganizationApiKeyAsync(string clientId, string clientSecret,
|
|
|
|
|
DeviceType deviceType = DeviceType.FirefoxBrowser)
|
|
|
|
|
{
|
|
|
|
|
var context = await Server.PostAsync("/connect/token",
|
|
|
|
|
new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "scope", "api.organization" },
|
|
|
|
|
{ "client_id", clientId },
|
|
|
|
|
{ "client_secret", clientSecret },
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "deviceType", ((int)deviceType).ToString() }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
|
|
|
|
|
return root.GetProperty("access_token").GetString();
|
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
}
|