Removed model bits
This commit is contained in:
parent
ffec788bf7
commit
564d1431df
@ -89,7 +89,7 @@ instance SafeFromObject Html where
|
|||||||
map helper2 m ++
|
map helper2 m ++
|
||||||
[ toLazyByteString "</dl>" ]
|
[ toLazyByteString "</dl>" ]
|
||||||
helper2 :: (B.ByteString, RawObject) -> B.ByteString
|
helper2 :: (B.ByteString, RawObject) -> B.ByteString
|
||||||
helper2 (k, v) = B.concat $
|
helper2 (k, v) = B.concat
|
||||||
[ toLazyByteString "<dt>"
|
[ toLazyByteString "<dt>"
|
||||||
, toLazyByteString k
|
, toLazyByteString k
|
||||||
, toLazyByteString "</dt><dd>"
|
, toLazyByteString "</dt><dd>"
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
{-# LANGUAGE FunctionalDependencies #-}
|
|
||||||
{-# LANGUAGE TypeSynonymInstances #-}
|
{-# LANGUAGE TypeSynonymInstances #-}
|
||||||
{-# LANGUAGE ExistentialQuantification #-}
|
{-# LANGUAGE ExistentialQuantification #-}
|
||||||
{-# LANGUAGE ScopedTypeVariables #-}
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
@ -44,10 +43,7 @@ import Web.Restful.Constants
|
|||||||
import Web.Restful.Resource
|
import Web.Restful.Resource
|
||||||
|
|
||||||
-- | A data type that can be turned into a Hack application.
|
-- | A data type that can be turned into a Hack application.
|
||||||
class ResourceName a b => RestfulApp a b | a -> b where
|
class ResourceName a => RestfulApp a where
|
||||||
-- | Load up the model, ie the data which use passed to each handler.
|
|
||||||
getModel :: a -> IO b
|
|
||||||
|
|
||||||
-- | The encryption key to be used for encrypting client sessions.
|
-- | The encryption key to be used for encrypting client sessions.
|
||||||
encryptKey :: a -> IO Word256
|
encryptKey :: a -> IO Word256
|
||||||
encryptKey _ = getKey defaultKeyFile
|
encryptKey _ = getKey defaultKeyFile
|
||||||
@ -87,25 +83,23 @@ class ResourceName a b => RestfulApp a b | a -> b where
|
|||||||
|
|
||||||
-- | Given a sample resource name (purely for typing reasons), generating
|
-- | Given a sample resource name (purely for typing reasons), generating
|
||||||
-- a Hack application.
|
-- a Hack application.
|
||||||
toHackApp :: RestfulApp resourceName modelType
|
toHackApp :: RestfulApp resourceName
|
||||||
=> resourceName
|
=> resourceName
|
||||||
-> IO Hack.Application
|
-> IO Hack.Application
|
||||||
toHackApp a = do
|
toHackApp a = do
|
||||||
when (checkOverlaps a) $ checkResourceName a -- FIXME maybe this should be done compile-time?
|
when (checkOverlaps a) $ checkResourceName a -- FIXME maybe this should be done compile-time?
|
||||||
model <- getModel a
|
|
||||||
key <- encryptKey a
|
key <- encryptKey a
|
||||||
let handlers = getHandler model
|
let app' = toHackApplication a getHandler
|
||||||
app' = toHackApplication a handlers
|
|
||||||
clientsession' = clientsession [authCookieName] key -- FIXME gotta be a better way...
|
clientsession' = clientsession [authCookieName] key -- FIXME gotta be a better way...
|
||||||
app = foldr ($) app' $ hackMiddleware a ++ [clientsession']
|
app = foldr ($) app' $ hackMiddleware a ++ [clientsession']
|
||||||
return app
|
return app
|
||||||
|
|
||||||
findResourceNames :: ResourceName a model
|
findResourceNames :: ResourceName a
|
||||||
=> Resource
|
=> Resource
|
||||||
-> [(a, [(String, String)])]
|
-> [(a, [(String, String)])]
|
||||||
findResourceNames r = takeJusts $ map (checkPatternHelper r) enumerate
|
findResourceNames r = takeJusts $ map (checkPatternHelper r) enumerate
|
||||||
|
|
||||||
checkPatternHelper :: ResourceName a model
|
checkPatternHelper :: ResourceName a
|
||||||
=> Resource
|
=> Resource
|
||||||
-> a
|
-> a
|
||||||
-> Maybe (a, [(String, String)])
|
-> Maybe (a, [(String, String)])
|
||||||
@ -119,7 +113,7 @@ takeJusts [] = []
|
|||||||
takeJusts (Nothing:rest) = takeJusts rest
|
takeJusts (Nothing:rest) = takeJusts rest
|
||||||
takeJusts (Just x:rest) = x : takeJusts rest
|
takeJusts (Just x:rest) = x : takeJusts rest
|
||||||
|
|
||||||
toHackApplication :: RestfulApp resourceName model
|
toHackApplication :: RestfulApp resourceName
|
||||||
=> resourceName
|
=> resourceName
|
||||||
-> (resourceName -> Verb -> Handler)
|
-> (resourceName -> Verb -> Handler)
|
||||||
-> Hack.Application
|
-> Hack.Application
|
||||||
|
|||||||
@ -15,6 +15,9 @@
|
|||||||
---------------------------------------------------------
|
---------------------------------------------------------
|
||||||
module Web.Restful.Helpers.Auth
|
module Web.Restful.Helpers.Auth
|
||||||
( AuthResource
|
( AuthResource
|
||||||
|
, authHandler
|
||||||
|
, authResourcePattern
|
||||||
|
, RpxnowApiKey (..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Hack
|
import qualified Hack
|
||||||
@ -50,24 +53,26 @@ instance Enumerable AuthResource where
|
|||||||
, LoginRpxnow
|
, LoginRpxnow
|
||||||
]
|
]
|
||||||
|
|
||||||
type RpxnowApiKey = String -- FIXME newtype
|
newtype RpxnowApiKey = RpxnowApiKey String
|
||||||
instance ResourceName AuthResource (Maybe RpxnowApiKey) where
|
|
||||||
getHandler _ Check Get = authCheck
|
|
||||||
getHandler _ Logout Get = authLogout
|
|
||||||
getHandler _ Openid Get = authOpenidForm
|
|
||||||
getHandler _ OpenidForward Get = authOpenidForward
|
|
||||||
getHandler _ OpenidComplete Get = authOpenidComplete
|
|
||||||
-- two different versions of RPX protocol apparently...
|
|
||||||
getHandler (Just key) LoginRpxnow Get = rpxnowLogin key
|
|
||||||
getHandler (Just key) LoginRpxnow Post = rpxnowLogin key
|
|
||||||
getHandler _ _ _ = notFound
|
|
||||||
|
|
||||||
resourcePattern Check = "/auth/check/"
|
authHandler :: Maybe RpxnowApiKey -> AuthResource -> Verb -> Handler
|
||||||
resourcePattern Logout = "/auth/logout/"
|
authHandler _ Check Get = authCheck
|
||||||
resourcePattern Openid = "/auth/openid/"
|
authHandler _ Logout Get = authLogout
|
||||||
resourcePattern OpenidForward = "/auth/openid/forward/"
|
authHandler _ Openid Get = authOpenidForm
|
||||||
resourcePattern OpenidComplete = "/auth/openid/complete/"
|
authHandler _ OpenidForward Get = authOpenidForward
|
||||||
resourcePattern LoginRpxnow = "/auth/login/rpxnow/"
|
authHandler _ OpenidComplete Get = authOpenidComplete
|
||||||
|
-- two different versions of RPX protocol apparently...
|
||||||
|
authHandler (Just (RpxnowApiKey key)) LoginRpxnow Get = rpxnowLogin key
|
||||||
|
authHandler (Just (RpxnowApiKey key)) LoginRpxnow Post = rpxnowLogin key
|
||||||
|
authHandler _ _ _ = notFound
|
||||||
|
|
||||||
|
authResourcePattern :: AuthResource -> String -- FIXME supply prefix as well
|
||||||
|
authResourcePattern Check = "/auth/check/"
|
||||||
|
authResourcePattern Logout = "/auth/logout/"
|
||||||
|
authResourcePattern Openid = "/auth/openid/"
|
||||||
|
authResourcePattern OpenidForward = "/auth/openid/forward/"
|
||||||
|
authResourcePattern OpenidComplete = "/auth/openid/complete/"
|
||||||
|
authResourcePattern LoginRpxnow = "/auth/login/rpxnow/"
|
||||||
|
|
||||||
|
|
||||||
data OIDFormReq = OIDFormReq (Maybe String) (Maybe String)
|
data OIDFormReq = OIDFormReq (Maybe String) (Maybe String)
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
{-# LANGUAGE MultiParamTypeClasses #-}
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
{-# LANGUAGE FunctionalDependencies #-}
|
|
||||||
{-# LANGUAGE TypeSynonymInstances #-}
|
{-# LANGUAGE TypeSynonymInstances #-}
|
||||||
{-# LANGUAGE OverlappingInstances #-}
|
{-# LANGUAGE OverlappingInstances #-}
|
||||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
@ -70,7 +69,7 @@ fromString' ('*':rest) = Slurp rest
|
|||||||
fromString' ('#':rest) = DynInt rest
|
fromString' ('#':rest) = DynInt rest
|
||||||
fromString' x = Static x
|
fromString' x = Static x
|
||||||
|
|
||||||
class (Show a, Enumerable a) => ResourceName a b | a -> b where
|
class (Show a, Enumerable a) => ResourceName a where
|
||||||
-- | Get the URL pattern for each different resource name.
|
-- | Get the URL pattern for each different resource name.
|
||||||
-- Something like /foo/$bar/baz/ will match the regular expression
|
-- Something like /foo/$bar/baz/ will match the regular expression
|
||||||
-- /foo/(\\w*)/baz/, matching the middle part with the urlParam bar.
|
-- /foo/(\\w*)/baz/, matching the middle part with the urlParam bar.
|
||||||
@ -80,7 +79,7 @@ class (Show a, Enumerable a) => ResourceName a b | a -> b where
|
|||||||
resourcePattern :: a -> String
|
resourcePattern :: a -> String
|
||||||
|
|
||||||
-- | Find the handler for each resource name/verb pattern.
|
-- | Find the handler for each resource name/verb pattern.
|
||||||
getHandler :: b -> a -> Verb -> Handler
|
getHandler :: a -> Verb -> Handler
|
||||||
|
|
||||||
type SMap = [(String, String)]
|
type SMap = [(String, String)]
|
||||||
|
|
||||||
@ -135,7 +134,7 @@ overlaps (Static s:x) (DynInt _:y)
|
|||||||
| otherwise = False
|
| otherwise = False
|
||||||
overlaps (Static a:x) (Static b:y) = a == b && overlaps x y
|
overlaps (Static a:x) (Static b:y) = a == b && overlaps x y
|
||||||
|
|
||||||
checkResourceName :: (Monad m, ResourceName rn model) => rn -> m ()
|
checkResourceName :: (Monad m, ResourceName rn) => rn -> m ()
|
||||||
checkResourceName rn = do
|
checkResourceName rn = do
|
||||||
let avs@(y:_) = enumerate
|
let avs@(y:_) = enumerate
|
||||||
_ignore = asTypeOf rn y
|
_ignore = asTypeOf rn y
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
name: restful
|
name: restful
|
||||||
version: 0.1.6
|
version: 0.1.7
|
||||||
license: BSD3
|
license: BSD3
|
||||||
license-file: LICENSE
|
license-file: LICENSE
|
||||||
author: Michael Snoyman <michael@snoyman.com>
|
author: Michael Snoyman <michael@snoyman.com>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user