git.iko.soy/typeable/servant
on master help sign in
Fix the auth combinator example.

This change adapt the auth combinator example to the new router code. In general, the server interpretation of user-written combinators will be affected by the new routing code. The change here also introduces a change in functionality: previously, wrong authentication triggered a "hard failure", whereas we now trigger a "soft failure", which is recoverable. For the simple example, this does not make a lot of difference. In general, I think having a soft failure is the right option to take here, although we want a more general story about the relative priorities of different error codes.

commite83397a1db504ee4195ee3f951c27588108e335d authorAndres Loeh <andres@well-typed.com> dateMon 1 Jun 2015, 15:30 (11 years ago) parent404bfdd
1 file changed+9−8
servant-examples/auth-combinator/auth-combinator.hs+9−8
servant-examples/auth-combinator/auth-combinator.hs+9−8
@@ -28,14 +28,15 @@ data AuthProtected
2828instance HasServer rest => HasServer (AuthProtected :> rest) where
2929 type ServerT (AuthProtected :> rest) m = ServerT rest m
3030
31− route Proxy a request respond =
32− case lookup "Cookie" (requestHeaders request) of
33− Nothing -> respond . succeedWith $ responseLBS status401 [] "Missing auth header."
34− Just v -> do
35− authGranted <- isGoodCookie v
36− if authGranted
37− then route (Proxy :: Proxy rest) a request respond
38− else respond . succeedWith $ responseLBS status403 [] "Invalid cookie."
31+ route Proxy a = WithRequest $ \ request ->
32+ route (Proxy :: Proxy rest) $ do
33+ case lookup "Cookie" (requestHeaders request) of
34+ Nothing -> return $ failWith $ HttpError status401 (Just "Missing auth header.")
35+ Just v -> do
36+ authGranted <- isGoodCookie v
37+ if authGranted
38+ then a
39+ else return $ failWith $ HttpError status403 (Just "Invalid cookie.")
3940
4041type PrivateAPI = Get '[JSON] [PrivateData]
4142