Last active
August 29, 2015 14:21
-
-
Save seanhess/f4cf71ea58e4483d8ee4 to your computer and use it in GitHub Desktop.
Servant Request Cookie
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
type AuthToken = Cookie "token" Text | |
type AuthAPI = "auth" :> AuthToken :> Get SecureUser | |
authServer :: Pool RethinkDBHandle -> Server AuthAPI | |
authServer h = current | |
where current mt = liftE $ checkAuth h mt | |
-- Auth code ------------------------------------------ | |
-- some stuff is hidden, do you need more context? | |
checkCurrentAuth :: Pool RethinkDBHandle -> Maybe Text -> IO (Maybe User) | |
checkCurrentAuth h mjwt = case mjwt of | |
Nothing -> return Nothing | |
Just jwt -> do | |
mt <- verifyJwt jwt | |
case mt of | |
Nothing -> return Nothing | |
Just t -> do | |
case subject t of | |
Nothing -> return Nothing | |
Just s -> do | |
User.find h $ pack $ show s | |
checkAuth :: Pool RethinkDBHandle -> Maybe Text -> IO (Maybe SecureUser) | |
checkAuth h mt = secure <$> checkCurrentAuth h mt | |
-- ToStatus ------------------------------------------------------ | |
class ToStatus a where | |
toStatus :: a val -> Either ServantErr val | |
instance ToStatus Maybe where | |
toStatus Nothing = Left $ err404 | |
toStatus (Just v) = Right v | |
instance Show a => ToStatus (Either a) where | |
toStatus (Left e) = Left $ err500 { errBody = "Server Error: " <> BL.pack (show e) } | |
toStatus (Right v) = Right v | |
liftE :: ToStatus a => IO (a v) -> EitherT ServantErr IO v | |
liftE action = EitherT $ toStatus <$> action |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment