servant-rawm icon indicating copy to clipboard operation
servant-rawm copied to clipboard

Question: please advise on how to combine RawM with servant-auth's Auth combinator

Open adetokunbo opened this issue 6 years ago • 5 comments

Hi, and thank you for producing a really useful extension to servant

I'm trying to use it with servant-auth. I declare a route that uses both RawM and Auth in a custom Monad e.g.

type ProtectedRaw = (Auth '[Cookie, JWT] Account) :> "protected" :> RawM

protectedRawR
  :: CookieSettings
  -> JWTSettings -> ServerT ProtectedRaw MainApp
protectedRawR = undefined

-- | The application-level Monad, provides acccess to the configuration
-- information via the Reader Monad.
type MainApp = ReaderT Env (ExceptT ServantErr IO)

-- | Convert a MainApp to a Handler
appToHandler :: Env -> MainApp a -> Handler a
appToHandler env action = do
  res <- liftIO $ runExceptT $ runReaderT action env
  liftEither res

testServer env = serveWithContext @ProtectedRaw Proxy theContext server'
  where
    xsrfs = def { xsrfExcludeGet = True }
    cs = defaultCookieSettings { cookieXsrfSetting = Just xsrfs }
    jwts =  defaultJWTSettings $ envSigningKey env
    theContext = cs :. jwts :. EmptyContext
    server' =
      hoistServerWithContext
      @ProtectedRaw Proxy
      (Proxy :: Proxy '[CookieSettings, JWTSettings])
      (appToHandler env) $ protectedRawR cs jwts

this fails to compile with the following message:

    • No instance for (HasServer
                         (Servant.Auth.Server.Internal.AddSetCookie.AddSetCookieApi
                            (Servant.Auth.Server.Internal.AddSetCookie.AddSetCookieApi
                               (Servant.RawM.Internal.API.RawM'
                                  Servant.RawM.Internal.API.FileServer)))
                         '[CookieSettings, JWTSettings])
        arising from a use of ‘hoistServerWithContext’
    • In the expression:
        hoistServerWithContext
          @ProtectedRaw
          Proxy
          (Proxy :: Proxy '[CookieSettings, JWTSettings])
          (appToHandler env)
      In the expression:
        hoistServerWithContext
          @ProtectedRaw
          Proxy
          (Proxy :: Proxy '[CookieSettings, JWTSettings])
          (appToHandler env)
          $ protectedRawR cs jwts
      In an equation for ‘server'’:
          server'
            = hoistServerWithContext
                @ProtectedRaw
                Proxy
                (Proxy :: Proxy '[CookieSettings, JWTSettings])
                (appToHandler env)
                $ protectedRawR cs jwts
    |
139 |       hoistServerWithContext
    |       ^^^^^^^^^^^^^^^^^^^^^^...

Is that expected ? Is there something wrong with how hoistServerWithContext is used here? Will it be necessary to write a distinct HasServer instance that combines the two combinators ?

adetokunbo avatar Sep 07 '18 04:09 adetokunbo