public void Check(AddDocumentParameter parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (string.IsNullOrWhiteSpace(parameter.Id))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, string.Format(ErrorDescriptions.ParameterIsMissing, "id"));
            }

            if (string.IsNullOrWhiteSpace(parameter.DisplayName))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, string.Format(ErrorDescriptions.ParameterIsMissing, "display_name"));
            }

            if (string.IsNullOrWhiteSpace(parameter.Subject))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InvalidRequest, ErrorDescriptions.SubjectIsMissing);
            }
        }
        public async Task <bool> Execute(string openidWellKnownConfiguration, AddDocumentParameter document, AuthenticateParameter authenticateParameter)
        {
            if (string.IsNullOrWhiteSpace(openidWellKnownConfiguration))
            {
                throw new ArgumentNullException(nameof(openidWellKnownConfiguration));
            }

            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (authenticateParameter == null)
            {
                throw new ArgumentNullException(nameof(authenticateParameter));
            }

            _addDocumentParameterValidator.Check(document);
            var officeDocument = await _officeDocumentRepository.Get(document.Id);

            if (officeDocument != null)
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.OfficeDocumentExists);
            }

            var grantedToken = await _accessTokenStore.GetToken(authenticateParameter.WellKnownConfigurationUrl, authenticateParameter.ClientId, authenticateParameter.ClientSecret, new[] { "uma_protection" });

            if (grantedToken == null || string.IsNullOrWhiteSpace(grantedToken.AccessToken))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.CannotRetrieveAccessToken);
            }

            var resource = await _identityServerUmaClientFactory.GetResourceSetClient().AddByResolution(new PostResourceSet
            {
                Name   = $"officedocument_{document.Id}",
                Scopes = Constants.DEFAULT_SCOPES.ToList()
            }, authenticateParameter.WellKnownConfigurationUrl, grantedToken.AccessToken);

            if (resource.ContainsError)
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.CannotAddUmaResource);
            }

            var policy = await _identityServerUmaClientFactory.GetPolicyClient().AddByResolution(new PostPolicy
            {
                ResourceSetIds = new List <string> {
                    resource.Content.Id
                },
                Rules = new List <PostPolicyRule>
                {
                    new PostPolicyRule
                    {
                        Claims = new List <PostClaim>
                        {
                            new PostClaim
                            {
                                Type  = "sub",
                                Value = document.Subject
                            }
                        },
                        Scopes         = Constants.DEFAULT_SCOPES.ToList(),
                        OpenIdProvider = openidWellKnownConfiguration
                    }
                }
            }, authenticateParameter.WellKnownConfigurationUrl, grantedToken.AccessToken);

            if (policy.ContainsError)
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.CannotAddUmaPolicy);
            }

            officeDocument = new OfficeDocumentAggregate
            {
                Id            = document.Id,
                Subject       = document.Subject,
                DisplayName   = document.DisplayName,
                UmaResourceId = resource.Content.Id,
                UmaPolicyId   = policy.Content.PolicyId,
            };
            if (!await _officeDocumentRepository.Add(officeDocument))
            {
                throw new BaseDocumentManagementApiException(ErrorCodes.InternalError, ErrorDescriptions.CannotAddOfficeDocument);
            }

            return(true);
        }
 public Task <bool> Add(string openidWellKnownConfiguration, AddDocumentParameter document, AuthenticateParameter authenticateParameter)
 {
     return(_addOfficeDocumentAction.Execute(openidWellKnownConfiguration, document, authenticateParameter));
 }