public static void EnableJwtStatelessAuthentication(this IPipelines pipelines,
                                                            Func <NancyContext, ClaimsPrincipal> principalLocator, IPlatformLogger logger)
        {
            StatelessAuthentication.Enable(pipelines, new StatelessAuthenticationConfiguration(ctx =>
            {
                var user = principalLocator(ctx);

                if (user != null)
                {
                    var identity = new ClaimsIdentity(user.Claims.Union(new[] { new Claim("ApiKey", user.Claims.First(c => c.Type == "apikey").Value) }), "jwt");

                    ctx.CurrentUser = new ClaimsPrincipal(identity);

                    logger.ForContext("clientAddress", ctx.Request.UserHostAddress)
                    .Verbose("Authenticated principal: {principalName} " +
                             "for request {requestMethod} {requestPath}.",
                             ctx.CurrentUser.Identity.Name, ctx.Request.Method, ctx.Request.Path);
                }

                return(ctx.CurrentUser);
            }));
        }
Ejemplo n.º 2
0
        public Func <CancellationToken, Task> ConstructAction(IStateMachine machineInstance, State state, string contentType, string payload, IDictionary <string, string> configuration)
        {
            return((cancellationToken) =>
            {
                string headersString = null;
                IDictionary <string, string> headers = null;
                if (configuration != null && configuration.TryGetValue("headers", out headersString))
                {
                    headers = StringSerializer.Deserialize <IDictionary <string, string> >(headersString);
                }

                var actionSettings = new ActionConfiguration(configuration, headers);

                if (payload == null)
                {
                    payload = actionSettings.MessageBody;
                }


                using (var connection = ConnectionFactory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var properties = channel.CreateBasicProperties();

                        properties.ContentType = contentType ?? actionSettings.ContentType ?? "text/plain";
                        properties.Type = actionSettings.MessageType;
                        if (actionSettings.Headers != null)
                        {
                            properties.Headers = actionSettings.Headers
                                                 .ToDictionary <KeyValuePair <string, string>,
                                                                string, object>(kvp => kvp.Key, kvp => kvp.Value);
                        }

                        properties.Headers = properties.Headers ?? new Dictionary <string, object>();

                        properties.Headers.Add("MachineId", machineInstance.MachineId);

                        try
                        {
                            channel.BasicPublish(actionSettings.ExchangeName,
                                                 actionSettings.MessageType, properties, Encoding.UTF8.GetBytes(payload));

                            Logger
                            .ForContext("ActionConfiguration", actionSettings, true)
                            .Debug("Successfully published RabbitMq action message. " +
                                   "MessageType: {messageType}, Payload: {payload}",
                                   actionSettings.MessageType, payload);
                        }
                        catch (Exception ex)
                        {
                            ex.Data.Add("VirtualHost", ConnectionFactory.VirtualHost);
                            ex.Data.Add("HostName", ConnectionFactory.HostName);
                            ex.Data.Add("ExchangeName", actionSettings.ExchangeName);
                            ex.Data.Add("RoutingKey", properties.Type);
                            ex.Data.Add("Headers", properties.Headers);
                            ex.Data.Add("Payload", payload);

                            Logger.Error(ex, "Failed to publish action message to RabbitMq. " +
                                         "MessageType: {messageType}, Payload: {payload}",
                                         actionSettings.MessageType, payload);

                            throw;
                        }

                        return Task.CompletedTask;
                    }
            });
        }