|
1 | 1 | module Saturn |
2 | 2 |
|
3 | | -open Saturn |
4 | | -open FSharp.Control.Tasks.V2.ContextInsensitive |
| 3 | +open System |
| 4 | + |
5 | 5 | open Microsoft.AspNetCore |
6 | 6 | open Microsoft.Extensions.DependencyInjection |
7 | 7 | open Microsoft.AspNetCore.Builder |
| 8 | +open Microsoft.AspNetCore.Authentication |
8 | 9 | open Microsoft.AspNetCore.Authentication.Cookies |
9 | | -open Microsoft.AspNetCore.Http |
10 | | -open System.Threading.Tasks |
11 | | -open System |
12 | 10 | open Microsoft.AspNetCore.Authentication.Google |
13 | | -open Microsoft.AspNetCore.Authentication |
14 | | -open System.Net.Http |
15 | | -open System.Net.Http.Headers |
16 | | -open Newtonsoft.Json.Linq |
| 11 | +open Microsoft.AspNetCore.Authentication.OpenIdConnect |
| 12 | +open Microsoft.AspNetCore.Http |
| 13 | + |
| 14 | +open Saturn |
17 | 15 |
|
18 | 16 | let private addCookie state (c : AuthenticationBuilder) = if not state.CookiesAlreadyAdded then c.AddCookie() |> ignore |
19 | 17 |
|
@@ -185,3 +183,25 @@ type Saturn.Application.ApplicationBuilder with |
185 | 183 | AppConfigs = middleware::state.AppConfigs |
186 | 184 | CookiesAlreadyAdded = true |
187 | 185 | } |
| 186 | + |
| 187 | + /// Enables OpenId authentication with custom configuration |
| 188 | + [<CustomOperation("use_open_id_auth_with_config")>] |
| 189 | + member __.UseOpenIdAuthWithConfig(state: ApplicationState, (config: Action<OpenIdConnect.OpenIdConnectOptions>)) = |
| 190 | + let middleware (app : IApplicationBuilder) = |
| 191 | + app.UseAuthentication() |
| 192 | + |
| 193 | + let service (s: IServiceCollection) = |
| 194 | + let authBuilder = s.AddAuthentication(fun authConfig -> |
| 195 | + authConfig.DefaultScheme <- CookieAuthenticationDefaults.AuthenticationScheme |
| 196 | + authConfig.DefaultChallengeScheme <- OpenIdConnectDefaults.AuthenticationScheme |
| 197 | + authConfig.DefaultSignInScheme <- CookieAuthenticationDefaults.AuthenticationScheme) |
| 198 | + addCookie state authBuilder |
| 199 | + authBuilder.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, config) |> ignore |
| 200 | + |
| 201 | + s |
| 202 | + |
| 203 | + { state with |
| 204 | + ServicesConfig = service::state.ServicesConfig |
| 205 | + AppConfigs = middleware::state.AppConfigs |
| 206 | + CookiesAlreadyAdded = true } |
| 207 | + |
0 commit comments