Multi-Factor Authentication
This page explains how to implement Multi-Factor Authentication (MFA) functionality using the SaaSus Auth API, based on the sample application's MFA settings feature.
Below is a screenshot of the multi-factor authentication settings dialog.
※This QR code is dummy data for demonstration purposes. It cannot be used for actual authentication.
The MFA functionality provides the following features:
- Check MFA settings status
- Register authentication applications (Google Authenticator, etc.)
- Enable/disable MFA
- Easy setup with QR codes
Frontend Implementation
Example Implementations
The following links point to repositories that include implementations of the frontend.
- React:
UserMfaSettingDialog.tsx- MFA Settings Dialog Component - React:
HeaderUserbox.tsx- Header User Menu
Backend Implementation
Endpoint Summary
| Type | Method & Path | Description |
|---|---|---|
| MFA Status Check | GET /mfa_status | Retrieves the user's MFA enabled/disabled status. |
| MFA Setup | GET /mfa_setup | Generates QR code URL for authentication app registration. |
| MFA Code Verification | POST /mfa_verify | Verifies authentication code from authentication app and registers MFA. |
| MFA Enable | POST /mfa_enable | Enables MFA for the user. |
| MFA Disable | POST /mfa_disable | Disables MFA for the user. |
The following code samples assume Go for the backend.
MFA Status Check Endpoint
- Go
// Retrieve MFA status (enabled/disabled)
func getMfaStatus(c echo.Context) error {
// Retrieve user information from context
userInfo, ok := c.Get(string(ctxlib.UserInfoKey)).(*authapi.UserInfo)
if !ok {
c.Logger().Error("Failed to get user info")
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve user information"})
}
// Use the SaaSus API to get the user's MFA status
response, err := authClient.GetUserMfaPreferenceWithResponse(context.Background(), userInfo.Id)
if err != nil || response.JSON200 == nil {
c.Logger().Errorf("failed to get MFA status: %v", err)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve MFA status"})
}
// Return MFA enabled/disabled status
return c.JSON(http.StatusOK, map[string]bool{"enabled": response.JSON200.Enabled})
}
Implementation Example Links
The following links contain implementations of this endpoint. Search for the function name to find the relevant section.
- Go (Echo):
getMfaStatus - Python (FastAPI):
get_mfa_status - Java (Spring):
getMfaStatus - C# (.NET 8):
app.MapGet("/mfa_status" - C# (.NET Framework 4.8):
GetMfaStatus
MFA Setup Endpoint
- Go
// Retrieve MFA setup information (generate QR code)
// The frontend application must include X-Access-Token in the request header
func getMfaSetup(c echo.Context) error {
// Retrieve X-Access-Token from the request header
accessToken := c.Request().Header.Get("X-Access-Token")
if accessToken == "" {
// Return authentication error if access token is missing
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Access token is missing"})
}
// Retrieve user information from context
userInfo, ok := c.Get(string(ctxlib.UserInfoKey)).(*authapi.UserInfo)
if !ok {
c.Logger().Error("failed to get user info")
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve user information"})
}
// Use the SaaSus API to generate a secret code for MFA authentication app registration
response, err := authClient.CreateSecretCodeWithResponse(context.Background(), userInfo.Id, authapi.CreateSecretCodeJSONRequestBody{
AccessToken: accessToken,
})
if err != nil || response.JSON201 == nil {
c.Logger().Errorf("failed to create secret code: %v", err)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to generate QR code"})
}
// Generate a QR code URL for Google Authenticator and other authentication apps
qrCodeUrl := "otpauth://totp/SaaSusPlatform:" + userInfo.Email + "?secret=" + response.JSON201.SecretCode + "&issuer=SaaSusPlatform"
// Return the QR code URL
return c.JSON(http.StatusOK, map[string]string{
"qrCodeUrl": qrCodeUrl,
})
}
Implementation Example Links
The following links contain implementations of this endpoint. Search for the function name to find the relevant section.
- Go (Echo):
getMfaSetup - Python (FastAPI):
get_mfa_setup - Java (Spring):
getMfaSetup - C# (.NET 8):
app.MapGet("/mfa_setup" - C# (.NET Framework 4.8):
SetupMfa
MFA Authentication Code Verification Endpoint
- Go
// Verify the user's MFA authentication code
// The frontend application must include X-Access-Token in the request header
func verifyMfa(c echo.Context) error {
// Retrieve user information from context
userInfo, ok := c.Get(string(ctxlib.UserInfoKey)).(*authapi.UserInfo)
if !ok {
c.Logger().Error("Failed to get user info")
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve user information"})
}
// Retrieve X-Access-Token from the request header
accessToken := c.Request().Header.Get("X-Access-Token")
if accessToken == "" {
return c.JSON(http.StatusUnauthorized, map[string]string{"error": "Access token is missing"})
}
// Retrieve the verification code from the request body
var requestBody struct {
VerificationCode string `json:"verification_code"`
}
if err := c.Bind(&requestBody); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request: malformed JSON or incorrect parameters"})
}
if requestBody.VerificationCode == "" {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Verification code is required"})
}
// Use the SaaSus API to register the authentication application
response, err := authClient.UpdateSoftwareTokenWithResponse(context.Background(), userInfo.Id, authapi.UpdateSoftwareTokenJSONRequestBody{
AccessToken: accessToken,
VerificationCode: requestBody.VerificationCode,
})
if err != nil || response.StatusCode() != http.StatusOK {
c.Logger().Errorf("MFA verification failed: Status Code %d, Response %s", response.StatusCode(), string(response.Body))
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "MFA verification failed"})
}
return c.JSON(http.StatusOK, map[string]string{"message": "MFA verification successful"})
}
Implementation Example Links
The following links contain implementations of this endpoint. Search for the function name to find the relevant section.
- Go (Echo):
verifyMfa - Python (FastAPI):
verify_mfa - Java (Spring):
verifyMfa - C# (.NET 8):
app.MapPost("/mfa_verify" - C# (.NET Framework 4.8):
VerifyMfa
MFA Enable Endpoint
- Go
// Enable MFA for the user
func enableMfa(c echo.Context) error {
// Retrieve user information from context
userInfo, ok := c.Get(string(ctxlib.UserInfoKey)).(*authapi.UserInfo)
if !ok {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve user information"})
}
// Create request body to enable MFA
method := authapi.SoftwareToken
requestBody := authapi.UpdateUserMfaPreferenceJSONRequestBody{
Enabled: true,
Method: &method,
}
// Use the SaaSus API to enable MFA
_, err := authClient.UpdateUserMfaPreferenceWithResponse(context.Background(), userInfo.Id, requestBody)
if err != nil {
c.Logger().Errorf("Failed to enable MFA: %v", err)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to enable MFA"})
}
return c.JSON(http.StatusOK, map[string]string{"message": "MFA has been enabled"})
}
Implementation Example Links
The following links contain implementations of this endpoint. Search for the function name to find the relevant section.
- Go (Echo):
enableMfa - Python (FastAPI):
enable_mfa - Java (Spring):
enableMfa - C# (.NET 8):
app.MapPost("/mfa_enable" - C# (.NET Framework 4.8):
EnableMfa
MFA Disable Endpoint
- Go
// Disable MFA for the user
func disableMfa(c echo.Context) error {
// Retrieve user information from context
userInfo, ok := c.Get(string(ctxlib.UserInfoKey)).(*authapi.UserInfo)
if !ok {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to retrieve user information"})
}
// Create request body to disable MFA
method := authapi.SoftwareToken
requestBody := authapi.UpdateUserMfaPreferenceJSONRequestBody{
Enabled: false,
Method: &method,
}
// Use the SaaSus API to disable MFA
_, err := authClient.UpdateUserMfaPreferenceWithResponse(context.Background(), userInfo.Id, requestBody)
if err != nil {
c.Logger().Errorf("Failed to disable MFA: %v", err)
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to disable MFA"})
}
return c.JSON(http.StatusOK, map[string]string{"message": "MFA has been disabled"})
}
Implementation Example Links
The following links contain implementations of this endpoint. Search for the function name to find the relevant section.
- Go (Echo):
disableMfa - Python (FastAPI):
disable_mfa - Java (Spring):
disableMfa - C# (.NET 8):
app.MapPost("/mfa_disable" - C# (.NET Framework 4.8):
DisableMfa