Wt
3.7.1
|
Basic authentication service. More...
#include <Wt/Auth/AuthService>
Public Member Functions | |
AuthService () | |
Constructor. | |
virtual | ~AuthService () |
Destructor. | |
void | setRandomTokenLength (int length) |
Sets the token length. More... | |
int | randomTokenLength () const |
Returns the token length. More... | |
void | setIdentityPolicy (IdentityPolicy policy) |
Configures the identity policy. More... | |
IdentityPolicy | identityPolicy () const |
Returns the identity policy. More... | |
virtual User | identifyUser (const Identity &identity, AbstractUserDatabase &users) const |
Tries to match the identity to an existing user. More... | |
Authentication token support | |
void | setAuthTokensEnabled (bool enabled, const std::string &cookieName="wtauth", const std::string &cookieDomain=std::string()) |
Configures authentication token support. More... | |
bool | authTokensEnabled () const |
Returns whether authentication tokens are enabled. More... | |
void | setAuthTokenUpdateEnabled (bool enabled) |
Set whether processAuthToken() updates the auth token. More... | |
bool | authTokenUpdateEnabled () const |
Returns whether the auth token is updated. More... | |
std::string | authTokenCookieName () const |
Returns the authentication token cookie name. More... | |
std::string | authTokenCookieDomain () const |
Returns the authentication token cookie domain. More... | |
void | setTokenHashFunction (HashFunction *function) |
Sets the token hash function. More... | |
HashFunction * | tokenHashFunction () const |
Returns the token hash function. More... | |
std::string | createAuthToken (const User &user) const |
Creates and stores an authentication token for the user. More... | |
virtual AuthTokenResult | processAuthToken (const std::string &token, AbstractUserDatabase &users) const |
Processes an authentication token. More... | |
void | setAuthTokenValidity (int minutes) |
Configures the duration for an authenticaton to remain valid. More... | |
int | authTokenValidity () const |
Returns the authentication token validity. More... | |
Email verification | |
void | setEmailVerificationEnabled (bool enabled) |
Configures email verification. More... | |
bool | emailVerificationEnabled () const |
Returns whether email verification is configured. More... | |
void | setEmailVerificationRequired (bool enabled) |
Configure email verificiation to be required for login. More... | |
bool | emailVerificationRequired () const |
void | setEmailRedirectInternalPath (const std::string &internalPath) |
Sets the internal path used to present tokens in emails. More... | |
std::string | emailRedirectInternalPath () const |
Returns the internal path used for email tokens. More... | |
virtual std::string | parseEmailToken (const std::string &internalPath) const |
Parses the emailtoken from an internal path. More... | |
virtual void | verifyEmailAddress (const User &user, const std::string &emailAddress) const |
Verifies an email address. More... | |
virtual void | lostPassword (const std::string &emailAddress, AbstractUserDatabase &users) const |
Implements lost password functionality. More... | |
virtual EmailTokenResult | processEmailToken (const std::string &token, AbstractUserDatabase &users) const |
Processes an email token. More... | |
void | setEmailTokenValidity (int minutes) |
Configures the duration for an email token to remain valid. More... | |
int | emailTokenValidity () const |
Returns the duration for an email token to remain valid. More... | |
virtual void | sendMail (const Mail::Message &message) const |
Sends an email. More... | |
Protected Member Functions | |
virtual void | sendConfirmMail (const std::string &address, const User &user, const std::string &token) const |
Sends a confirmation email to the user to verify his email address. More... | |
virtual void | sendLostPasswordMail (const std::string &address, const User &user, const std::string &token) const |
Sends an email to the user to enter a new password. More... | |
Basic authentication service.
This class presents an basic authentication service, which offers authentication functionality that is not specific to an authentication mechanism (such as password authentication or OAuth authentication).
Like all service classes, this class holds only configuration state. Thus, once configured, it can be safely shared between multiple sessions since its state (the configuration) is read-only. A "const AuthService" object is thus thread-safe.
The class provides the following services (and relevant configuration):
std::string Wt::Auth::AuthService::authTokenCookieDomain | ( | ) | const |
Returns the authentication token cookie domain.
This is the domain used for the authentication cookie. By default this is empty, which means that a cookie will be set for this application.
You may want to set a more general domain if you are sharing the authentication with multiple applications.
std::string Wt::Auth::AuthService::authTokenCookieName | ( | ) | const |
Returns the authentication token cookie name.
This is the default cookie name used for storing the authentication token in the user's browser.
bool Wt::Auth::AuthService::authTokensEnabled | ( | ) | const |
Returns whether authentication tokens are enabled.
bool Wt::Auth::AuthService::authTokenUpdateEnabled | ( | ) | const |
Returns whether the auth token is updated.
int Wt::Auth::AuthService::authTokenValidity | ( | ) | const |
Returns the authentication token validity.
std::string Wt::Auth::AuthService::createAuthToken | ( | const User & | user | ) | const |
Creates and stores an authentication token for the user.
This creates and stores a new authentication token for the given user.
The returned value is the token that may be used to re-identify the user in processAuthToken().
std::string Wt::Auth::AuthService::emailRedirectInternalPath | ( | ) | const |
Returns the internal path used for email tokens.
int Wt::Auth::AuthService::emailTokenValidity | ( | ) | const |
Returns the duration for an email token to remain valid.
bool Wt::Auth::AuthService::emailVerificationEnabled | ( | ) | const |
Returns whether email verification is configured.
bool Wt::Auth::AuthService::emailVerificationRequired | ( | ) | const |
\ Returns whether email verification is required for login.
|
virtual |
Tries to match the identity to an existing user.
When authenticating using a 3rd party Identity Provider, the identity is matched against the existing users, based on the id (with AbstractUserDatabase::findWithIdentity()), or if not matched, based on whether there is a user with the same verified email address as the one indicated by the identity.
IdentityPolicy Wt::Auth::AuthService::identityPolicy | ( | ) | const |
Returns the identity policy.
|
virtual |
Implements lost password functionality.
If email address verification is enabled, then a user may recover his password (or rather, chose a new password) using a procedure which involves sending an email to a verified email address.
This method triggers this process, starting from an email address, if this email address corresponds to a verified email address in the database. The current password is not invalidated.
|
virtual |
Parses the emailtoken from an internal path.
This method parses an internal path and if it matches the email redirection path, it returns the token contained.
It returns an empty string if the internal path does not contain an email token.
|
virtual |
Processes an authentication token.
This verifies an authentication token, and considers whether it matches with a token hash value stored in database. If it matches and auth token update is enabled, the token is updated with a new hash.
|
virtual |
Processes an email token.
This processes a token received through an email. If successful, the token is removed from the database.
This may return two successful results:
int Wt::Auth::AuthService::randomTokenLength | ( | ) | const |
Returns the token length.
|
protectedvirtual |
Sends a confirmation email to the user to verify his email address.
Sends a confirmation email to the given address.
The email content is provided by the following string keys:
|
protectedvirtual |
Sends an email to the user to enter a new password.
This sends a lost password email to the given address
, with a given token
.
The default implementation will call sendMail() with the following message:
|
virtual |
Sends an email.
Sends an email to the given address with subject and body.
The default implementation will consult configuration properties to add a sender address if it hasn't already been set:
Then it uses Mail::Client to send the message, using default the default client settings.
void Wt::Auth::AuthService::setAuthTokensEnabled | ( | bool | enabled, |
const std::string & | cookieName = "wtauth" , |
||
const std::string & | cookieDomain = std::string() |
||
) |
Configures authentication token support.
This method allows you to configure whether authentication tokens are in use. Authentication tokens are used for the user to bypass a more elaborate authentication method, and are a secret shared with the user's user agent, usually in a cookie. They are typically presented in the user interfaces as a "remember me" option.
Whenever a valid authentication token is presented in processToken(), it is invalidated a new token is generated and stored for the user.
The tokens are generated and subsequently hashed using the token hash function. Only the hash values are stored in the user database so that a compromised user database does not compromise these tokens.
Authentication tokens are disabled by default.
void Wt::Auth::AuthService::setAuthTokenUpdateEnabled | ( | bool | enabled | ) |
Set whether processAuthToken() updates the auth token.
If this option is enabled, processAuthToken() will replace the auth token with a new token. This is a bit more secure, because an auth token can only be used once. This is enabled by default.
However, this means that if a user concurrently opens multiple sessions within the same browsers (e.g. multiple tabs being restored at the same time) or refreshes before they receive the new cookie, the user will be logged out, unless the AbstractUserDatabase implementation takes this into account (e.g. keeps the old token valid for a little bit longer)
The default Dbo UserDatabase does not handle concurrent token updates well, so disable this option if you want to prevent that issue.
void Wt::Auth::AuthService::setAuthTokenValidity | ( | int | minutes | ) |
Configures the duration for an authenticaton to remain valid.
The default duration is two weeks (14 * 24 * 60 minutes).
void Wt::Auth::AuthService::setEmailRedirectInternalPath | ( | const std::string & | internalPath | ) |
Sets the internal path used to present tokens in emails.
The default path is "/auth/mail/".
void Wt::Auth::AuthService::setEmailTokenValidity | ( | int | minutes | ) |
Configures the duration for an email token to remain valid.
The default duration is three days (3 * 24 * 60 minutes). Three is a divine number.
void Wt::Auth::AuthService::setEmailVerificationEnabled | ( | bool | enabled | ) |
Configures email verification.
Email verification is useful for a user to recover a lost password, or to be able to confidently confirm other events with this user (such as order processing).
void Wt::Auth::AuthService::setEmailVerificationRequired | ( | bool | enabled | ) |
Configure email verificiation to be required for login.
When enabled, a user will not be able to login if the email-address was not verified.
void Wt::Auth::AuthService::setIdentityPolicy | ( | IdentityPolicy | policy | ) |
Configures the identity policy.
The identity policy has an impact on the login and registration procedure.
void Wt::Auth::AuthService::setRandomTokenLength | ( | int | length | ) |
Sets the token length.
Configures the length used for random tokens. Random tokens are generated for authentication tokens, and email tokens.
The default length is 32 characters.
void Wt::Auth::AuthService::setTokenHashFunction | ( | HashFunction * | function | ) |
Sets the token hash function.
Sets the hash function used to safely store authentication tokens in the database. Ownership of the hash function is transferred.
The default token hash function is an MD5HashFunction.
HashFunction * Wt::Auth::AuthService::tokenHashFunction | ( | ) | const |
Returns the token hash function.
|
virtual |
Verifies an email address.
This registers a new email token with the user.
Then it sends an email to the user's unverified email address with instructions that redirect him to this site, using sendConfirmEmail().