Yesod.Auth.Email

yesod-auth-1.4.17: Authentication for Yesod.

Safe Haskell | None
Language | Haskell98

Yesod.Auth.Email

Contents

- Plugin
- Routes
- Types
- Misc
- Default handlers

Description

A Yesod plugin for Authentication via e-mail

This plugin works out of the box by only setting a few methods on the type class that tell the plugin how to interoperate with your user data storage (your database). However, almost everything is customizeable by setting more methods on the type class. In addition, you can send all the form submissions via JSON and completely control the user's flow.

This is a standard registration e-mail flow

1. A user registers a new e-mail address, and an e-mail is sent there
2. The user clicks on the registration link in the e-mail. Note that at this point they are actually logged in (without a password). That means that when they log out they will need to reset their password.
3. The user sets their password and is redirected to the site.

4. The user can now

   - logout and sign in
   - reset their password

# Using JSON Endpoints

We are assuming that you have declared auth route as follows

       /auth AuthR Auth getAuth

If you are using a different route, then you have to adjust the endpoints accordingly.

- Registration

          Endpoint: /auth/page/email/register
          Method: POST
          JSON Data: { "email": "myemail@domain.com" }

- Forgot password

          Endpoint: /auth/page/email/forgot-password
          Method: POST
          JSON Data: { "email": "myemail@domain.com" }

- Login

          Endpoint: /auth/page/email/login
          Method: POST
          JSON Data: {
                         "email": "myemail@domain.com",
                         "password": "myStrongPassword"
                     }

- Set new password

          Endpoint: /auth/page/email/set-password
          Method: POST
          JSON Data: {
                          "new": "newPassword",
                          "confirm": "newPassword",
                          "current": "currentPassword"
                     }

Note that in the set password endpoint, the presence of the key "current" is dependent on how the `needOldPassword` is defined in the instance for `YesodAuthEmail`.

Synopsis

- authEmail :: YesodAuthEmail m => AuthPlugin m
- class (YesodAuth site, PathPiece (AuthEmailId site), RenderMessage site AuthMessage) => YesodAuthEmail site where
  - type AuthEmailId site
- data EmailCreds site = EmailCreds {
  - emailCredsId :: AuthEmailId site
  - emailCredsAuthId :: Maybe (AuthId site)
  - emailCredsStatus :: VerStatus
  - emailCredsVerkey :: Maybe VerKey
  - emailCredsEmail :: Email
  }
- saltPass :: Text -> IO Text
- loginR :: AuthRoute
- registerR :: AuthRoute
- forgotPasswordR :: AuthRoute
- setpassR :: AuthRoute
- verifyR :: Text -> Text -> AuthRoute
- isValidPass :: Text -> SaltedPass -> Bool
- type Email = Text
- type VerKey = Text
- type VerUrl = Text
- type SaltedPass = Text
- type VerStatus = Bool
- type Identifier = Text
- loginLinkKey :: Text
- setLoginLinkKey :: (MonadHandler m, YesodAuthEmail (HandlerSite m)) => AuthId (HandlerSite m) -> m ()
- defaultEmailLoginHandler :: YesodAuthEmail master => (Route Auth -> Route master) -> WidgetT master IO ()
- defaultRegisterHandler :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) Html
- defaultForgotPasswordHandler :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) Html
- defaultSetPasswordHandler :: YesodAuthEmail master => Bool -> HandlerT Auth (HandlerT master IO) TypedContent

# Plugin

authEmail :: YesodAuthEmail m => AuthPlugin m #

class (YesodAuth site, PathPiece (AuthEmailId site), RenderMessage site AuthMessage) => YesodAuthEmail site where #

Minimal complete definition

addUnverified, sendVerifyEmail, getVerifyKey, setVerifyKey, verifyAccount, getPassword, setPassword, getEmailCreds, getEmail, afterPasswordRoute

Associated Types

type AuthEmailId site #

Methods

addUnverified :: Email -> VerKey -> HandlerT site IO (AuthEmailId site) #

Add a new email address to the database, but indicate that the address has not yet been verified.

Since: 1.1.0

sendVerifyEmail :: Email -> VerKey -> VerUrl -> HandlerT site IO () #

Send an email to the given address to verify ownership.

Since: 1.1.0

getVerifyKey :: AuthEmailId site -> HandlerT site IO (Maybe VerKey) #

Get the verification key for the given email ID.

Since: 1.1.0

setVerifyKey :: AuthEmailId site -> VerKey -> HandlerT site IO () #

Set the verification key for the given email ID.

Since: 1.1.0

verifyAccount :: AuthEmailId site -> HandlerT site IO (Maybe (AuthId site)) #

Verify the email address on the given account.

Warning! If you have persisted the ``AuthEmailId` site` somewhere, this method should delete that key, or make it unusable in some fashion. Otherwise, the same key can be used multiple times!

See https://github.com/yesodweb/yesod/issues/1222.

Since: 1.1.0

getPassword :: AuthId site -> HandlerT site IO (Maybe SaltedPass) #

Get the salted password for the given account.

Since: 1.1.0

setPassword :: AuthId site -> SaltedPass -> HandlerT site IO () #

Set the salted password for the given account.

Since: 1.1.0

getEmailCreds :: Identifier -> HandlerT site IO (Maybe (EmailCreds site)) #

Get the credentials for the given `Identifier`, which may be either an email address or some other identification (e.g., username).

Since: 1.2.0

getEmail :: AuthEmailId site -> HandlerT site IO (Maybe Email) #

Get the email address for the given email ID.

Since: 1.1.0

randomKey :: site -> IO VerKey #

Generate a random alphanumeric string.

Since: 1.1.0

afterPasswordRoute :: site -> Route site #

Route to send user to after password has been set correctly.

Since: 1.2.0

needOldPassword :: AuthId site -> HandlerT site IO Bool #

Does the user need to provide the current password in order to set a new password?

Default: if the user logged in via an email link do not require a password.

Since: 1.2.1

checkPasswordSecurity :: AuthId site -> Text -> HandlerT site IO (Either Text ()) #

Check that the given plain-text password meets minimum security standards.

Default: password is at least three characters.

confirmationEmailSentResponse :: Text -> HandlerT site IO TypedContent #

Response after sending a confirmation email.

Since: 1.2.2

normalizeEmailAddress :: site -> Text -> Text #

Additional normalization of email addresses, besides standard canonicalization.

Default: Lower case the email address.

Since: 1.2.3

emailLoginHandler :: (Route Auth -> Route site) -> WidgetT site IO () #

Handler called to render the login page. The default works fine, but you may want to override it in order to have a different DOM.

Default: `defaultEmailLoginHandler`.

Since: 1.4.17

registerHandler :: HandlerT Auth (HandlerT site IO) Html #

Handler called to render the registration page. The default works fine, but you may want to override it in order to have a different DOM.

Default: `defaultRegisterHandler`.

@since: 1.2.6

forgotPasswordHandler :: HandlerT Auth (HandlerT site IO) Html #

Handler called to render the "forgot password" page. The default works fine, but you may want to override it in order to have a different DOM.

Default: `defaultForgotPasswordHandler`.

@since: 1.2.6

setPasswordHandler :: Bool -> HandlerT Auth (HandlerT site IO) TypedContent #

Handler called to render the "set password" page. The default works fine, but you may want to override it in order to have a different DOM.

Default: `defaultSetPasswordHandler`.

@since: 1.2.6

data EmailCreds site #

Data stored in a database for each e-mail address.

Constructors

EmailCreds |
Fields emailCredsId :: AuthEmailId site emailCredsAuthId :: Maybe (AuthId site) emailCredsStatus :: VerStatus emailCredsVerkey :: Maybe VerKey emailCredsEmail :: Email

saltPass :: Text -> IO Text #

Salt a password with a randomly generated salt.

# Routes

loginR :: AuthRoute #

registerR :: AuthRoute #

forgotPasswordR :: AuthRoute #

setpassR :: AuthRoute #

verifyR :: Text -> Text -> AuthRoute #

Since: 1.4.5

isValidPass #

Arguments

:: Text | cleartext password
-> SaltedPass | salted password
-> Bool |

# Types

type Email = Text #

type VerKey = Text #

type VerUrl = Text #

type SaltedPass = Text #

type VerStatus = Bool #

type Identifier = Text #

An Identifier generalizes an email address to allow users to log in with some other form of credentials (e.g., username).

Note that any of these other identifiers must not be valid email addresses.

Since: 1.2.0

# Misc

loginLinkKey :: Text #

Session variable set when user logged in via a login link. See `needOldPassword`.

Since: 1.2.1

setLoginLinkKey :: (MonadHandler m, YesodAuthEmail (HandlerSite m)) => AuthId (HandlerSite m) -> m () #

Set `loginLinkKey` to the current time.

setLoginLinkKey :: (MonadHandler m) => AuthId site -> m ()

Since: 1.2.1

# Default handlers

defaultEmailLoginHandler :: YesodAuthEmail master => (Route Auth -> Route master) -> WidgetT master IO () #

Default implementation of `emailLoginHandler`.

Since: 1.4.17

defaultRegisterHandler :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) Html #

Default implementation of `registerHandler`.

Since: 1.2.6

defaultForgotPasswordHandler :: YesodAuthEmail master => HandlerT Auth (HandlerT master IO) Html #

Default implementation of `forgotPasswordHandler`.

Since: 1.2.6

defaultSetPasswordHandler :: YesodAuthEmail master => Bool -> HandlerT Auth (HandlerT master IO) TypedContent #

Default implementation of `setPasswordHandler`.

Since: 1.2.6