Wt
4.11.1
|
Basic authentication service. More...
#include <Wt/Auth/AuthService.h>
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 (std::unique_ptr< 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... | |
Multi-factor authentication (Time-Based One-Time Password). | |
void | setMfaProvider (const std::string &provider) |
Sets whether multiple factors are enabled when logging in. More... | |
const std::string & | mfaProvider () const |
Returns the provider that manages the MFA step. More... | |
bool | mfaEnabled () const |
Returns whether a second factor is enabled when logging in. More... | |
void | setMfaRequired (bool required) |
Sets whether multiple factors are required when logging in. More... | |
bool | mfaRequired () const |
Returns whether multiple factors are required when logging in. More... | |
void | setMfaCodeLength (int length) |
Sets the length of the code that is expected for MFA. More... | |
int | mfaCodeLength () const |
Returns the length of the expected MFA codes. More... | |
void | setMfaTokenCookieName (const std::string &name) |
Sets the name of the cookie used for MFA "remember-me". More... | |
const std::string & | mfaTokenCookieName () const |
Returns the name of the MFA "remember-me" cookie. More... | |
void | setMfaTokenCookieDomain (const std::string &domain) |
Returns the domain of the MFA "remember-me" cookie. More... | |
const std::string & | mfaTokenCookieDomain () const |
Returns the MFA token cookie domain. More... | |
void | setMfaTokenValidity (int validity) |
Sets the validity duration of the MFA "remember-me" cookie. More... | |
int | mfaTokenValidity () const |
Returns the validity span of the MFA "remember-me" cookie. More... | |
void | setMfaThrottleEnabled (bool enabled) |
Sets whether throttling is enabled during the MFA process. More... | |
bool | mfaThrottleEnabled () const |
Returns if throttling is enabled during the MFA process. 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.
int Wt::Auth::AuthService::mfaCodeLength | ( | ) | const |
Returns the length of the expected MFA codes.
bool Wt::Auth::AuthService::mfaEnabled | ( | ) | const |
Returns whether a second factor is enabled when logging in.
This marks that users can enable the MFA feature. This can be any implemenation of the Mfa::AbstractMfaProcess interface. Wt supplies a single default implementation, namely Mfa::TotpProcess.
For the default Wt implementation, this means that developers can generate a TOTP secret key (Mfa::generateSecretKey()) which can be used for MFA, by means of the Mfa::TotpProcess.
const std::string& Wt::Auth::AuthService::mfaProvider | ( | ) | const |
Returns the provider that manages the MFA step.
bool Wt::Auth::AuthService::mfaRequired | ( | ) | const |
Returns whether multiple factors are required when logging in.
This marks that the MFA feature is enforced for all users.
For Wt's default implementation this means that upon logging in they will have to provide a TOTP code for verification.
bool Wt::Auth::AuthService::mfaThrottleEnabled | ( | ) | const |
Returns if throttling is enabled during the MFA process.
const std::string& Wt::Auth::AuthService::mfaTokenCookieDomain | ( | ) | const |
Returns the MFA token cookie domain.
const std::string& Wt::Auth::AuthService::mfaTokenCookieName | ( | ) | const |
Returns the name of the MFA "remember-me" cookie.
This can be configured by the developer, or otherwise uses the same name as authTokenCookieName() with the "-mfa" suffix. If nothing is changed, the default is "wtauth-mfa".
int Wt::Auth::AuthService::mfaTokenValidity | ( | ) | const |
Returns the validity span of the MFA "remember-me" cookie.
|
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. This indicates that the cookie a User has in their browser is still valid, and can be used to uniquely identify them.
If it matches and auth token update is enabled (setAuthTokenUpdateEnabled()), the token is updated with a new hash.
|
virtual |
Processes an email token.
This processes a token received through an email. If it is an email verification token, 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.
The default name for these cookies is wtauth
, and if MFA is enabled, their corresponding name will be wtauth-mfa
. The name of these tokens can be changed (setMfaTokenCookieName()).
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::setMfaCodeLength | ( | int | length | ) |
Sets the length of the code that is expected for MFA.
This function does not make sense for all implementations of MFA.
Currently Wt ships with a TOTP implementation, in which case the code can be any length in the range [6-16]. Of course longer is "better" generally speaking.
By default the length is 6
.
6
is most often seen.void Wt::Auth::AuthService::setMfaProvider | ( | const std::string & | provider | ) |
Sets whether multiple factors are enabled when logging in.
If this is set to any value that isn't empty
, a second factor (currently only TOTP as a default implementation) will be enabled. Instead of logging the User in with the LoginState::Strong (when authenticating with a password), the state will be set to LoginState::RequiresMfa.
This indicates that the user is still required to provide an additional login step. This applied to the regular login only. Login via OAuth does not make use of MFA. The authenticator should be sufficiently secure in itself, and should be trusted.
By default Wt offers TOTP (Time-based One-time password) as a second factor. This is a code that any authenticator app/extension can generate.
By default this feature is disabled.
provider
is set here, MFA will be enabled. This will use TOTP by default. Any custom implementation (using Mfa::AbstractMfaProcess) will require more changes. void Wt::Auth::AuthService::setMfaRequired | ( | bool | required | ) |
Sets whether multiple factors are required when logging in.
If this is set to true
, a second factor (defaulting to TOTP) will be required on login. Instead of logging the User in with the LoginState::Strong (when authenticating with a password), the state will be set to LoginState::RequiresMfa.
This will only take effect is mfaEnabled() is true
as well (and thus if an MFA provider has been configured (setMfaProvider()). If this is the case, all users will be enforced to verify their login with an extra step, for more security.
By Wt's default implementation this will boil down to providing a TOTP code. Either this will be the first time they do this. In which case they will be given the secret key both in QR code format (using Mfa::AbstractMfaWidgget::createSetupView()), and as a string. When they enter a correct generated code, the secret will be attached to the User record as an Identity, and will be used for subsequent login attempts (using Mfa::AbstractMfaProcess::createInputView()).
void Wt::Auth::AuthService::setMfaThrottleEnabled | ( | bool | enabled | ) |
Sets whether throttling is enabled during the MFA process.
By default this is not enabled.
void Wt::Auth::AuthService::setMfaTokenCookieDomain | ( | const std::string & | domain | ) |
Returns the domain of the MFA "remember-me" cookie.
This can be configured by the developer, or otherwise uses the same name as authTokenCookieDomain(). If nothing is changed, the default is an empty string.
This should best be used if multiple applications are living on the same
void Wt::Auth::AuthService::setMfaTokenCookieName | ( | const std::string & | name | ) |
Sets the name of the cookie used for MFA "remember-me".
This name can be changed to any name the developer desires, that is a valid name for a cookie. By default this name is the same name as authTokenCookieName() with "-mfa" appended. If this name is not changed from the default, this will result in the "wtauth-mfa" name.
void Wt::Auth::AuthService::setMfaTokenValidity | ( | int | validity | ) |
Sets the validity duration of the MFA "remember-me" cookie.
The duration is specified in minutes. So a validity of 1440 will keep the cookie valid for a whole day. This ensures that there is a limit on how long the User's MFA verification can be bypassed.
From a security aspect, this value should be kept on the lower side so that a user will be required to demonstrate a TOTP code often to the application, by which they validate their identity.
The default duration is 90 days.
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 | ( | std::unique_ptr< 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().