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;
|
2024-09-06 17:05:25 +02:00
|
|
|
|
using HandlebarsDotNet;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
namespace Bit.IntegrationTestCommon.Factories;
|
|
|
|
|
|
|
|
|
|
public class IdentityApplicationFactory : WebApplicationFactoryBase<Startup>
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public const string DefaultDeviceIdentifier = "92b9d953-b9b6-4eaf-9d3e-11d57144dfeb";
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
public async Task<HttpContext> RegisterAsync(RegisterRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 23:24:42 +02:00
|
|
|
|
public async Task<HttpContext> PostRegisterVerificationEmailClicked(RegisterVerificationEmailClickedRequestModel model)
|
|
|
|
|
{
|
|
|
|
|
return await Server.PostAsync("/accounts/register/verification-email-clicked", JsonContent.Create(model));
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 17:05:25 +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 ContextFromPasswordAsync(
|
|
|
|
|
username, password, deviceIdentifier, clientId, deviceType, deviceName);
|
|
|
|
|
|
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
|
|
|
|
|
return (root.GetProperty("access_token").GetString(), root.GetProperty("refresh_token").GetString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HttpContext> ContextFromPasswordAsync(
|
|
|
|
|
string username,
|
2022-08-29 22:06:55 +02:00
|
|
|
|
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 22:06:55 +02:00
|
|
|
|
{ "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
|
|
|
|
|
2024-09-06 17:05:25 +02:00
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<HttpContext> ContextFromPasswordWithTwoFactorAsync(
|
|
|
|
|
string username,
|
|
|
|
|
string password,
|
|
|
|
|
string deviceIdentifier = DefaultDeviceIdentifier,
|
|
|
|
|
string clientId = "web",
|
|
|
|
|
DeviceType deviceType = DeviceType.FirefoxBrowser,
|
|
|
|
|
string deviceName = "firefox",
|
|
|
|
|
string twoFactorProviderType = "Email",
|
|
|
|
|
string twoFactorToken = "two-factor-token")
|
|
|
|
|
{
|
|
|
|
|
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 },
|
|
|
|
|
{ "TwoFactorToken", twoFactorToken },
|
|
|
|
|
{ "TwoFactorProvider", twoFactorProviderType },
|
|
|
|
|
{ "TwoFactorRemember", "1" },
|
|
|
|
|
}), context => context.Request.Headers.Append("Auth-Email", CoreHelpers.Base64UrlEncodeString(username)));
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> TokenFromAccessTokenAsync(Guid clientId, string clientSecret,
|
|
|
|
|
DeviceType deviceType = DeviceType.SDK)
|
|
|
|
|
{
|
|
|
|
|
var context = await ContextFromAccessTokenAsync(clientId, clientSecret, deviceType);
|
|
|
|
|
|
2022-08-29 22:06:55 +02:00
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2024-09-06 17:05:25 +02:00
|
|
|
|
return root.GetProperty("access_token").GetString();
|
2022-05-20 21:24:59 +02:00
|
|
|
|
}
|
2024-03-29 17:00:30 +01:00
|
|
|
|
|
2024-09-06 17:05:25 +02:00
|
|
|
|
public async Task<HttpContext> ContextFromAccessTokenAsync(Guid clientId, string clientSecret,
|
2024-03-29 17:00:30 +01:00
|
|
|
|
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() }
|
|
|
|
|
}));
|
|
|
|
|
|
2024-09-06 17:05:25 +02:00
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> TokenFromOrganizationApiKeyAsync(string clientId, string clientSecret,
|
|
|
|
|
DeviceType deviceType = DeviceType.FirefoxBrowser)
|
|
|
|
|
{
|
|
|
|
|
var context = await ContextFromOrganizationApiKeyAsync(clientId, clientSecret, deviceType);
|
|
|
|
|
|
2024-03-29 17:00:30 +01:00
|
|
|
|
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
|
|
|
|
|
2024-09-06 17:05:25 +02:00
|
|
|
|
public async Task<HttpContext> ContextFromOrganizationApiKeyAsync(string clientId, string clientSecret,
|
2024-05-31 01:23:31 +02:00
|
|
|
|
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() }
|
|
|
|
|
}));
|
2024-09-06 17:05:25 +02:00
|
|
|
|
return context;
|
2024-05-31 01:23:31 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
}
|