Skip to content

Instantly share code, notes, and snippets.

@Krzysztof-Cieslak
Created August 3, 2018 22:08
Show Gist options
  • Save Krzysztof-Cieslak/5b53a1ff47edf5d323d788cce4913934 to your computer and use it in GitHub Desktop.
Save Krzysztof-Cieslak/5b53a1ff47edf5d323d788cce4913934 to your computer and use it in GitHub Desktop.
Gists for "Using OAuth with Saturn" blog posts
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