It is possible to authenticate and verify, in a second step, the user who is trying to log in. This is known as Two Factor Authentication (2FA) and 2 Step Verification (2SV).
2FA is commonly used when a password or username is entered, and a second validation step is performed by sending an email or SMS to mitigate brute force attacks.
The second factor authentication can only be One Time Password (OTP) verification, including OTP Custom.
To improve security, the user is required to validate twice before accessing. The first step involves entering a username and password, and the second step performs another validation (like the one mentioned above).
In both cases, in a web or mobile application, if the user has this functionality enabled, there are two steps that the user will have to follow each time they log in. This means that while logging in, the user has to successfully pass the second factor to stay logged in to the app.
In web applications, the user can avoid the second factor on a device they trust for a configured number of days. See Trusted devices.
Two Factor Authentication can be configured with the following authentication types: OTP, Local, Custom, WebService, GAMRemoteRest.
In this example, Local Authentication is set as the first factor authentication and OTP is the second factor authentication.
- Step 1: Add the second factor authentication by choosing OTP. Read Steps to set up OTP to fully understand OTP configuration.
To use OTP only as second factor authentication, the checkbox "Use For First Factor Authentication?" must be kept unchecked.

- Step 2: In this example, the first factor authentication is going to be Local. Local is the default authentication in GAM, so you have to edit it.

In the edit form of Local Authentication, the check box "Enable Two Factor Authentication?" has to be selected.
In the "List of second-factor authentication types" grid, click Add and choose the OTP that was added in step 1. You can add more than one second factor; in that case, the user chooses which one to use when logging in. See More than one second factor authentication type.
The checkbox "Force 2FA for all users?" is selected. This option sets 2FA for all the users in the application.

By selecting the checkbox "Force 2FA for all users?" in the first factor authentication, every user of the app is set with the "Enable two factor authentication?" checkbox selected.
If "Force 2FA for all users?" is not selected, you must select the "Enable two factor authentication?" checkbox in the settings of every user that you want to use 2FA.

A first factor authentication type can offer several second factors, so that each user chooses the one that suits them best, for example a code sent by email or a code from an authenticator app.
The list is configured in the "List of second-factor authentication types" grid of the first factor authentication type. It is available in the Local, Custom, External Web Service, GAMRemoteRest and API Key authentication types.

- Each item must be an enabled OTP authentication type (OTP sent by email, OTP Custom or TOTP with an authenticator app). Otherwise, the authentication type cannot be saved: an authentication type that is not OTP returns error 501, and one that does not exist or is disabled also returns an error.
- At least one second factor is required when "Enable Two Factor Authentication?" is selected (error 503).
- The order of the list is the order in which the options are shown to the user. A repeated authentication type is ignored.
- If the list has only one second factor, the login works as before: after the first factor, the code is sent and the user enters it.
- If the list has more than one second factor, after the first factor the login returns error 505 (The type of two-factor authentication must be selected), and the GAM_OTPAuthentication panel shows the available options. The code is generated and sent only after the user selects one.
- After selecting an option, the user can go back and choose another one with the link "If you are unable to log in, select another method.".
- A TOTP second factor can only be used by users who have the authenticator enabled.

The GAM_OTPAuthentication panel included in the GAM Examples uses the following methods of the GAMRepository external object:
- GetUserAndTwoFactorAuthentications(out:&AuthenticationTypeName, out:&TwoFactorAuthenticationProperties, out:&Errors): Returns the user who passed the first factor, the name of the first factor authentication type and its 2FA properties. TwoFactorAuthenticationProperties.AuthenticationTypes has the list of second factors to show, with Name, Description and IsTOTP, among others.
- TwoFactorAuthenticationNextStep(&UserName, &AuthenticationTypeName, out:&Errors): Generates and sends the code of the second factor selected by the user.
To finish the login, call GAMRepository.Login with the code entered by the user and the following LoginAdditionalParameters:
&AdditionalParameter.AuthenticationTypeName = &AuthenticationTypeName
&AdditionalParameter.UseTwoFactorAuthentication = True
&AdditionalParameter.TFAAuthenticationTypeName = &SelectedTFAAuthenticationTypeName
&AdditionalParameter.OTPStep = 2
&LoginOK = GeneXusSecurity.Tenants.Repository.Login(&UserName, &UserPassword, &AdditionalParameter, &GAMErrorCollection)
Trusted devices allow users to skip the second factor on a device they have already validated. After entering the second factor code, the user can mark the device as trusted. From then on, while the trust is valid, logging in from that device only requires the first factor. When the configured number of days ends, the second factor is requested again.
Trusted devices are configured in the first factor authentication type, in the "Two-Factor Authentication settings" section, so "Enable Two Factor Authentication?" must be selected.
- Trusted devices enable: Allows users to save the device as trusted after entering the second factor code. Default = False.
- Trusted devices expiration days: Number of days the device remains trusted. It cannot be zero when "Trusted devices enable" is selected. Default = 30.
- User event when insert a trusted device: Event subscription executed when a device is saved as trusted. Optional.
- User event when validate a trusted device: Event subscription executed when a trusted device is used to log in. Optional.

When "Trusted devices enable" is selected, the second factor page shows the option "Don't ask again for N days.", where N is the value of "Trusted devices expiration days".

- If the user selects the option and enters a valid code, GAM saves the device as trusted for that user. The device is identified by a random value stored in the GAMTrustedDeviceID cookie (HttpOnly) of the browser. GAM only stores a hash of this value, together with the creation date, the expiration date and a device name built from the browser and the operating system, for example "Google Chrome (Windows)".
- The next time the user logs in from that browser, GAM checks that the device is saved for that user and that it has not expired. If so, the user is logged in after the first factor, without the second factor.
- If the device has expired, or the cookie is not present (for example, after deleting the browser cookies or when using another browser), the second factor is requested again. An expired device is removed when it is detected.
- Each time the user marks the device as trusted again, the previous record of that device is replaced, and the expired devices of the user are removed.
The events allow you to add your own logic, for example to notify the user or to apply additional checks. They receive a TrustedDeviceEventSubscription SDT with: ApplicationID, UserGUID, DeviceID, IPAddress, UserAgent, CreatedDate and Expiration.
- User event when insert a trusted device (User_TrustedDeviceInsert): It is executed before the trusted device is saved. If it returns an error, the device is not saved and the login does not finish.
- User event when validate a trusted device (User_TrustedDeviceValidate): It is executed after GAM validates that the device exists and has not expired. If it returns an error, the device is not considered trusted and the user is asked for the second factor.
For more information, see the following document: GAM - Events subscription
In the GAM Backoffice, users can see the devices they trusted in "My Devices", in the user menu.

The GAM_UserTrustedDevices panel shows the name, creation date and expiration date of each device. A device can be removed with the Delete option; the second factor will be requested again the next time the user logs in from that device.

When an administrator kills the sessions of a user (Kill sessions option of the user in the GAM Backoffice), all the trusted devices of that user are also removed.
The following GAMUser methods are available to manage trusted devices from your own objects:
- GetAllTrustedDevices(out:&Errors): Static method that returns the trusted devices of the current user (collection of UserTrustedDevice: DeviceID, DeviceName, DateCreated, ExpirationDate).
- DeleteTrustedDevice(&DeviceID, out:&Errors): Removes a trusted device of the user.
- DeleteAllTrustedDevices(): Removes all the trusted devices of the user. Returns a Boolean.
Example:
&GAMUserTrustedDevices = GeneXusSecurity.Users.User.GetAllTrustedDevices(&GAMErrorCollection)
For &GAMUserTrustedDevice in &GAMUserTrustedDevices
&DeviceID = &GAMUserTrustedDevice.DeviceID
&DeviceName = &GAMUserTrustedDevice.DeviceName
&ExpirationDate = &GAMUserTrustedDevice.ExpirationDate
EndFor
&GAMUser = GeneXusSecurity.Users.User.Get()
&GAMUser.DeleteTrustedDevice(&DeviceID, &GAMErrorCollection)
If &GAMErrorCollection.Count = 0
Commit
EndIf
- Trusted devices only apply to the login of web applications. They do not apply to the login through REST services (oauth/gam/v2.0/access_token), where the second factor is always requested.
- The trust is bound to the browser cookie, not to the physical device. Anyone who has the cookie can skip the second factor while it is valid, so HTTPS is recommended.
Besides the configuration shown above, 2FA properties can also be set in the GeneXus IDE. For this, you will have to refer to the properties by the following names:
- &AuthenticationTypeLocal.TwoFactorAuthentication.Enable
- &AuthenticationTypeLocal.TwoFactorAuthentication.AuthenticationTypes: Collection of TwoFactorAuthenticationPropertiesAuthType with the second factor authentication types. Set the Name of each item.
- &AuthenticationTypeLocal.TwoFactorAuthentication.AuthenticationTypeName: Deprecated. Use AuthenticationTypes.
- &AuthenticationTypeLocal.TwoFactorAuthentication.FirstAuthenticationFactorExpiration (seconds): Numeric 9. Default = 900.
- &AuthenticationTypeLocal.TwoFactorAuthentication.ForceForAllUsers: If set to false, every user can enable this.
- &AuthenticationTypeLocal.TwoFactorAuthentication.TrustedDeviceEnable: Boolean. Default = False.
- &AuthenticationTypeLocal.TwoFactorAuthentication.TrustedDeviceExpires (days): Numeric 9. Default = 30.
- &AuthenticationTypeLocal.TwoFactorAuthentication.TrustedDeviceEventDeviceInsert: GUID of the event subscription executed when a trusted device is saved.
- &AuthenticationTypeLocal.TwoFactorAuthentication.TrustedDeviceEventDeviceValidate: GUID of the event subscription executed when a trusted device is validated.
- &UserEnableTwoFactorAuth = Boolean (allows null)
Example of adding a second factor authentication type:
&GAMTwoFAAuthenticationType = new()
&GAMTwoFAAuthenticationType.Name = !"otp"
&AuthenticationTypeLocal.TwoFactorAuthentication.AuthenticationTypes.Add(&GAMTwoFAAuthenticationType)
Read this article: Email server configuration with GAM to learn more about it.
1. Access Token
The Endpoint is: https://<domain>/<virtual_directory>/oauth/gam/v2.0/access_token
FIRST POST
Body:
client_id: Client ID of the application, required.
client_secret: Client Secret of the application, required.
grant_type=password: It is required.
scope: Scope of the user account you want to access. It's only required when "&GAMApplication.ClientAuthenticationRequestMustIncludeUserScopes" is True.
username: Username of the user with 2FA enabled, required.
password: Password of the User to be authenticated, required.
authentication_type_name: Name of the authentication type with 2FA configured at the server, required.
POSTMAN Example:

After this request, you will receive an email with the code you will use in the SECOND POST.
SECOND POST
client_id: Client ID of the application, required.
client_secret: Client Secret of the application, required.
grant_type=password: It is required.
scope: Scope of the user account you want to access. It's only required when "&GAMApplication.ClientAuthenticationRequestMustIncludeUserScopes" is True.
password: OTP Code requested in the FIRST POST, required.
username: Username of the user with 2FA enabled, required.
authentication_type_name: Name of the authentication type with 2FA configured at the server, required.
use_2fa=true: It is required.
otp_step=2: It is required.
POSTMAN Example:

2. User Info
The Endpoint is: https://<domain>/<virtual_directory>/oauth/gam/v2.0/userinfo
GET
Headers
Content-Type: Type of content that will be returned. Use application/x-www-form-urlencoded, required.
Authorization: access_token obtained in the SECOND POST, required.

Response:
{
"guid": "4c921af0-6805-4f51-9cfe-4d6cd6821e05",
"username": "juan",
"email": "jperez@genexus.com",
"verified_email": true,
"first_name": "Juan",
"last_name": "Perez",
"external_id": "",
"gender": "M",
"url_image": "",
"url_profile": "",
"phone": "099 888 888",
"address": "",
"city": "",
"state": "",
"post_code": "",
"language": "",
"timezone": "",
}
Since GeneXus 17 Upgrade 5.
More than one second factor authentication type and trusted devices are available since GeneXus 18 Upgrade 17.