Ejemplo n.º 1
0
        public Task <CommandMessage> Send(CommandMessage commandMessage)
        {
            Header             authorizationContextHeader = commandMessage.HeaderCollection.GetHeader(CommandMessageHeaderNames.AuthorizationContext);
            Header             routingKeyHeader           = commandMessage.HeaderCollection.GetHeader(CommandMessageHeaderNames.RoutingKey);
            AuthorizationToken authorizationToken         = _serializationService.DeserializeFromString <AuthorizationToken>(authorizationContextHeader.Value);
            bool isAuthorized = authorizationToken.Permissions.Any(p =>
                                                                   p.RoutingKey == routingKeyHeader.Value &&
                                                                   p.CommandName == commandMessage.CommandMessageBody.CommandName
                                                                   );

            commandMessage.HeaderCollection.AddHeader(new Header(CommandMessageHeaderNames.IsAuthorized, isAuthorized.ToString()));
            if (!isAuthorized)
            {
                throw new Exception($"User {authorizationToken.User.Username} is not authorized to execute {commandMessage.CommandMessageBody.CommandName} on {routingKeyHeader.Value}.");
            }
            return(Task.FromResult(commandMessage));
        }
Ejemplo n.º 2
0
        public async Task Send(CommandMessage commandMessage)
        {
            string correlationId = "";

            try {
                correlationId = commandMessage.HeaderCollection.GetHeader(CommandMessageHeaderNames.CorrelationId).Value;
            } catch {
                correlationId = Guid.NewGuid().ToString();
            }
            JsonRpcRequest jsonRpcRequest = new JsonRpcRequest()
            {
                id      = 1,
                jsonrpc = "2.0",
                method  = "ExecuteWithCorrelationAndHeaders",
                Params  = new JsonRpcGatewayExecuteParams()
                {
                    routingKey = commandMessage.HeaderCollection.GetHeader(CommandMessageHeaderNames.RoutingKey).Value,
                    command    = new JsonRpcGatewayCommand()
                    {
                        name = commandMessage.CommandMessageBody.CommandName,
                        data = commandMessage.CommandMessageBody.Command
                    },
                    authorizationToken = commandMessage.HeaderCollection.GetHeader(CommandMessageHeaderNames.AuthorizationToken).Value,
                    correlationId      = correlationId,
                    headers            = commandMessage.HeaderCollection.ToDictionary(h => h.Name, h => h.Value)
                }
            };

            using (HttpClient httpClient = new HttpClient()) {
                HttpResponseMessage httpResponse = await httpClient.PostAsync(
                    _configuration.GatewayUrl + "/api/rpc.json",
                    new StringContent(_serializationService.SerializeToString(jsonRpcRequest), Encoding.UTF8, "application/json")
                    );

                httpResponse.EnsureSuccessStatusCode();
                JsonRpcResponse jsonRpcResponse = _serializationService.DeserializeFromString <JsonRpcResponse>(
                    await httpResponse.Content.ReadAsStringAsync()
                    );
                if (jsonRpcResponse.error != null)
                {
                    throw new Exception($"JSON RPC Error {jsonRpcResponse.error.code}: {jsonRpcResponse.error.message}");
                }
            }
        }