Using authentication with a custom mono for reading with a servant

Basic authentication protected API

type SubApi = API1 :<|> API2 :<|> API3 type API = BasicAuth "foo-realm" AuthData :> SubApi 

supports handlers like AuthData -> Handler a .

I have a set of handlers:

 handler1 :: Request1 -> AuthMonad Response handler2 :: Request2 -> AuthMonad Response 

Runs in AuthMonad, which is ReaderT, whose context is partially created from AuthData. Using enter and AuthMonad :~> Handler , I can get Server API support handlers like AuthData -> AuthMonad , but I would really like to use the AuthData argument as the environment for runReaderT.

I'm not well-versed in the wizardry type working with enter to figure out how to do this. Any ideas?

+5
source share
1 answer

Just realized that I left this hanging.

Basically, I just changed my mind about this question - the answer is pretty obvious. In the above case, I created a function that would create a natural transformation:

 enterAuth :: AuthData -> AuthMonad :~> Handler 

What I then used when building ServerT:

 protectedServer :: AuthData -> ServerT ProtectedAPI Handler protectedServer ad = enter (enterAuth ad) protectedServer' 

Where

 genAuthServerContext :: Context (AuthHandler Request AuthData ': '[]) 
0
source

All Articles