1
- namespace { {packageName} }
2
-
3
- open Microsoft.AspNetCore.Authentication
4
- open Microsoft.AspNetCore.Authentication.Cookies
5
- open Microsoft.Extensions.DependencyInjection
6
- open Microsoft.AspNetCore.Http
7
- open Microsoft.AspNetCore.Authentication.OAuth
8
- open System
9
- open Giraffe
10
- open FSharp.Control.Tasks.V2.ContextInsensitive
11
- open Microsoft.Extensions.Configuration
12
- open AspNet.Security.ApiKey.Providers.Extensions
13
- open AspNet.Security.ApiKey.Providers.Events
14
-
15
-
16
- module AuthSchemes =
17
-
18
- let accessDenied : HttpHandler = setStatusCode 401 >=> text "Access Denied"
19
-
20
- let buildGoogle (builder:AuthenticationBuilder) name authorizationUrl scopes (settings:IConfiguration) =
21
- builder.AddGoogle(fun googleOptions -> CustomHandlers.setOAuthOptions "Google" googleOptions scopes settings)
22
-
23
- let buildGitHub (builder:AuthenticationBuilder) name authorizationUrl scopes (settings:IConfiguration) =
24
- builder.AddGitHub(fun githubOptions -> CustomHandlers.setOAuthOptions "GitHub" githubOptions scopes settings)
25
-
26
- let buildOAuth (builder:AuthenticationBuilder) (name:string) authorizationUrl scopes (settings:IConfiguration) =
27
- builder.AddOAuth(name, (fun (options:OAuthOptions) ->
28
- options.AuthorizationEndpoint <- authorizationUrl
29
- options.TokenEndpoint <- settings.[name + " TokenUrl" ]
30
- options.CallbackPath <- PathString(settings.[name + " CallbackPath" ])
31
- CustomHandlers.setOAuthOptions " { { name} } " options scopes settings
32
- ))
33
-
34
- let OAuthBuilders = Map.empty.Add( " Google" , buildGoogle).Add( " GitHub" , buildGitHub)
35
-
36
- let checkEnvironment (settings:IConfiguration) name =
37
- if (isNull settings.[name + " ClientId" ]) then
38
- raise (Exception(name + " ClientId is not set." ))
39
- else if (isNull settings.[name + " ClientSecret" ]) then
40
- raise (Exception((name + " ClientSecret is not set." )))
41
-
42
- let getOAuthBuilder settings name =
43
- // check that " xxxClientId" and " xxxClientSecret" configuration variables have been set for all OAuth providers
44
- checkEnvironment settings name
45
- if OAuthBuilders.ContainsKey(name) then
46
- OAuthBuilders.[name]
47
- else
48
- buildOAuth
49
-
50
- let configureOAuth (settings:IConfiguration) services =
51
- { { #authMethods} }
52
- { { #isOAuth} }
53
- (getOAuthBuilder settings " { { name} } " ) services " { { name} } " " { { authorizationUrl} } " [{{#scopes}} " { { scope} } " ;{{ /scopes}}] settings
54
- { { /isOAuth} }
55
- { { /authMethods} }
56
-
57
- let buildApiKeyAuth name (services:AuthenticationBuilder) =
58
- services.AddApiKey(fun options - >
59
- options.Header <- name
60
- options.HeaderKey <- String.Empty
61
- let events = ApiKeyEvents()
62
- options.Events <- CustomHandlers.setApiKeyEvents name events
63
- )
64
-
65
- let configureApiKeyAuth (settings:IConfiguration) services =
66
- { { #authMethods} }
67
- { { #isApiKey} }
68
- { { #isKeyInHeader} }
69
- buildApiKeyAuth " { { name} } " services
70
- { { /isKeyInHeader} }
71
- { { ^isKeyInHeader} }
72
- raise (NotImplementedException( " API key security scheme outside of header has not yet been implemented" ))
73
- { { /isKeyInHeader} }
74
- { { /isApiKey} }
75
- { { /authMethods} }
76
-
77
-
78
- let configureCookie (builder:AuthenticationBuilder) =
79
- builder.AddCookie(CustomHandlers.cookieAuth)
80
-
81
- let configureServices (services:IServiceCollection) =
82
- let serviceProvider = services.BuildServiceProvider()
83
- let settings = serviceProvider.GetService <IConfiguration >()
84
- services.AddAuthentication(fun o -> o.DefaultScheme <- CookieAuthenticationDefaults.AuthenticationScheme)
85
- | > configureOAuth settings
86
- |> configureApiKeyAuth settings
87
- |> configureCookie
88
-
89
- let (|||) v1 v2 =
90
- match v1 with
91
- | Some v -> v1
92
- | None -> v2
93
-
94
- // this can be replaced with ctx.GetCookieValue in Giraffe >= 3.6
95
- let getCookieValue (ctx:HttpContext) (key : string) =
96
- match ctx.Request.Cookies.TryGetValue key with
97
- | true , cookie -> Some cookie
98
- | false, _ -> None
99
-
1
+ namespace { {packageName} }
2
+
3
+ open Microsoft.AspNetCore.Authentication
4
+ open Microsoft.AspNetCore.Authentication.Cookies
5
+ open Microsoft.Extensions.DependencyInjection
6
+ open Microsoft.AspNetCore.Http
7
+ open Microsoft.AspNetCore.Authentication.OAuth
8
+ open System
9
+ open Giraffe
10
+ open FSharp.Control.Tasks.V2.ContextInsensitive
11
+ open Microsoft.Extensions.Configuration
12
+ open AspNet.Security.ApiKey.Providers.Extensions
13
+ open AspNet.Security.ApiKey.Providers.Events
14
+
15
+
16
+ module AuthSchemes =
17
+
18
+ let accessDenied : HttpHandler = setStatusCode 401 >=> text "Access Denied"
19
+
20
+ let buildGoogle (builder:AuthenticationBuilder) name authorizationUrl scopes (settings:IConfiguration) =
21
+ builder.AddGoogle(fun googleOptions -> CustomHandlers.setOAuthOptions "Google" googleOptions scopes settings)
22
+
23
+ let buildGitHub (builder:AuthenticationBuilder) name authorizationUrl scopes (settings:IConfiguration) =
24
+ builder.AddGitHub(fun githubOptions -> CustomHandlers.setOAuthOptions "GitHub" githubOptions scopes settings)
25
+
26
+ let buildOAuth (builder:AuthenticationBuilder) (name:string) authorizationUrl scopes (settings:IConfiguration) =
27
+ builder.AddOAuth(name, (fun (options:OAuthOptions) ->
28
+ options.AuthorizationEndpoint <- authorizationUrl
29
+ options.TokenEndpoint <- settings.[name + " TokenUrl" ]
30
+ options.CallbackPath <- PathString(settings.[name + " CallbackPath" ])
31
+ CustomHandlers.setOAuthOptions " { { name} } " options scopes settings
32
+ ))
33
+
34
+ let OAuthBuilders = Map.empty.Add( " Google" , buildGoogle).Add( " GitHub" , buildGitHub)
35
+
36
+ let checkEnvironment (settings:IConfiguration) name =
37
+ if (isNull settings.[name + " ClientId" ]) then
38
+ raise (Exception(name + " ClientId is not set." ))
39
+ else if (isNull settings.[name + " ClientSecret" ]) then
40
+ raise (Exception((name + " ClientSecret is not set." )))
41
+
42
+ let getOAuthBuilder settings name =
43
+ // check that " xxxClientId" and " xxxClientSecret" configuration variables have been set for all OAuth providers
44
+ checkEnvironment settings name
45
+ if OAuthBuilders.ContainsKey(name) then
46
+ OAuthBuilders.[name]
47
+ else
48
+ buildOAuth
49
+
50
+ let configureOAuth (settings:IConfiguration) services =
51
+ { { #authMethods} }
52
+ { { #isOAuth} }
53
+ (getOAuthBuilder settings " { { name} } " ) services " { { name} } " " { { authorizationUrl} } " [{{#scopes}} " { { scope} } " ;{{ /scopes}}] settings
54
+ { { /isOAuth} }
55
+ { { /authMethods} }
56
+
57
+ let buildApiKeyAuth name (services:AuthenticationBuilder) =
58
+ services.AddApiKey(fun options - >
59
+ options.Header <- name
60
+ options.HeaderKey <- String.Empty
61
+ let events = ApiKeyEvents()
62
+ options.Events <- CustomHandlers.setApiKeyEvents name events
63
+ )
64
+
65
+ let configureApiKeyAuth (settings:IConfiguration) services =
66
+ { { #authMethods} }
67
+ { { #isApiKey} }
68
+ { { #isKeyInHeader} }
69
+ buildApiKeyAuth " { { name} } " services
70
+ { { /isKeyInHeader} }
71
+ { { ^isKeyInHeader} }
72
+ raise (NotImplementedException( " API key security scheme outside of header has not yet been implemented" ))
73
+ { { /isKeyInHeader} }
74
+ { { /isApiKey} }
75
+ { { /authMethods} }
76
+
77
+
78
+ let configureCookie (builder:AuthenticationBuilder) =
79
+ builder.AddCookie(CustomHandlers.cookieAuth)
80
+
81
+ let configureServices (services:IServiceCollection) =
82
+ let serviceProvider = services.BuildServiceProvider()
83
+ let settings = serviceProvider.GetService <IConfiguration >()
84
+ services.AddAuthentication(fun o -> o.DefaultScheme <- CookieAuthenticationDefaults.AuthenticationScheme)
85
+ | > configureOAuth settings
86
+ |> configureApiKeyAuth settings
87
+ |> configureCookie
88
+
89
+ let (|||) v1 v2 =
90
+ match v1 with
91
+ | Some v -> v1
92
+ | None -> v2
93
+
94
+ // this can be replaced with ctx.GetCookieValue in Giraffe >= 3.6
95
+ let getCookieValue (ctx:HttpContext) (key : string) =
96
+ match ctx.Request.Cookies.TryGetValue key with
97
+ | true , cookie -> Some cookie
98
+ | false, _ -> None
99
+
100
100
0 commit comments