1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

[SG-841] Refactor GetOrganizationApiKeyCommand (#2436)

* Renamed and split up class to only query for an organization key

* Added a command class to create an organization api key

* Updated service registration and controller to include new changes

* Updated test cases to reflect refactor

* fixed lint issues

* Fixed PR comment
This commit is contained in:
Gbubemi Smith
2022-11-28 17:39:09 -07:00
committed by GitHub
parent 5bcacf785f
commit f74730dd2f
9 changed files with 97 additions and 55 deletions

View File

@ -33,8 +33,9 @@ public class OrganizationsController : Controller
private readonly ICurrentContext _currentContext;
private readonly ISsoConfigRepository _ssoConfigRepository;
private readonly ISsoConfigService _ssoConfigService;
private readonly IGetOrganizationApiKeyCommand _getOrganizationApiKeyCommand;
private readonly IGetOrganizationApiKeyQuery _getOrganizationApiKeyQuery;
private readonly IRotateOrganizationApiKeyCommand _rotateOrganizationApiKeyCommand;
private readonly ICreateOrganizationApiKeyCommand _createOrganizationApiKeyCommand;
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
private readonly GlobalSettings _globalSettings;
@ -48,8 +49,9 @@ public class OrganizationsController : Controller
ICurrentContext currentContext,
ISsoConfigRepository ssoConfigRepository,
ISsoConfigService ssoConfigService,
IGetOrganizationApiKeyCommand getOrganizationApiKeyCommand,
IGetOrganizationApiKeyQuery getOrganizationApiKeyQuery,
IRotateOrganizationApiKeyCommand rotateOrganizationApiKeyCommand,
ICreateOrganizationApiKeyCommand createOrganizationApiKeyCommand,
IOrganizationApiKeyRepository organizationApiKeyRepository,
GlobalSettings globalSettings)
{
@ -62,8 +64,9 @@ public class OrganizationsController : Controller
_currentContext = currentContext;
_ssoConfigRepository = ssoConfigRepository;
_ssoConfigService = ssoConfigService;
_getOrganizationApiKeyCommand = getOrganizationApiKeyCommand;
_getOrganizationApiKeyQuery = getOrganizationApiKeyQuery;
_rotateOrganizationApiKeyCommand = rotateOrganizationApiKeyCommand;
_createOrganizationApiKeyCommand = createOrganizationApiKeyCommand;
_organizationApiKeyRepository = organizationApiKeyRepository;
_globalSettings = globalSettings;
}
@ -514,8 +517,9 @@ public class OrganizationsController : Controller
}
}
var organizationApiKey = await _getOrganizationApiKeyCommand
.GetOrganizationApiKeyAsync(organization.Id, model.Type);
var organizationApiKey = await _getOrganizationApiKeyQuery
.GetOrganizationApiKeyAsync(organization.Id, model.Type) ??
await _createOrganizationApiKeyCommand.CreateAsync(organization.Id, model.Type);
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
@ -565,8 +569,9 @@ public class OrganizationsController : Controller
throw new NotFoundException();
}
var organizationApiKey = await _getOrganizationApiKeyCommand
.GetOrganizationApiKeyAsync(organization.Id, model.Type);
var organizationApiKey = await _getOrganizationApiKeyQuery
.GetOrganizationApiKeyAsync(organization.Id, model.Type) ??
await _createOrganizationApiKeyCommand.CreateAsync(organization.Id, model.Type);
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)

View File

@ -0,0 +1,32 @@
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Utilities;
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys;
public class CreateOrganizationApiKeyCommand : ICreateOrganizationApiKeyCommand
{
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
public CreateOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
{
_organizationApiKeyRepository = organizationApiKeyRepository;
}
public async Task<OrganizationApiKey> CreateAsync(Guid organizationId,
OrganizationApiKeyType organizationApiKeyType)
{
var apiKey = new OrganizationApiKey
{
OrganizationId = organizationId,
Type = organizationApiKeyType,
ApiKey = CoreHelpers.SecureRandomString(30),
RevisionDate = DateTime.UtcNow,
};
await _organizationApiKeyRepository.CreateAsync(apiKey);
return apiKey;
}
}

View File

@ -2,15 +2,14 @@
using Bit.Core.Enums;
using Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Utilities;
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys;
public class GetOrganizationApiKeyCommand : IGetOrganizationApiKeyCommand
public class GetOrganizationApiKeyQuery : IGetOrganizationApiKeyQuery
{
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
public GetOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
public GetOrganizationApiKeyQuery(IOrganizationApiKeyRepository organizationApiKeyRepository)
{
_organizationApiKeyRepository = organizationApiKeyRepository;
}
@ -25,20 +24,6 @@ public class GetOrganizationApiKeyCommand : IGetOrganizationApiKeyCommand
var apiKeys = await _organizationApiKeyRepository
.GetManyByOrganizationIdTypeAsync(organizationId, organizationApiKeyType);
if (apiKeys == null || !apiKeys.Any())
{
var apiKey = new OrganizationApiKey
{
OrganizationId = organizationId,
Type = organizationApiKeyType,
ApiKey = CoreHelpers.SecureRandomString(30),
RevisionDate = DateTime.UtcNow,
};
await _organizationApiKeyRepository.CreateAsync(apiKey);
return apiKey;
}
// NOTE: Currently we only allow one type of api key per organization
return apiKeys.Single();
}

View File

@ -0,0 +1,9 @@
using Bit.Core.Entities;
using Bit.Core.Enums;
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
public interface ICreateOrganizationApiKeyCommand
{
Task<OrganizationApiKey> CreateAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType);
}

View File

@ -3,7 +3,7 @@ using Bit.Core.Enums;
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
public interface IGetOrganizationApiKeyCommand
public interface IGetOrganizationApiKeyQuery
{
Task<OrganizationApiKey> GetOrganizationApiKeyAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType);
}

View File

@ -24,7 +24,7 @@ public static class OrganizationServiceCollectionExtensions
services.AddTokenizers();
services.AddOrganizationConnectionCommands();
services.AddOrganizationSponsorshipCommands(globalSettings);
services.AddOrganizationApiKeyCommands();
services.AddOrganizationApiKeyCommandsQueries();
}
private static void AddOrganizationConnectionCommands(this IServiceCollection services)
@ -59,10 +59,11 @@ public static class OrganizationServiceCollectionExtensions
}
}
private static void AddOrganizationApiKeyCommands(this IServiceCollection services)
private static void AddOrganizationApiKeyCommandsQueries(this IServiceCollection services)
{
services.AddScoped<IGetOrganizationApiKeyCommand, GetOrganizationApiKeyCommand>();
services.AddScoped<IGetOrganizationApiKeyQuery, GetOrganizationApiKeyQuery>();
services.AddScoped<IRotateOrganizationApiKeyCommand, RotateOrganizationApiKeyCommand>();
services.AddScoped<ICreateOrganizationApiKeyCommand, CreateOrganizationApiKeyCommand>();
}
private static void AddTokenizers(this IServiceCollection services)