Created
August 3, 2018 22:08
-
-
Save Krzysztof-Cieslak/5b53a1ff47edf5d323d788cce4913934 to your computer and use it in GitHub Desktop.
Gists for "Using OAuth with Saturn" blog posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Users | |
open Saturn | |
open Giraffe | |
open System.Security.Claims | |
let matchUpUsers : HttpHandler = fun next ctx -> | |
// A real implementation would match up user identities with something stored in a database | |
let isAdmin = | |
ctx.User.Claims |> Seq.exists (fun claim -> | |
claim.Issuer = "GitHub" && claim.Type = ClaimTypes.Name && claim.Value = "Krzysztof Cieślak") | |
if isAdmin then | |
ctx.User.AddIdentity(new ClaimsIdentity([Claim(ClaimTypes.Role, "Admin", ClaimValueTypes.String, "MyApplication")])) | |
next ctx | |
let loggedIn = pipeline { | |
requires_authentication (Giraffe.Auth.challenge "GitHub") | |
plug matchUpUsers | |
} | |
let error: HttpHandler = RequestErrors.forbidden (text "Must be admin") | |
let isAdmin = pipeline { | |
plug loggedIn | |
requires_role "Admin" error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment