module Servant.Auth.Wordpress
(
wpAuthHandler
, wpAuthorizedOnlyHandler
, WPAuthorization(..)
, WPAuthConfig(..)
, CookieName(..)
, AuthScheme(..)
, WordpressKey
, wpConfigKey
, WordpressSalt
, wpConfigSalt
, UserAuthData(..)
, WordpressUserId(..)
, WordpressUserPass(..)
, SessionToken(..)
, decodeSessionTokens
, WPAuthError(..)
, CookieHeaderError(..)
, CookieParseError(..)
, CookieValidationError(..)
)
where
import Network.Wai ( Request
, requestHeaders
, queryString
)
import Servant ( Handler )
import Servant.Server.Experimental.Auth
( AuthHandler
, mkAuthHandler
)
import Wordpress.Auth ( WPAuthConfig(..)
, UserAuthData(..)
, WPAuthorization(..)
, authorizeWordpressRequest
, WPAuthError(..)
, AuthScheme(..)
, CookieName(..)
, CookieHeaderError(..)
, CookieParseError(..)
, CookieValidationError(..)
, WordpressUserId(..)
, WordpressUserPass(..)
, SessionToken(..)
, decodeSessionTokens
, WordpressKey
, wpConfigKey
, WordpressSalt
, wpConfigSalt
, findCookie
)
wpAuthHandler
:: WPAuthConfig Handler a -> AuthHandler Request (WPAuthorization a)
wpAuthHandler = mkAuthHandler . handler
wpAuthorizedOnlyHandler
:: WPAuthConfig Handler a
-> (WPAuthError -> Handler a)
-> AuthHandler Request a
wpAuthorizedOnlyHandler cfg authFailure = mkAuthHandler $ \req -> do
result <- handler cfg req
case result of
WPAuthorizedUser uData -> return uData
WPAnonymousUser -> do
name <- getCookieName cfg
authFailure
$ either EHeader (const $ EHeader NoCookieMatches)
$ findCookie name (requestHeaders req)
handler :: WPAuthConfig Handler a -> Request -> Handler (WPAuthorization a)
handler cfg req =
authorizeWordpressRequest cfg (requestHeaders req) (queryString req)