|
2 | 2 | {-# LANGUAGE OverloadedStrings #-}
|
3 | 3 | {-# LANGUAGE FlexibleContexts #-}
|
4 | 4 | {-# LANGUAGE TypeFamilies #-}
|
| 5 | +{-# LANGUAGE ScopedTypeVariables #-} |
5 | 6 | -- | Provides a dummy authentication module that simply lets a user specify
|
6 |
| --- his/her identifier. This is not intended for real world use, just for |
7 |
| --- testing. |
| 7 | +-- their identifier. This is not intended for real world use, just for |
| 8 | +-- testing. This plugin supports form submissions via JSON (since 1.6.8). |
| 9 | +-- |
| 10 | +-- = Using the JSON Login Endpoint |
| 11 | +-- |
| 12 | +-- We are assuming that you have declared `authRoute` as follows |
| 13 | +-- |
| 14 | +-- @ |
| 15 | +-- Just $ AuthR LoginR |
| 16 | +-- @ |
| 17 | +-- |
| 18 | +-- If you are using a different one, then you have to adjust the |
| 19 | +-- endpoint accordingly. |
| 20 | +-- |
| 21 | +-- @ |
| 22 | +-- Endpoint: \/auth\/page\/dummy |
| 23 | +-- Method: POST |
| 24 | +-- JSON Data: { |
| 25 | +-- "ident": "my identifier" |
| 26 | +-- } |
| 27 | +-- @ |
| 28 | +-- |
| 29 | +-- Remember to add the following headers: |
| 30 | +-- |
| 31 | +-- - Accept: application\/json |
| 32 | +-- - Content-Type: application\/json |
| 33 | + |
8 | 34 | module Yesod.Auth.Dummy
|
9 | 35 | ( authDummy
|
10 | 36 | ) where
|
11 | 37 |
|
12 | 38 | import Yesod.Auth
|
13 |
| -import Yesod.Form (runInputPost, textField, ireq) |
| 39 | +import Yesod.Form (FormResult(..), runInputPostResult, textField, ireq) |
14 | 40 | import Yesod.Core
|
| 41 | +import Data.Text (Text) |
| 42 | +import qualified Data.Text as T |
| 43 | +import Data.Aeson.Types (Result(..), Parser) |
| 44 | +import qualified Data.Aeson.Types as A (parseEither, withObject) |
| 45 | + |
| 46 | +identParser :: Value -> Parser Text |
| 47 | +identParser = A.withObject "Ident" (.: "ident") |
15 | 48 |
|
16 | 49 | authDummy :: YesodAuth m => AuthPlugin m
|
17 | 50 | authDummy =
|
18 | 51 | AuthPlugin "dummy" dispatch login
|
19 | 52 | where
|
20 | 53 | dispatch "POST" [] = do
|
21 |
| - ident <- runInputPost $ ireq textField "ident" |
22 |
| - setCredsRedirect $ Creds "dummy" ident [] |
| 54 | + formResult <- runInputPostResult $ ireq textField "ident" |
| 55 | + eIdent <- case formResult of |
| 56 | + FormSuccess ident -> |
| 57 | + return $ Right ident |
| 58 | + _ -> do |
| 59 | + (jsonResult :: Result Value) <- parseCheckJsonBody |
| 60 | + case jsonResult of |
| 61 | + Success val -> return $ A.parseEither identParser val |
| 62 | + Error err -> return $ Left err |
| 63 | + case eIdent of |
| 64 | + Right ident -> setCredsRedirect $ Creds "dummy" ident [] |
| 65 | + Left err -> invalidArgs [T.pack err] |
23 | 66 | dispatch _ _ = notFound
|
24 | 67 | url = PluginR "dummy" []
|
25 | 68 | login authToMaster = do
|
|
0 commit comments