public ActionResult GenerateToken(string sub, string[] scopes)
    {
        if (!DebugHelpers.IsDevelopment())
        {
            return(NotFound());
        }

        if (string.IsNullOrWhiteSpace(sub))
        {
            return(BadRequest("Sub (user ID) should not be empty."));
        }

        return(Ok(new
        {
            access_token = LocalAuthentication.GenerateProfileAccessToken($"local-auth_{sub}", scopes)
        }));
    }
示例#2
0
    private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration)
    {
        var isDevelopment = DebugHelpers.IsDevelopment();
        var environment   = DebugHelpers.GetEnvironment().ToLowerInvariant();
        var serviceId     = configuration.GetServiceId();
        var indexFormat   = $"typingrealm-logs-{environment}-{serviceId}-{{0:yyyy.MM}}";

        var connectionString = configuration.GetConnectionString("Logging");

        var config = new LoggerConfiguration()
                     .MinimumLevel.Information()
                     .MinimumLevel.Override("TypingRealm", isDevelopment ? LogEventLevel.Verbose : LogEventLevel.Debug)
                     .Enrich.FromLogContext()
                     .Enrich.WithProperty("Environment", environment)
                     .Enrich.WithProperty("ServiceId", serviceId)
                     .WriteTo.Console()
                     .WriteTo.File(new CompactJsonFormatter(), "logs/log", rollingInterval: RollingInterval.Day);

        if (!string.IsNullOrWhiteSpace(connectionString))
        {
            var parts    = connectionString.Split(";");
            var url      = parts[0];
            var password = parts[1];

            config = config.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(url))
            {
                IndexFormat                 = indexFormat,
                AutoRegisterTemplate        = true,
                AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                ModifyConnectionSettings    = config => config.BasicAuthentication("elastic", password),
                EmitEventFailure            = EmitEventFailureHandling.WriteToSelfLog |
                                              EmitEventFailureHandling.WriteToFailureSink,
                FailureSink = new FileSink("logs/elastic-failures", new CompactJsonFormatter(), null)

                              // TODO:
                              // For some reason when using it with buffer - some logs are not being written.
                              // So right now it's dependent on up and running elastic stack.
                              // Improve this so that we use filebeat/logstash or smth like that.
                              //BufferBaseFilename = "./logs/elastic-buffer"
            });
        }

        return(config.CreateLogger());
    }
示例#3
0
    public void Configure(IApplicationBuilder app)
    {
        if (DebugHelpers.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("=== TypingRealm server ===").ConfigureAwait(false);
            });

            endpoints.MapHub <MessageHub>("/hub");
        });
    }