Skip to content

Commit 657b790

Browse files
committed
Dummy: Add support for JSON submissions
1 parent d8ebb95 commit 657b790

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

yesod-auth/ChangeLog.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
* Email: Fix typo in `defaultEmailLoginHandler` template [#1605](https://github.com/yesodweb/yesod/pull/1605)
66

7+
## 1.6.8
8+
9+
* Dummy: Add support for JSON submissions [#1619](https://github.com/yesodweb/yesod/pull/1619)
10+
711
## 1.6.7
812

913
* Redirect behavior of `clearCreds` depends on request type [#1598](https://github.com/yesodweb/yesod/pull/1598)

yesod-auth/Yesod/Auth/Dummy.hs

+48-5
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,67 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE TypeFamilies #-}
5+
{-# LANGUAGE ScopedTypeVariables #-}
56
-- | 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+
834
module Yesod.Auth.Dummy
935
( authDummy
1036
) where
1137

1238
import Yesod.Auth
13-
import Yesod.Form (runInputPost, textField, ireq)
39+
import Yesod.Form (FormResult(..), runInputPostResult, textField, ireq)
1440
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")
1548

1649
authDummy :: YesodAuth m => AuthPlugin m
1750
authDummy =
1851
AuthPlugin "dummy" dispatch login
1952
where
2053
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]
2366
dispatch _ _ = notFound
2467
url = PluginR "dummy" []
2568
login authToMaster = do

yesod-auth/yesod-auth.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yesod-auth
2-
version: 1.6.7
2+
version: 1.6.8
33
license: MIT
44
license-file: LICENSE
55
author: Michael Snoyman, Patrick Brisbin

0 commit comments

Comments
 (0)