Beispiel #1
0
        public async Task <ApiKeyResponseModel> ApiKey(string id, [FromBody] OrganizationApiKeyRequestModel model)
        {
            var orgIdGuid = new Guid(id);

            if (!await HasApiKeyAccessAsync(orgIdGuid, model.Type))
            {
                throw new NotFoundException();
            }

            var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);

            if (organization == null)
            {
                throw new NotFoundException();
            }

            if (model.Type == OrganizationApiKeyType.BillingSync || model.Type == OrganizationApiKeyType.Scim)
            {
                // Non-enterprise orgs should not be able to create or view an apikey of billing sync/scim key types
                var plan = StaticStore.GetPlan(organization.PlanType);
                if (plan.Product != ProductType.Enterprise)
                {
                    throw new NotFoundException();
                }
            }

            var organizationApiKey = await _getOrganizationApiKeyCommand
                                     .GetOrganizationApiKeyAsync(organization.Id, model.Type);

            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            if (model.Type != OrganizationApiKeyType.Scim &&
                !await _userService.VerifySecretAsync(user, model.Secret))
            {
                await Task.Delay(2000);

                throw new BadRequestException("MasterPasswordHash", "Invalid password.");
            }
            else
            {
                var response = new ApiKeyResponseModel(organizationApiKey);
                return(response);
            }
        }
Beispiel #2
0
        public async Task <ApiKeyResponseModel> RotateApiKey(string id, [FromBody] OrganizationApiKeyRequestModel model)
        {
            var orgIdGuid = new Guid(id);

            if (!await HasApiKeyAccessAsync(orgIdGuid, model.Type))
            {
                throw new NotFoundException();
            }

            var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);

            if (organization == null)
            {
                throw new NotFoundException();
            }

            var organizationApiKey = await _getOrganizationApiKeyCommand
                                     .GetOrganizationApiKeyAsync(organization.Id, model.Type);

            var user = await _userService.GetUserByPrincipalAsync(User);

            if (user == null)
            {
                throw new UnauthorizedAccessException();
            }

            if (model.Type != OrganizationApiKeyType.Scim &&
                !await _userService.VerifySecretAsync(user, model.Secret))
            {
                await Task.Delay(2000);

                throw new BadRequestException("MasterPasswordHash", "Invalid password.");
            }
            else
            {
                await _rotateOrganizationApiKeyCommand.RotateApiKeyAsync(organizationApiKey);

                var response = new ApiKeyResponseModel(organizationApiKey);
                return(response);
            }
        }