Last active
August 21, 2017 19:32
-
-
Save frasertweedale/9006a2188b0c92ebb914 to your computer and use it in GitHub Desktop.
WAI Middleware to modify request headers
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
{-# LANGUAGE OverloadedStrings #-} | |
module Middlware where | |
import Control.Monad ((<=<)) | |
import qualified Data.ByteString as B | |
import Network.HTTP.Types | |
import Network.Wai | |
data RemoteUserLookup = RemoteUserLookup | |
HeaderName | |
(B.ByteString -> Maybe B.ByteString) | |
remoteUserLookupX509 :: RemoteUserLookup | |
remoteUserLookupX509 = RemoteUserLookup "SSL_CLIENT_CERT" (const Nothing) | |
lookupRemoteUser :: RemoteUserLookup -> Request -> Maybe B.ByteString | |
lookupRemoteUser (RemoteUserLookup k f) = | |
f <=< lookup k . requestHeaders | |
remoteUserLookupMiddleware :: RemoteUserLookup -> Middleware | |
remoteUserLookupMiddleware a = (. modreq) | |
where | |
modreq req = case lookupRemoteUser a req of | |
Nothing -> req | |
Just s -> | |
req { requestHeaders = ("REMOTE_USER", s) : requestHeaders req } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment