Skip to content

Instantly share code, notes, and snippets.

@svdberg
Forked from dpwiz/scotty-auth.hs
Created July 16, 2012 07:20
Show Gist options
  • Save svdberg/3121327 to your computer and use it in GitHub Desktop.
Save svdberg/3121327 to your computer and use it in GitHub Desktop.
main :: IO ()
main = scotty 3000 app
app :: ScottyM ()
app = do
get "/favicon.ico" $ html "ಠ_ಠ"
get "/:method" $ do
r <- request
rcon <- liftIO $ R.connect R.defaultConnectInfo {R.connectPort = R.UnixSocket "redis.sock"}
auth <- liftIO $ auth r rcon
unless (isJust auth) next
liftIO someCode
auth :: Request -> R.Connection -> IO (Maybe ByteString)
auth (Request {requestHeaders=h}) rcon = do
case [v | (k, v) <- h, k == "Authorization" ] of
[value] -> R.runRedis rcon $ check (decode value)
_ -> return Nothing
where
decode = BS.split ':' . decodeLenient . head . drop 1 . BS.split ' '
check [login, pwd] = FA.checkAuth login pwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment