Skip to content

Commit

Permalink
Merge pull request #1619 from 3v0k4/auth-dummy-json
Browse files Browse the repository at this point in the history
Dummy: Add support for JSON submissions
  • Loading branch information
snoyberg authored Aug 23, 2019
2 parents d7a2997 + baa6bfb commit 2c2531c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
4 changes: 4 additions & 0 deletions yesod-auth/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

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

## 1.6.8

* Dummy: Add support for JSON submissions [#1619](https://github.com/yesodweb/yesod/pull/1619)

## 1.6.7

* Redirect behavior of `clearCreds` depends on request type [#1598](https://github.com/yesodweb/yesod/pull/1598)
Expand Down
48 changes: 44 additions & 4 deletions yesod-auth/Yesod/Auth/Dummy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,64 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- | Provides a dummy authentication module that simply lets a user specify
-- his/her identifier. This is not intended for real world use, just for
-- testing.
-- their identifier. This is not intended for real world use, just for
-- testing. This plugin supports form submissions via JSON (since 1.6.8).
--
-- = Using the JSON Login Endpoint
--
-- We are assuming that you have declared `authRoute` as follows
--
-- @
-- Just $ AuthR LoginR
-- @
--
-- If you are using a different one, then you have to adjust the
-- endpoint accordingly.
--
-- @
-- Endpoint: \/auth\/page\/dummy
-- Method: POST
-- JSON Data: {
-- "ident": "my identifier"
-- }
-- @
--
-- Remember to add the following headers:
--
-- - Accept: application\/json
-- - Content-Type: application\/json

module Yesod.Auth.Dummy
( authDummy
) where

import Yesod.Auth
import Yesod.Form (runInputPost, textField, ireq)
import Yesod.Core
import Data.Text (Text)
import Data.Aeson.Types (Result(..), Parser)
import qualified Data.Aeson.Types as A (parseEither, withObject)

identParser :: Value -> Parser Text
identParser = A.withObject "Ident" (.: "ident")

authDummy :: YesodAuth m => AuthPlugin m
authDummy =
AuthPlugin "dummy" dispatch login
where
dispatch "POST" [] = do
ident <- runInputPost $ ireq textField "ident"
setCredsRedirect $ Creds "dummy" ident []
(jsonResult :: Result Value) <- parseCheckJsonBody
eIdent <- case jsonResult of
Success val -> return $ A.parseEither identParser val
Error err -> return $ Left err
case eIdent of
Right ident ->
setCredsRedirect $ Creds "dummy" ident []
Left _ -> do
ident <- runInputPost $ ireq textField "ident"
setCredsRedirect $ Creds "dummy" ident []
dispatch _ _ = notFound
url = PluginR "dummy" []
login authToMaster = do
Expand Down
2 changes: 1 addition & 1 deletion yesod-auth/yesod-auth.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yesod-auth
version: 1.6.7
version: 1.6.8
license: MIT
license-file: LICENSE
author: Michael Snoyman, Patrick Brisbin
Expand Down

0 comments on commit 2c2531c

Please sign in to comment.