Exemple #1
0
        private (bool Success, ClaimsPrincipal Principal) IsTokenValid(string token)
        {
            try
            {
                var secret               = configPreloader.Get(ConfigNames.JwtSecret);
                var tokenHandler         = new JwtSecurityTokenHandler();
                var validationParameters = new TokenValidationParameters
                {
                    ValidAudience    = ValidAudience,
                    ValidIssuer      = ValidIssuer,
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(secret))
                };

                var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken securityToken);
                if (principal == null)
                {
                    return(false, null);
                }
                if (securityToken == null)
                {
                    return(false, null);
                }

                return(true, principal);
            }
            catch (Exception exce)
            {
                logger.LogWarning(exce, "Validating the authorization token failed");
                return(false, null);
            }
        }
Exemple #2
0
        public Task SendMessageAsync(string bus, string message)
        {
            var body    = Encoding.UTF8.GetBytes(message);
            var factory = new ConnectionFactory()
            {
                HostName = config.Get(ConfigNames.RabbitMqHost),
                UserName = config.Get(ConfigNames.RabbitMqUser),
                Password = config.Get(ConfigNames.RabbitMqPassword)
            };

            using var connection = factory.CreateConnection();
            using var channel    = connection.CreateModel();

            channel.QueueDeclare(queue: bus, durable: true, exclusive: false, autoDelete: false);
            channel.BasicPublish(exchange: string.Empty, routingKey: bus, body: body);
            return(Task.CompletedTask);
        }
Exemple #3
0
        public async Task SendMessageAsync(string bus, string message)
        {
            var client   = new AmazonSQSClient(config.Get(ConfigNames.AwsMessageBusAccessKeyId), config.Get(ConfigNames.AwsMessageBusAccessKey));
            var response = await client.SendMessageAsync(bus, message);

            logger.LogDebug($"called AwsSqsMessageBus service with {bus} and {message}");
            logger.LogDebug(JsonSerializer.Serialize(response));
        }
Exemple #4
0
        public async Task SendMessageAsync(string bus, string message)
        {
            var accessKeyId = config.Get(ConfigNames.AwsMessageBusAccessKeyId);
            var accessKey   = config.Get(ConfigNames.AwsMessageBusAccessKey);
            var client      = new AmazonEventBridgeClient(accessKeyId, accessKey);
            var response    = await client.PutEventsAsync(new PutEventsRequest
            {
                Entries = new List <PutEventsRequestEntry>
                {
                    new PutEventsRequestEntry
                    {
                        EventBusName = bus,
                        DetailType   = "ntfrex.blog.model",
                        Source       = "ntfrex.blog.web",
                        Detail       = message
                    }
                }
            });

            logger.LogDebug($"called AwsEventBridgeMessageBus service with {bus} and {message}");
            logger.LogDebug(JsonSerializer.Serialize(response));
        }
        public override async Task <HealthCheckResult> DoCheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var adminUsername = config.Get(ConfigNames.AdminUsername);
            var cacheKey      = CacheKeys.FailedLoginRequests;
            var attempts      = await cache.TryGetAsync <int>(cacheKey.Name(adminUsername));

            var message = $"There are {attempts.Value} login attempts for the admin user in the last {cacheKey.TimeToLive} hours";

            if (attempts.Success && attempts.Value >= LoginController.MaxLoginTries)
            {
                return(HealthCheckResult.Degraded(message));
            }

            return(HealthCheckResult.Healthy(message));
        }
        public bool IsAdmin()
        {
            if (httpContextAccessor?.HttpContext?.User?.Claims == null)
            {
                return(false);
            }

            var adminUser = configPreloader.Get(ConfigNames.AdminUsername);

            if (httpContextAccessor.HttpContext.User.GetIdClaim() == adminUser)
            {
                return(true);
            }

            return(false);
        }
Exemple #7
0
 public MongoConnectionFactory(ConfigPreloader config)
 {
     Client = new MongoClient(config.Get(ConfigNames.MongoDbConnectionString));
     Blog   = Client.GetDatabase(config.Get(ConfigNames.BlogDatabaseName));
 }
Exemple #8
0
 private Dictionary <string, string> GetUsersAndPasswords()
 => new Dictionary <string, string>()
 {
     { configPreloader.Get(ConfigNames.AdminUsername), configPreloader.Get(ConfigNames.AdminPassword) }
 };
Exemple #9
0
 public MySqlConnectionFactory(ConfigPreloader configLoader)
 {
     Connection = new MySqlConnection(configLoader.Get(ConfigNames.MySqlDbConnectionString));
     Connection.Open();
 }