-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.fs
59 lines (52 loc) · 1.91 KB
/
Program.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace IdentityServer
module Demo =
open System
open Suave
open Suave.Successful
open Suave.RequestErrors
open Suave.Filters
open Suave.Operators
open SecurityMiddleware
open Suave.Owin
let getLinks =
"Look at all my links"
let identity _ =
let config = { defaultConfig with bindings = [ HttpBinding.createSimple Protocol.HTTP "127.0.0.1" 8003] }
let app =
choose [
pathStarts "/identityserver" >=> OwinApp.ofAppFunc "/identityserver" identityServer
NOT_FOUND "No handlers found"
]
let a,startServer = startWebServerAsync config app
Async.Start startServer
a
let userKey = "server.User"
open System.Security.Claims
let authenticated = fun (ctx:HttpContext) ->
async{
if ctx.userState.ContainsKey userKey then
let claims = ctx.userState.[userKey] :?> ClaimsPrincipal
if claims.Identity.IsAuthenticated then
return Some ctx
else
return None
else
return None
}
[<EntryPoint>]
let main argv =
// Launch identity server first
let listening = identity ()
// Wait for the server to start listening
listening |> Async.RunSynchronously |> printfn "Identity server started: %A"
// The token authentication middleware will contact the identity server on construction
let securityMiddleware = securityMiddleware ()
let app =
choose [
path "/devices" >=> (OwinApp.ofAppFunc "/devices" securityMiddleware >=> choose [ authenticated >=> OK "Hello authenticated user" ; OK "Hello, this is a restricted area" ])
path "/test" >=> OK "Hello world, welcome to public recreational area."
NOT_FOUND "No handlers found"
]
// launch the app
startWebServer defaultConfig app
0