on master help sign in
Look inside cookies instead of just the header
400bd56Joe Shanahan
doc/tutorial/Authentication.lhs+9−7
@@ -284,14 +284,16 @@ create a value of type `AuthHandler Request Account` using the above `lookupAcco
284method:
284method:
285
285
286```haskell
286```haskell
287−-- | The auth handler wraps a function from Request -> Handler Account
287+import Web.Cookie (parseCookies)
288−-- we look for a Cookie and pass the value of the cookie to `lookupAccount`.
288+
289authHandler :: AuthHandler Request Account
289authHandler :: AuthHandler Request Account
290−authHandler =
290+authHandler = mkAuthHandler handler
291− let handler req = case lookup "servant-auth-cookie" (requestHeaders req) of
291+ where
292+ maybeToEither e = maybe (Left e) Right
292− Nothing -> throwError (err401 { errBody = "Missing auth header" })
293+ throw401 msg = throwError $ err401 { errBody = msg }
293− Just authCookieKey -> lookupAccount authCookieKey
294+ handler req = either throw401 lookupAccount $ do
294− in mkAuthHandler handler
295+ cookie <- maybeToEither "Missing cookie header" $ lookup "cookie" $ requestHeaders req
296+ maybeToEither "Missing token in cookie" $ lookup "servant-auth-cookie" $ parseCookies cookie
295```
297```
296
298
297Let's now protect our API with our new, bespoke authentication scheme. We'll
299Let's now protect our API with our new, bespoke authentication scheme. We'll