无法创建提供会话 cookie 的经过身份验证的 Servant 客户端

时间:2021-05-03 07:55:26

标签: haskell session-cookies servant

我正在尝试为具有会话/cookie 身份验证的基本 API 创建一个 Servant 客户端;但是,我收到了关于缺少 BearerAuthNotEnabled 实例的类型检查错误。

这是我的 API:

import           RIO
import qualified Data.Aeson                    as J
import           Servant                       ( (:>), (:<|>) )
import qualified Servant                       as SV
import qualified Servant.Auth.Server           as AS
import qualified Servant.Client                as SC

-- Contents of the session cookie
data User = User
  { username :: !Text
  , email :: !Text
  } deriving (Eq, Show, Generic)

instance J.ToJSON User
instance J.FromJSON User
instance AS.ToJWT User
instance AS.FromJWT User

-- An endpoint that requires authentication.
type ProtectedEndpoint = "protected" :> SV.Get '[SV.JSON] Text

-- The API with authentication wrapped around the protected endpoint.
type Api = (AS.Auth '[AS.Cookie] User :> ProtectedEndpoint)

这是我创建客户端的尝试:

apiProxy :: Proxy Api
apiProxy = Proxy

-- Let Servant generate HTTP clients.
protectedClient :: AC.Token -> SC.ClientM Text
protectedClient = SC.client apiProxy

在编译时,我得到以下实例错误,我无法理解:

    • No instance for Servant.Auth.Client.Internal.BearerAuthNotEnabled
        arising from a use of ‘SC.client’
    • In the expression: SC.client apiProxy
      In an equation for ‘protectedClient’:
          protectedClient = SC.client apiProxy

我是否正确配置了 Servant 客户端以传递会话 cookie?

更新: 向混合中添加令牌身份验证 (JWT) 解决了该问题。即,如果我将 API 类型声明为

type Api = (AS.Auth '[AS.Cookie, AS.JWT] User :> ProtectedEndpoint)
-- instead of
-- type Api = (AS.Auth '[AS.Cookie] User :> ProtectedEndpoint)

0 个答案:

没有答案
相关问题