diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-01-30 01:30:58 +0100 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-01-30 01:30:58 +0100 |
| commit | bda1e81c87a34bc0e6d2ce805f706a726087e957 (patch) | |
| tree | 124ebf09d1289900e0d67d8723528b96bfbb6a7e /src/server/Startup.cs | |
| parent | 253c8479b9ae0ba6853a70728d3f6e904e1ac2ba (diff) | |
| download | bookmark-thing-bda1e81c87a34bc0e6d2ce805f706a726087e957.tar.xz bookmark-thing-bda1e81c87a34bc0e6d2ce805f706a726087e957.zip | |
feat: WIP: Map github logins
When github is used for login, we want to create
a mapping to a regular user in our database.
This is mainly so that we don't have to change the PK in our database or add a column to it.
Diffstat (limited to 'src/server/Startup.cs')
| -rw-r--r-- | src/server/Startup.cs | 30 |
1 files changed, 6 insertions, 24 deletions
diff --git a/src/server/Startup.cs b/src/server/Startup.cs index 4ad70fc..aa07f33 100644 --- a/src/server/Startup.cs +++ b/src/server/Startup.cs @@ -12,25 +12,6 @@ public class Startup private IWebHostEnvironment WebHostEnvironment { get; } private IConfiguration Configuration { get; } - private string AppDatabaseConnectionString() { - var host = Configuration.GetValue<string>("DB_HOST"); - var port = Configuration.GetValue<string>("DB_PORT"); - var database = Configuration.GetValue<string>("DB_NAME"); - var user = Configuration.GetValue<string>("DB_USER"); - var password = Configuration.GetValue<string>("DB_PASSWORD"); - return $"Server={host};Port={port};Database={database};User Id={user};Password={password}"; - } - - public string QuartzDatabaseConnectionString() { - var host = Configuration.GetValue<string>("QUARTZ_DB_HOST"); - var port = Configuration.GetValue<string>("QUARTZ_DB_PORT"); - var database = Configuration.GetValue<string>("QUARTZ_DB_NAME"); - var user = Configuration.GetValue<string>("QUARTZ_DB_USER"); - var password = Configuration.GetValue<string>("QUARTZ_DB_PASSWORD"); - return $"Server={host};Port={port};Database={database};User Id={user};Password={password}"; - } - - // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddDataProtection() @@ -45,7 +26,7 @@ public class Startup services.Configure(AppJsonSettings.Default); services.AddDbContext<AppDbContext>(options => { - options.UseNpgsql(AppDatabaseConnectionString(), + options.UseNpgsql(ConnectionStrings.AppDatabaseConnectionString(Configuration), builder => { builder.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery); builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), default); @@ -58,7 +39,7 @@ public class Startup services.AddQuartz(options => { options.UsePersistentStore(o => { - o.UsePostgres(QuartzDatabaseConnectionString()); + o.UsePostgres(ConnectionStrings.QuartzDatabaseConnectionString(Configuration)); o.UseSerializer<QuartzJsonSerializer>(); }); options.UseMicrosoftDependencyInjectionJobFactory(); @@ -83,20 +64,21 @@ public class Startup options.Events.OnRedirectToAccessDenied = options.Events.OnRedirectToLogin = c => { c.Response.StatusCode = StatusCodes.Status401Unauthorized; - return Task.FromResult<object>(null); + return Task.FromResult<CookieAuthenticationOptions>(default); }; }) - // TODO: Handle github claims, current behaviour creates entries with user_id set to default guid :D .AddGitHub(options => { options.ClientSecret = Configuration.GetValue<string>("GH_CLIENT_SECRET"); options.ClientId = Configuration.GetValue<string>("GH_CLIENT_ID"); options.SaveTokens = true; + options.ClaimActions.MapJsonKey(AppClaims.GITHUB_ID, "id"); options.CorrelationCookie = new CookieBuilder { - Name = "gh_corr", + Name = "gh_correlation", SameSite = SameSiteMode.Lax, SecurePolicy = CookieSecurePolicy.Always, HttpOnly = true, }; + options.Events.OnCreatingTicket = context => HandleGithubCreatingTicket.Handle(context, Configuration); }) .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>(Constants.BASIC_AUTH_SCHEME, default); |
