module Mig.Extra.Plugin.Auth (
WithAuth (..),
withHeaderAuth,
) where
import Control.Monad.IO.Class
import Mig.Core
data WithAuth m token resp = WithAuth
{ forall (m :: * -> *) token resp.
WithAuth m token resp -> token -> m Bool
isValid :: token -> m Bool
, forall (m :: * -> *) token resp.
WithAuth m token resp -> token -> m resp
authFail :: token -> m resp
}
withHeaderAuth :: forall m token resp. (IsResp resp, MonadIO m) => WithAuth m token resp -> Header "auth" token -> Plugin m
WithAuth m token resp
env (Header token
token) = forall (m :: * -> *).
MonadIO m =>
(m (Maybe Response) -> m (Maybe Response)) -> Plugin m
processResponse forall a b. (a -> b) -> a -> b
$ \m (Maybe Response)
getResp -> do
Bool
isOk <- WithAuth m token resp
env.isValid token
token
if Bool
isOk
then m (Maybe Response)
getResp
else forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. IsResp a => a -> Response
toResponse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WithAuth m token resp
env.authFail token
token