Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module presents a Servant AuthHandler
that validates
a LOGGED_IN
Wordpress Cookie & the "wp_rest"
Nonce.
You'll need to build a WPAuthConfig
for your application to pass to
the wpAuthHandler
function. The config defines some specifics about your
Wordpress site, as well as functions to pull a User's authentication data
& to handle authentication failures.
You must define the AuthServerData
type
instance yourself:
type instance "AuthServerData" ("AuthProtect" \"wp\") = WPAuthorization (Entity User)
For more information, be sure to check out the Generalized Authentication section of the servant tutorial.
If you want to build your own custom AuthHandler
, check out the
Wordpress.Auth module.
Synopsis
- wpAuthHandler :: WPAuthConfig Handler a -> AuthHandler Request (WPAuthorization a)
- wpAuthorizedOnlyHandler :: WPAuthConfig Handler a -> (WPAuthError -> Handler a) -> AuthHandler Request a
- data WPAuthorization a
- data WPAuthConfig (m :: Type -> Type) a = WPAuthConfig {
- getCookieName :: m CookieName
- loggedInScheme :: AuthScheme
- nonceScheme :: AuthScheme
- nonceLifetime :: NominalDiffTime
- getUserData :: Text -> m (Maybe (UserAuthData a))
- onAuthenticationFailure :: WPAuthError -> m (WPAuthorization a)
- data CookieName
- data AuthScheme = AuthScheme {}
- data WordpressKey
- wpConfigKey :: Text -> WordpressKey
- data WordpressSalt
- wpConfigSalt :: Text -> WordpressSalt
- data UserAuthData a = UserAuthData {
- userData :: a
- wpUser :: WordpressUserId
- wpPass :: WordpressUserPass
- wpTokens :: [SessionToken]
- newtype WordpressUserId = WordpressUserId {}
- newtype WordpressUserPass = WordpressUserPass {}
- data SessionToken = SessionToken {}
- decodeSessionTokens :: Text -> [SessionToken]
- data WPAuthError
- data CookieHeaderError
- data CookieParseError
- data CookieValidationError
Auth Handlers
wpAuthHandler :: WPAuthConfig Handler a -> AuthHandler Request (WPAuthorization a) Source #
A Servant Authentication Handler that valiates a logged_in
Cookie
& a wp_rest
Nonce.
wpAuthorizedOnlyHandler :: WPAuthConfig Handler a -> (WPAuthError -> Handler a) -> AuthHandler Request a Source #
This is similar to wpAuthHandler
but it allows you to throw an error
for anonymous users with valid nonces - restricting handlers to only
logged in users.
data WPAuthorization a #
The result of the authorizeWordpressRequest
function can be an
authorized user with some additional data, or an anonymous user.
Instances
Eq a => Eq (WPAuthorization a) | |
Defined in Wordpress.Auth (==) :: WPAuthorization a -> WPAuthorization a -> Bool # (/=) :: WPAuthorization a -> WPAuthorization a -> Bool # | |
Show a => Show (WPAuthorization a) | |
Defined in Wordpress.Auth showsPrec :: Int -> WPAuthorization a -> ShowS # show :: WPAuthorization a -> String # showList :: [WPAuthorization a] -> ShowS # |
Configs
data WPAuthConfig (m :: Type -> Type) a #
Configuration data specific to your Wordpress site & Haskell application.
WPAuthConfig | |
|
data CookieName #
The name of a Wordpress authentication cookie. Wordpress's frontend
uses CookieNameWithMD5 "wordpress_logged_in_" "<your-site-url>"
by
default.
CustomCookieName Text | A constant name for the cookie. |
CookieNameWithMD5 Text Text | A cookie name with some text to hash & append. E.g., Wordpress's
|
Instances
Eq CookieName | |
Defined in Wordpress.Auth (==) :: CookieName -> CookieName -> Bool # (/=) :: CookieName -> CookieName -> Bool # | |
Show CookieName | |
Defined in Wordpress.Auth showsPrec :: Int -> CookieName -> ShowS # show :: CookieName -> String # showList :: [CookieName] -> ShowS # |
data AuthScheme #
This represents one of the $scheme
s that Wordpress's cookie/nonce
functions use to salt their hashes.
The built-in Wordpress schemes are auth
/auth_sec
for HTTP/HTTPS
requests to wp-admin
, logged_in
for authenticated front-end
requests, & nonce
for form submissions & API requests.
The secret keys & salts are constants found in your wp-config.php
file, defined as LOGGED_IN_SALT
, LOGGED_IN_KEY
, etc.
Instances
Eq AuthScheme | |
Defined in Wordpress.Auth (==) :: AuthScheme -> AuthScheme -> Bool # (/=) :: AuthScheme -> AuthScheme -> Bool # | |
Show AuthScheme | |
Defined in Wordpress.Auth showsPrec :: Int -> AuthScheme -> ShowS # show :: AuthScheme -> String # showList :: [AuthScheme] -> ShowS # |
data WordpressKey #
An auth scheme's _KEY
constant, usually defined in your Wordpress
site's wp-config.php
. E.g., LOGGED_IN_KEY
Instances
Eq WordpressKey | |
Defined in Wordpress.Auth (==) :: WordpressKey -> WordpressKey -> Bool # (/=) :: WordpressKey -> WordpressKey -> Bool # | |
Show WordpressKey | |
Defined in Wordpress.Auth showsPrec :: Int -> WordpressKey -> ShowS # show :: WordpressKey -> String # showList :: [WordpressKey] -> ShowS # |
wpConfigKey :: Text -> WordpressKey #
Build the _KEY
value for an authentiation scheme.
data WordpressSalt #
An auth scheme's _SALT
constant, usually defined in your Wordpress
site's wp-config.php
. E.g., LOGGED_IN_SALT
Instances
Eq WordpressSalt | |
Defined in Wordpress.Auth (==) :: WordpressSalt -> WordpressSalt -> Bool # (/=) :: WordpressSalt -> WordpressSalt -> Bool # | |
Show WordpressSalt | |
Defined in Wordpress.Auth showsPrec :: Int -> WordpressSalt -> ShowS # show :: WordpressSalt -> String # showList :: [WordpressSalt] -> ShowS # |
wpConfigSalt :: Text -> WordpressSalt #
Build the _SALT
value for an authentiation scheme.
data UserAuthData a #
The data needed for authentication, along with some arbitrary data that is returned on success.
UserAuthData | |
|
Instances
Eq a => Eq (UserAuthData a) | |
Defined in Wordpress.Auth (==) :: UserAuthData a -> UserAuthData a -> Bool # (/=) :: UserAuthData a -> UserAuthData a -> Bool # | |
Show a => Show (UserAuthData a) | |
Defined in Wordpress.Auth showsPrec :: Int -> UserAuthData a -> ShowS # show :: UserAuthData a -> String # showList :: [UserAuthData a] -> ShowS # |
newtype WordpressUserId #
The ID
field from the users
table of a Wordpress site.
Instances
Eq WordpressUserId | |
Defined in Wordpress.Auth (==) :: WordpressUserId -> WordpressUserId -> Bool # (/=) :: WordpressUserId -> WordpressUserId -> Bool # | |
Show WordpressUserId | |
Defined in Wordpress.Auth showsPrec :: Int -> WordpressUserId -> ShowS # show :: WordpressUserId -> String # showList :: [WordpressUserId] -> ShowS # |
newtype WordpressUserPass #
The user_pass
field from the users
table of a Wordpress site.
Instances
Eq WordpressUserPass | |
Defined in Wordpress.Auth (==) :: WordpressUserPass -> WordpressUserPass -> Bool # (/=) :: WordpressUserPass -> WordpressUserPass -> Bool # | |
Show WordpressUserPass | |
Defined in Wordpress.Auth showsPrec :: Int -> WordpressUserPass -> ShowS # show :: WordpressUserPass -> String # showList :: [WordpressUserPass] -> ShowS # |
data SessionToken #
A User Session's Token. These can be found in the usermeta
Wordpress
table for rows where meta_key="session_token"
.
You'll probably want to use decodeSessionTokens
to parse the tables's
meta_value
instead of constructing them yourself.
Instances
Eq SessionToken | |
Defined in Wordpress.Auth (==) :: SessionToken -> SessionToken -> Bool # (/=) :: SessionToken -> SessionToken -> Bool # | |
Show SessionToken | |
Defined in Wordpress.Auth showsPrec :: Int -> SessionToken -> ShowS # show :: SessionToken -> String # showList :: [SessionToken] -> ShowS # |
decodeSessionTokens :: Text -> [SessionToken] #
Decode a serialized PHP array containing a User's Session Tokens.
These are usually stored as the session_tokens
usermeta.
It may be an associative array of tokens to expiration times, or tokens to an associative array of sub-fields:
array( 'some-random-hex-text' => 192836504, // ... ); array( 'deadbeef ' => array( 'expiration' => 9001, // ... ), );
Errors
data WPAuthError #
Potential errors during authentication.
EHeader CookieHeaderError | Header Error. |
EParse CookieParseError | Parsing Error. |
EValid CookieValidationError | Validation Error. |
UserDataNotFound | The |
NoNonce | The |
InvalidNonce | The nonce couldn't be validated. |
Instances
Eq WPAuthError | |
Defined in Wordpress.Auth (==) :: WPAuthError -> WPAuthError -> Bool # (/=) :: WPAuthError -> WPAuthError -> Bool # | |
Show WPAuthError | |
Defined in Wordpress.Auth showsPrec :: Int -> WPAuthError -> ShowS # show :: WPAuthError -> String # showList :: [WPAuthError] -> ShowS # |
data CookieHeaderError #
Potential errors while searching for a specific cookie in the request headers.
NoCookieHeader | The |
NoCookieMatches | No Cookie matched the expected |
Instances
Eq CookieHeaderError | |
Defined in Wordpress.Auth (==) :: CookieHeaderError -> CookieHeaderError -> Bool # (/=) :: CookieHeaderError -> CookieHeaderError -> Bool # | |
Show CookieHeaderError | |
Defined in Wordpress.Auth showsPrec :: Int -> CookieHeaderError -> ShowS # show :: CookieHeaderError -> String # showList :: [CookieHeaderError] -> ShowS # |
data CookieParseError #
Potential errors we may encounter while parsing a WPCookie
.
MalformedCookie | The cookie did not have 4 fields separated by `|` characters. |
InvalidExpiration | The |
Instances
Eq CookieParseError | |
Defined in Wordpress.Auth (==) :: CookieParseError -> CookieParseError -> Bool # (/=) :: CookieParseError -> CookieParseError -> Bool # | |
Show CookieParseError | |
Defined in Wordpress.Auth showsPrec :: Int -> CookieParseError -> ShowS # show :: CookieParseError -> String # showList :: [CookieParseError] -> ShowS # |
data CookieValidationError #
Potential validation errors for a WPCookie
.
CookieExpired | The |
InvalidHash | The |
InvalidToken | The |
Instances
Eq CookieValidationError | |
Defined in Wordpress.Auth (==) :: CookieValidationError -> CookieValidationError -> Bool # (/=) :: CookieValidationError -> CookieValidationError -> Bool # | |
Show CookieValidationError | |
Defined in Wordpress.Auth showsPrec :: Int -> CookieValidationError -> ShowS # show :: CookieValidationError -> String # showList :: [CookieValidationError] -> ShowS # |