コード例 #1
0
        public async Task <ApiKeyResponseModel> ApiKey(string id, [FromBody] ApiKeyRequestModel model)
        {
            var orgIdGuid = new Guid(id);

            if (!_currentContext.OrganizationOwner(orgIdGuid))
            {
                throw new NotFoundException();
            }

            var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);

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

            var user = await _userService.GetUserByPrincipalAsync(User);

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

            if (!await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
            {
                await Task.Delay(2000);

                throw new BadRequestException("MasterPasswordHash", "Invalid password.");
            }
            else
            {
                var response = new ApiKeyResponseModel(organization);
                return(response);
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
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);
            }
        }
コード例 #4
0
        public async Task <ApiKeyResponseModel> ApiKey([FromBody] ApiKeyRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

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

            if (!await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
            {
                await Task.Delay(2000);

                throw new BadRequestException("MasterPasswordHash", "Invalid password.");
            }
            else
            {
                var response = new ApiKeyResponseModel(user);
                return(response);
            }
        }
コード例 #5
0
        public async Task <ApiKeyResponseModel> RotateApiKey([FromBody] SecretVerificationRequestModel model)
        {
            var user = await _userService.GetUserByPrincipalAsync(User);

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

            if (!await _userService.VerifySecretAsync(user, model.Secret))
            {
                await Task.Delay(2000);

                throw new BadRequestException(string.Empty, "User verification failed.");
            }

            await _userService.RotateApiKeyAsync(user);

            var response = new ApiKeyResponseModel(user);

            return(response);
        }