Added cookie/JWT auth config
This commit is contained in:
		
							parent
							
								
									b379654879
								
							
						
					
					
						commit
						f30d484e9c
					
				@ -2,6 +2,8 @@ namespace PrayerTracker
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
open Microsoft.AspNetCore.Builder
 | 
					open Microsoft.AspNetCore.Builder
 | 
				
			||||||
open Microsoft.AspNetCore.Hosting
 | 
					open Microsoft.AspNetCore.Hosting
 | 
				
			||||||
 | 
					open Microsoft.AspNetCore.Http
 | 
				
			||||||
 | 
					open Microsoft.IdentityModel.Tokens
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Module to hold configuration for the web app
 | 
					/// Module to hold configuration for the web app
 | 
				
			||||||
[<RequireQualifiedAccess>]
 | 
					[<RequireQualifiedAccess>]
 | 
				
			||||||
@ -19,6 +21,7 @@ module Configure =
 | 
				
			|||||||
  open Microsoft.Extensions.Logging
 | 
					  open Microsoft.Extensions.Logging
 | 
				
			||||||
  open Microsoft.Extensions.Options
 | 
					  open Microsoft.Extensions.Options
 | 
				
			||||||
  open NodaTime
 | 
					  open NodaTime
 | 
				
			||||||
 | 
					  open System
 | 
				
			||||||
  open System.Globalization
 | 
					  open System.Globalization
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Set up the configuration for the app
 | 
					  /// Set up the configuration for the app
 | 
				
			||||||
@ -51,6 +54,25 @@ module Configure =
 | 
				
			|||||||
      .AddSingleton<IClock>(SystemClock.Instance)
 | 
					      .AddSingleton<IClock>(SystemClock.Instance)
 | 
				
			||||||
    |> ignore
 | 
					    |> ignore
 | 
				
			||||||
    let config = svc.BuildServiceProvider().GetRequiredService<IConfiguration>()
 | 
					    let config = svc.BuildServiceProvider().GetRequiredService<IConfiguration>()
 | 
				
			||||||
 | 
					    let authConfig = config.GetSection "Tokens"
 | 
				
			||||||
 | 
					    svc.AddAuthentication()
 | 
				
			||||||
 | 
					      .AddCookie(
 | 
				
			||||||
 | 
					        fun opts ->
 | 
				
			||||||
 | 
					          opts.Cookie.Name       <- "PrayerTrackerAuth"
 | 
				
			||||||
 | 
					          opts.Cookie.HttpOnly   <- false
 | 
				
			||||||
 | 
					          opts.Cookie.SameSite   <- SameSiteMode.Strict
 | 
				
			||||||
 | 
					          opts.SlidingExpiration <- true
 | 
				
			||||||
 | 
					          opts.ClaimsIssuer      <- authConfig.["Issuer"])
 | 
				
			||||||
 | 
					      .AddJwtBearer(
 | 
				
			||||||
 | 
					        fun opts ->
 | 
				
			||||||
 | 
					          opts.SaveToken    <- true
 | 
				
			||||||
 | 
					          opts.ClaimsIssuer <- "PrayerTracker"
 | 
				
			||||||
 | 
					          opts.TokenValidationParameters                  <- TokenValidationParameters ()
 | 
				
			||||||
 | 
					          opts.TokenValidationParameters.ValidIssuer      <- authConfig.["Issuer"]
 | 
				
			||||||
 | 
					          opts.TokenValidationParameters.ValidAudience    <- authConfig.["Issuer"]
 | 
				
			||||||
 | 
					          opts.TokenValidationParameters.IssuerSigningKey <- SymmetricSecurityKey (Convert.FromBase64String authConfig.["Key"]))
 | 
				
			||||||
 | 
					    |> ignore
 | 
				
			||||||
 | 
					    let config = svc.BuildServiceProvider().GetRequiredService<IConfiguration>()
 | 
				
			||||||
    let crypto = config.GetSection "CookieCrypto"
 | 
					    let crypto = config.GetSection "CookieCrypto"
 | 
				
			||||||
    CookieCrypto (crypto.["Key"], crypto.["IV"]) |> setCrypto
 | 
					    CookieCrypto (crypto.["Key"], crypto.["IV"]) |> setCrypto
 | 
				
			||||||
    svc.AddDbContext<AppDbContext>(
 | 
					    svc.AddDbContext<AppDbContext>(
 | 
				
			||||||
@ -173,6 +195,7 @@ module Configure =
 | 
				
			|||||||
      .UseStaticFiles()
 | 
					      .UseStaticFiles()
 | 
				
			||||||
      .UseSession()
 | 
					      .UseSession()
 | 
				
			||||||
      .UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value)
 | 
					      .UseRequestLocalization(app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value)
 | 
				
			||||||
 | 
					      .UseAuthentication()
 | 
				
			||||||
      .UseGiraffe(webApp)
 | 
					      .UseGiraffe(webApp)
 | 
				
			||||||
      |> ignore
 | 
					      |> ignore
 | 
				
			||||||
    Views.I18N.setUpFactories <| app.ApplicationServices.GetRequiredService<IStringLocalizerFactory> ()
 | 
					    Views.I18N.setUpFactories <| app.ApplicationServices.GetRequiredService<IStringLocalizerFactory> ()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user