authBrowserId'

This commit is contained in:
Michael Snoyman 2011-09-11 08:52:23 +03:00
parent 40e83beae3
commit b00fd948bd
3 changed files with 35 additions and 3 deletions

View File

@ -3,6 +3,7 @@
{-# LANGUAGE OverloadedStrings #-}
module Yesod.Auth.BrowserId
( authBrowserId
, authBrowserId'
) where
import Yesod.Auth
@ -11,6 +12,8 @@ import Data.Text (Text)
import Yesod.Core
import Text.Hamlet (hamlet)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T
import Data.Maybe (fromMaybe)
#include "qq.h"
@ -46,3 +49,33 @@ authBrowserId audience = AuthPlugin
<img src="https://browserid.org/i/sign_in_green.png">
|]
}
authBrowserId' :: YesodAuth m => AuthPlugin m
authBrowserId' = AuthPlugin
{ apName = pid
, apDispatch = \m ps ->
case (m, ps) of
("GET", [assertion]) -> do
tm <- getRouteToMaster
r <- getUrlRender
let audience = T.takeWhile (/= '/') $ stripScheme $ r $ tm LoginR
memail <- liftIO $ checkAssertion audience assertion
case memail of
Nothing -> error "Invalid assertion"
Just email -> setCreds True Creds
{ credsPlugin = pid
, credsIdent = email
, credsExtra = []
}
(_, []) -> badMethod
_ -> notFound
, apLogin = \toMaster -> do
addScriptRemote browserIdJs
addHamlet [QQ(hamlet)|
<p>
<a href="javascript:navigator.id.getVerifiedEmail(function(a){if(a)document.location='@{toMaster complete}/'+a});">
<img src="https://browserid.org/i/sign_in_green.png">
|]
}
where
stripScheme t = fromMaybe t $ T.stripPrefix "//" $ snd $ T.breakOn "//" t

View File

@ -13,7 +13,6 @@ import Yesod.Form
import Network.Wai.Handler.Warp (run)
data BID = BID
type Handler = GHandler BID BID
mkYesod "BID" [parseRoutes|
/ RootR GET
@ -39,7 +38,7 @@ instance YesodAuth BID where
loginDest _ = AfterLoginR
logoutDest _ = AuthR LoginR
getAuthId = return . Just . credsIdent
authPlugins = [authBrowserId "localhost:3000"]
authPlugins = [authBrowserId']
instance RenderMessage BID FormMessage where
renderMessage _ _ = defaultFormMessage

View File

@ -1,5 +1,5 @@
name: yesod-auth
version: 0.7.2
version: 0.7.3
license: BSD3
license-file: LICENSE
author: Michael Snoyman, Patrick Brisbin