2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Text.Json;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Bit.Core.Enums;
|
|
|
|
|
using Bit.Core.Models.Api.Request.Accounts;
|
|
|
|
|
using Bit.Core.Repositories;
|
2022-09-27 18:30:37 +02:00
|
|
|
|
using Bit.Identity.IdentityServer;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Bit.IntegrationTestCommon.Factories;
|
|
|
|
|
using Bit.Test.Common.AutoFixture.Attributes;
|
|
|
|
|
using Bit.Test.Common.Helpers;
|
2022-12-20 14:08:29 +01:00
|
|
|
|
using Microsoft.AspNetCore.TestHost;
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Identity.IntegrationTest.Endpoints;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public class IdentityServerTests : IClassFixture<IdentityApplicationFactory>
|
|
|
|
|
{
|
|
|
|
|
private const int SecondsInMinute = 60;
|
|
|
|
|
private const int MinutesInHour = 60;
|
|
|
|
|
private const int SecondsInHour = SecondsInMinute * MinutesInHour;
|
|
|
|
|
private readonly IdentityApplicationFactory _factory;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public IdentityServerTests(IdentityApplicationFactory factory)
|
|
|
|
|
{
|
|
|
|
|
_factory = factory;
|
2023-01-17 16:21:36 +01:00
|
|
|
|
ReinitializeDbForTests();
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task WellKnownEndpoint_Success()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.GetAsync("/.well-known/openid-configuration");
|
|
|
|
|
|
|
|
|
|
using var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var endpointRoot = body.RootElement;
|
|
|
|
|
|
|
|
|
|
// WARNING: Edits to this file should NOT just be made to "get the test to work" they should be made when intentional
|
|
|
|
|
// changes were made to this endpoint and proper testing will take place to ensure clients are backwards compatible
|
|
|
|
|
// or loss of functionality is properly noted.
|
|
|
|
|
await using var fs = File.OpenRead("openid-configuration.json");
|
|
|
|
|
using var knownConfiguration = await JsonSerializer.DeserializeAsync<JsonDocument>(fs);
|
|
|
|
|
var knownConfigurationRoot = knownConfiguration.RootElement;
|
|
|
|
|
|
|
|
|
|
AssertHelper.AssertEqualJson(endpointRoot, knownConfigurationRoot);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_Success(string deviceId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var username = "test+tokenpassword@email.com";
|
|
|
|
|
|
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
var context = await PostLoginAsync(_factory.Server, username, deviceId, context => context.SetAuthEmail(username));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
using var body = await AssertDefaultTokenBodyAsync(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
AssertRefreshTokenExists(root);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "ForcePasswordReset", JsonValueKind.False);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "ResetMasterPassword", JsonValueKind.False);
|
|
|
|
|
var kdf = AssertHelper.AssertJsonProperty(root, "Kdf", JsonValueKind.Number).GetInt32();
|
|
|
|
|
Assert.Equal(0, kdf);
|
|
|
|
|
var kdfIterations = AssertHelper.AssertJsonProperty(root, "KdfIterations", JsonValueKind.Number).GetInt32();
|
|
|
|
|
Assert.Equal(5000, kdfIterations);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_NoAuthEmailHeader_Fails(string deviceId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var username = "test+noauthemailheader@email.com";
|
|
|
|
|
|
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
var context = await PostLoginAsync(_factory.Server, username, deviceId, null);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-06-24 16:39:34 +02:00
|
|
|
|
var error = AssertHelper.AssertJsonProperty(root, "error", JsonValueKind.String).GetString();
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal("invalid_grant", error);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "error_description", JsonValueKind.String);
|
2022-06-24 16:39:34 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_InvalidBase64AuthEmailHeader_Fails(string deviceId)
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{
|
|
|
|
|
var username = "test+badauthheader@email.com";
|
|
|
|
|
|
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
var context = await PostLoginAsync(_factory.Server, username, deviceId, context => context.Request.Headers.Add("Auth-Email", "bad_value"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var error = AssertHelper.AssertJsonProperty(root, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_grant", error);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "error_description", JsonValueKind.String);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WrongAuthEmailHeader_Fails(string deviceId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var username = "test+badauthheader@email.com";
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
var context = await PostLoginAsync(_factory.Server, username, deviceId, context => context.SetAuthEmail("bad_value"));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var error = AssertHelper.AssertJsonProperty(root, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_grant", error);
|
|
|
|
|
AssertHelper.AssertJsonProperty(root, "error_description", JsonValueKind.String);
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Custom)]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WithAllUserTypes_WithSsoPolicyDisabled_WithEnforceSsoPolicyForAllUsersTrue_Success(OrganizationUserType organizationUserType, Guid organizationId, string deviceId, int generatedUsername)
|
|
|
|
|
{
|
|
|
|
|
var username = $"{generatedUsername}@example.com";
|
|
|
|
|
|
|
|
|
|
var server = _factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSetting("globalSettings:sso:enforceSsoPolicyForAllUsers", "true");
|
|
|
|
|
}).Server;
|
|
|
|
|
|
|
|
|
|
await server.PostAsync("/accounts/register", JsonContent.Create(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await CreateOrganizationWithSsoPolicyAsync(organizationId, username, organizationUserType, ssoPolicyEnabled: false);
|
|
|
|
|
|
|
|
|
|
var context = await PostLoginAsync(server, username, deviceId, context => context.SetAuthEmail(username));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Custom)]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WithAllUserTypes_WithSsoPolicyDisabled_WithEnforceSsoPolicyForAllUsersFalse_Success(OrganizationUserType organizationUserType, Guid organizationId, string deviceId, int generatedUsername)
|
|
|
|
|
{
|
|
|
|
|
var username = $"{generatedUsername}@example.com";
|
|
|
|
|
|
|
|
|
|
var server = _factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSetting("globalSettings:sso:enforceSsoPolicyForAllUsers", "false");
|
|
|
|
|
}).Server;
|
|
|
|
|
|
|
|
|
|
await server.PostAsync("/accounts/register", JsonContent.Create(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await CreateOrganizationWithSsoPolicyAsync(organizationId, username, organizationUserType, ssoPolicyEnabled: false);
|
|
|
|
|
|
|
|
|
|
var context = await PostLoginAsync(server, username, deviceId, context => context.SetAuthEmail(username));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Custom)]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WithAllUserTypes_WithSsoPolicyEnabled_WithEnforceSsoPolicyForAllUsersTrue_Throw(OrganizationUserType organizationUserType, Guid organizationId, string deviceId, int generatedUsername)
|
|
|
|
|
{
|
|
|
|
|
var username = $"{generatedUsername}@example.com";
|
|
|
|
|
|
|
|
|
|
var server = _factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSetting("globalSettings:sso:enforceSsoPolicyForAllUsers", "true");
|
|
|
|
|
}).Server;
|
|
|
|
|
|
|
|
|
|
await server.PostAsync("/accounts/register", JsonContent.Create(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await CreateOrganizationWithSsoPolicyAsync(organizationId, username, organizationUserType, ssoPolicyEnabled: true);
|
|
|
|
|
|
|
|
|
|
var context = await PostLoginAsync(server, username, deviceId, context => context.SetAuthEmail(username));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
await AssertRequiredSsoAuthenticationResponseAsync(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Owner)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Admin)]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WithOwnerOrAdmin_WithSsoPolicyEnabled_WithEnforceSsoPolicyForAllUsersFalse_Success(OrganizationUserType organizationUserType, Guid organizationId, string deviceId, int generatedUsername)
|
|
|
|
|
{
|
|
|
|
|
var username = $"{generatedUsername}@example.com";
|
|
|
|
|
|
|
|
|
|
var server = _factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSetting("globalSettings:sso:enforceSsoPolicyForAllUsers", "false");
|
|
|
|
|
}).Server;
|
|
|
|
|
|
|
|
|
|
await server.PostAsync("/accounts/register", JsonContent.Create(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await CreateOrganizationWithSsoPolicyAsync(organizationId, username, organizationUserType, ssoPolicyEnabled: true);
|
|
|
|
|
|
|
|
|
|
var context = await PostLoginAsync(server, username, deviceId, context => context.SetAuthEmail(username));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory]
|
|
|
|
|
[BitAutoData(OrganizationUserType.User)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Manager)]
|
|
|
|
|
[BitAutoData(OrganizationUserType.Custom)]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypePassword_WithNonOwnerOrAdmin_WithSsoPolicyEnabled_WithEnforceSsoPolicyForAllUsersFalse_Throws(OrganizationUserType organizationUserType, Guid organizationId, string deviceId, int generatedUsername)
|
|
|
|
|
{
|
|
|
|
|
var username = $"{generatedUsername}@example.com";
|
|
|
|
|
|
|
|
|
|
var server = _factory.WithWebHostBuilder(builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.UseSetting("globalSettings:sso:enforceSsoPolicyForAllUsers", "false");
|
|
|
|
|
}).Server;
|
|
|
|
|
|
|
|
|
|
await server.PostAsync("/accounts/register", JsonContent.Create(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash"
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
await CreateOrganizationWithSsoPolicyAsync(organizationId, username, organizationUserType, ssoPolicyEnabled: true);
|
|
|
|
|
|
|
|
|
|
var context = await PostLoginAsync(server, username, deviceId, context => context.SetAuthEmail(username));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
await AssertRequiredSsoAuthenticationResponseAsync(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeRefreshToken_Success(string deviceId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var username = "test+tokenrefresh@email.com";
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var (_, refreshToken) = await _factory.TokenFromPasswordAsync(username, "master_password_hash", deviceId);
|
|
|
|
|
|
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "refresh_token" },
|
|
|
|
|
{ "client_id", "web" },
|
|
|
|
|
{ "refresh_token", refreshToken },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
using var body = await AssertDefaultTokenBodyAsync(context);
|
|
|
|
|
AssertRefreshTokenExists(body.RootElement);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_Success(string deviceId)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var username = "test+tokenclientcredentials@email.com";
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var database = _factory.GetDatabaseContext();
|
|
|
|
|
var user = await database.Users
|
|
|
|
|
.FirstAsync(u => u.Email == username);
|
|
|
|
|
|
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", $"user.{user.Id}" },
|
|
|
|
|
{ "client_secret", user.ApiKey },
|
|
|
|
|
{ "scope", "api" },
|
|
|
|
|
{ "DeviceIdentifier", deviceId },
|
|
|
|
|
{ "DeviceType", DeviceTypeAsString(DeviceType.FirefoxBrowser) },
|
|
|
|
|
{ "DeviceName", "firefox" },
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
await AssertDefaultTokenBodyAsync(context, "api");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, BitAutoData]
|
2022-12-20 14:08:29 +01:00
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsOrganization_Success(Bit.Core.Entities.Organization organization, Bit.Core.Entities.OrganizationApiKey organizationApiKey)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var orgRepo = _factory.Services.GetRequiredService<IOrganizationRepository>();
|
|
|
|
|
organization.Enabled = true;
|
|
|
|
|
organization.UseApi = true;
|
|
|
|
|
organization = await orgRepo.CreateAsync(organization);
|
|
|
|
|
organizationApiKey.OrganizationId = organization.Id;
|
|
|
|
|
organizationApiKey.Type = OrganizationApiKeyType.Default;
|
|
|
|
|
|
|
|
|
|
var orgApiKeyRepo = _factory.Services.GetRequiredService<IOrganizationApiKeyRepository>();
|
|
|
|
|
await orgApiKeyRepo.CreateAsync(organizationApiKey);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", $"organization.{organization.Id}" },
|
|
|
|
|
{ "client_secret", organizationApiKey.ApiKey },
|
|
|
|
|
{ "scope", "api.organization" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
await AssertDefaultTokenBodyAsync(context, "api.organization");
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
[Fact]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsOrganization_BadOrgId_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", "organization.bad_guid_zz&" },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.organization" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This test currently does not test any code that is not covered by other tests but
|
2022-09-27 18:30:37 +02:00
|
|
|
|
/// it shows that we probably have some dead code in <see cref="ClientStore"/>
|
2022-05-20 21:24:59 +02:00
|
|
|
|
/// for installation, organization, and user they split on a <c>'.'</c> but have already checked that at least one
|
|
|
|
|
/// <c>'.'</c> exists in the <c>client_id</c> by checking it with <see cref="string.StartsWith(string)"/>
|
|
|
|
|
/// I believe that idParts.Length > 1 will ALWAYS return true
|
|
|
|
|
/// </summary>
|
2022-08-29 21:53:48 +02:00
|
|
|
|
[Fact]
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsOrganization_NoIdPart_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", "organization." },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.organization" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsOrganization_OrgDoesNotExist_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", $"organization.{Guid.NewGuid()}" },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.organization" },
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}));
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
[Theory, BitAutoData]
|
2022-12-20 14:08:29 +01:00
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsInstallation_InstallationExists_Succeeds(Bit.Core.Entities.Installation installation)
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{
|
|
|
|
|
var installationRepo = _factory.Services.GetRequiredService<IInstallationRepository>();
|
|
|
|
|
installation = await installationRepo.CreateAsync(installation);
|
|
|
|
|
|
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", $"installation.{installation.Id}" },
|
|
|
|
|
{ "client_secret", installation.Key },
|
|
|
|
|
{ "scope", "api.push" },
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}));
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(StatusCodes.Status200OK, context.Response.StatusCode);
|
|
|
|
|
await AssertDefaultTokenBodyAsync(context, "api.push", 24 * SecondsInHour);
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2022-05-20 21:24:59 +02:00
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsInstallation_InstallationDoesNotExist_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", $"installation.{Guid.NewGuid()}" },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.push" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsInstallation_BadInsallationId_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", "organization.bad_guid_zz&" },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.organization" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc cref="TokenEndpoint_GrantTypeClientCredentials_AsOrganization_NoIdPart_Fails"/>
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task TokenEndpoint_GrantTypeClientCredentials_AsInstallation_NoIdPart_Fails()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-05-20 21:24:59 +02:00
|
|
|
|
var context = await _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "grant_type", "client_credentials" },
|
|
|
|
|
{ "client_id", "installation." },
|
|
|
|
|
{ "client_secret", "something" },
|
|
|
|
|
{ "scope", "api.push" },
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
Assert.Equal(StatusCodes.Status400BadRequest, context.Response.StatusCode);
|
|
|
|
|
|
|
|
|
|
var errorBody = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(errorBody.RootElement, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_client", error);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
[Theory, BitAutoData]
|
|
|
|
|
public async Task TokenEndpoint_ToQuickInOneSecond_BlockRequest(string deviceId)
|
2022-06-24 16:39:34 +02:00
|
|
|
|
{
|
|
|
|
|
const int AmountInOneSecondAllowed = 5;
|
|
|
|
|
|
|
|
|
|
// The rule we are testing is 10 requests in 1 second
|
|
|
|
|
var username = "test+ratelimiting@email.com";
|
|
|
|
|
|
|
|
|
|
await _factory.RegisterAsync(new RegisterRequestModel
|
|
|
|
|
{
|
|
|
|
|
Email = username,
|
|
|
|
|
MasterPasswordHash = "master_password_hash",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var database = _factory.GetDatabaseContext();
|
|
|
|
|
var user = await database.Users
|
|
|
|
|
.FirstAsync(u => u.Email == username);
|
|
|
|
|
|
|
|
|
|
var tasks = new Task<HttpContext>[AmountInOneSecondAllowed + 1];
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < AmountInOneSecondAllowed + 1; i++)
|
|
|
|
|
{
|
|
|
|
|
// Queue all the amount of calls allowed plus 1
|
|
|
|
|
tasks[i] = MakeRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 18:39:43 +02:00
|
|
|
|
var responses = (await Task.WhenAll(tasks)).ToList();
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
2022-07-05 18:39:43 +02:00
|
|
|
|
Assert.Equal(5, responses.Count(c => c.Response.StatusCode == StatusCodes.Status200OK));
|
|
|
|
|
Assert.Equal(1, responses.Count(c => c.Response.StatusCode == StatusCodes.Status429TooManyRequests));
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
|
|
|
|
Task<HttpContext> MakeRequest()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-06-24 16:39:34 +02:00
|
|
|
|
return _factory.Server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "scope", "api offline_access" },
|
|
|
|
|
{ "client_id", "web" },
|
|
|
|
|
{ "deviceType", DeviceTypeAsString(DeviceType.FirefoxBrowser) },
|
|
|
|
|
{ "deviceIdentifier", deviceId },
|
|
|
|
|
{ "deviceName", "firefox" },
|
|
|
|
|
{ "grant_type", "password" },
|
|
|
|
|
{ "username", username },
|
|
|
|
|
{ "password", "master_password_hash" },
|
|
|
|
|
}), context => context.SetAuthEmail(username).SetIp("1.1.1.2"));
|
|
|
|
|
}
|
2022-08-29 22:06:55 +02:00
|
|
|
|
}
|
2022-06-24 16:39:34 +02:00
|
|
|
|
|
2022-12-20 14:08:29 +01:00
|
|
|
|
private async Task<HttpContext> PostLoginAsync(TestServer server, string username, string deviceId, Action<HttpContext> extraConfiguration)
|
|
|
|
|
{
|
|
|
|
|
return await server.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary<string, string>
|
|
|
|
|
{
|
|
|
|
|
{ "scope", "api offline_access" },
|
|
|
|
|
{ "client_id", "web" },
|
|
|
|
|
{ "deviceType", DeviceTypeAsString(DeviceType.FirefoxBrowser) },
|
|
|
|
|
{ "deviceIdentifier", deviceId },
|
|
|
|
|
{ "deviceName", "firefox" },
|
|
|
|
|
{ "grant_type", "password" },
|
|
|
|
|
{ "username", username },
|
|
|
|
|
{ "password", "master_password_hash" },
|
|
|
|
|
}), extraConfiguration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task CreateOrganizationWithSsoPolicyAsync(Guid organizationId, string username, OrganizationUserType organizationUserType, bool ssoPolicyEnabled)
|
|
|
|
|
{
|
|
|
|
|
var userRepository = _factory.Services.GetService<IUserRepository>();
|
|
|
|
|
var organizationRepository = _factory.Services.GetService<IOrganizationRepository>();
|
|
|
|
|
var organizationUserRepository = _factory.Services.GetService<IOrganizationUserRepository>();
|
|
|
|
|
var policyRepository = _factory.Services.GetService<IPolicyRepository>();
|
|
|
|
|
|
2023-01-17 16:21:36 +01:00
|
|
|
|
var organization = new Bit.Core.Entities.Organization { Id = organizationId, Enabled = true, UseSso = ssoPolicyEnabled };
|
|
|
|
|
await organizationRepository.CreateAsync(organization);
|
2022-12-20 14:08:29 +01:00
|
|
|
|
|
|
|
|
|
var user = await userRepository.GetByEmailAsync(username);
|
2023-01-17 16:21:36 +01:00
|
|
|
|
var organizationUser = new Bit.Core.Entities.OrganizationUser
|
2022-12-20 14:08:29 +01:00
|
|
|
|
{
|
2023-01-17 16:21:36 +01:00
|
|
|
|
OrganizationId = organization.Id,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
Status = OrganizationUserStatusType.Confirmed,
|
|
|
|
|
Type = organizationUserType
|
|
|
|
|
};
|
|
|
|
|
await organizationUserRepository.CreateAsync(organizationUser);
|
2022-12-20 14:08:29 +01:00
|
|
|
|
|
2023-01-17 16:21:36 +01:00
|
|
|
|
var ssoPolicy = new Bit.Core.Entities.Policy { OrganizationId = organization.Id, Type = PolicyType.RequireSso, Enabled = ssoPolicyEnabled };
|
|
|
|
|
await policyRepository.CreateAsync(ssoPolicy);
|
2022-12-20 14:08:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
private static string DeviceTypeAsString(DeviceType deviceType)
|
|
|
|
|
{
|
|
|
|
|
return ((int)deviceType).ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<JsonDocument> AssertDefaultTokenBodyAsync(HttpContext httpContext, string expectedScope = "api offline_access", int expectedExpiresIn = SecondsInHour * 1)
|
|
|
|
|
{
|
|
|
|
|
var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(httpContext);
|
|
|
|
|
var root = body.RootElement;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-05-20 21:24:59 +02:00
|
|
|
|
Assert.Equal(JsonValueKind.Object, root.ValueKind);
|
|
|
|
|
AssertAccessTokenExists(root);
|
|
|
|
|
AssertExpiresIn(root, expectedExpiresIn);
|
|
|
|
|
AssertTokenType(root);
|
|
|
|
|
AssertScope(root, expectedScope);
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AssertTokenType(JsonElement tokenResponse)
|
|
|
|
|
{
|
|
|
|
|
var tokenTypeProperty = AssertHelper.AssertJsonProperty(tokenResponse, "token_type", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("Bearer", tokenTypeProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int AssertExpiresIn(JsonElement tokenResponse, int expectedExpiresIn = 3600)
|
|
|
|
|
{
|
|
|
|
|
var expiresIn = AssertHelper.AssertJsonProperty(tokenResponse, "expires_in", JsonValueKind.Number).GetInt32();
|
|
|
|
|
Assert.Equal(expectedExpiresIn, expiresIn);
|
|
|
|
|
return expiresIn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string AssertAccessTokenExists(JsonElement tokenResponse)
|
|
|
|
|
{
|
|
|
|
|
return AssertHelper.AssertJsonProperty(tokenResponse, "access_token", JsonValueKind.String).GetString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string AssertRefreshTokenExists(JsonElement tokenResponse)
|
|
|
|
|
{
|
|
|
|
|
return AssertHelper.AssertJsonProperty(tokenResponse, "refresh_token", JsonValueKind.String).GetString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string AssertScopeExists(JsonElement tokenResponse)
|
|
|
|
|
{
|
|
|
|
|
return AssertHelper.AssertJsonProperty(tokenResponse, "scope", JsonValueKind.String).GetString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AssertScope(JsonElement tokenResponse, string expectedScope)
|
|
|
|
|
{
|
|
|
|
|
var actualScope = AssertScopeExists(tokenResponse);
|
|
|
|
|
Assert.Equal(expectedScope, actualScope);
|
|
|
|
|
}
|
2022-12-20 14:08:29 +01:00
|
|
|
|
|
|
|
|
|
private static async Task AssertRequiredSsoAuthenticationResponseAsync(HttpContext context)
|
|
|
|
|
{
|
|
|
|
|
var body = await AssertHelper.AssertResponseTypeIs<JsonDocument>(context);
|
|
|
|
|
var root = body.RootElement;
|
|
|
|
|
|
|
|
|
|
var error = AssertHelper.AssertJsonProperty(root, "error", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.Equal("invalid_grant", error);
|
|
|
|
|
var errorDescription = AssertHelper.AssertJsonProperty(root, "error_description", JsonValueKind.String).GetString();
|
|
|
|
|
Assert.StartsWith("sso authentication", errorDescription.ToLowerInvariant());
|
|
|
|
|
}
|
2023-01-17 16:21:36 +01:00
|
|
|
|
|
|
|
|
|
private void ReinitializeDbForTests()
|
|
|
|
|
{
|
|
|
|
|
var databaseContext = _factory.GetDatabaseContext();
|
|
|
|
|
databaseContext.Policies.RemoveRange(databaseContext.Policies);
|
|
|
|
|
databaseContext.OrganizationUsers.RemoveRange(databaseContext.OrganizationUsers);
|
|
|
|
|
databaseContext.Organizations.RemoveRange(databaseContext.Organizations);
|
|
|
|
|
databaseContext.Users.RemoveRange(databaseContext.Users);
|
|
|
|
|
databaseContext.SaveChanges();
|
|
|
|
|
}
|
2022-05-20 21:24:59 +02:00
|
|
|
|
}
|