image: ./sw-MFA.webp # Multi-Factor Authentication Digital Employee In todays digital age, security is paramount. As we navigate through the vast expanse of online platforms, the need for robust security measures has never been more crucial. This is where **Multi-Factor Authentication (MFA)** comes into play, adding an extra layer of security by requiring users to verify verify a 6-digit token sent to one of their devices prior to access. On this page, youll find a full breakdown of the prompts used, along with all the functions. The functions are written in Perl. To quickly access all relevant files, please visit the [GitHub repository](https://github.com/signalwire/digital_employees/tree/main/server/Perl_Examples/MFA). :::tip **Try it out!** *Live demo coming soon.* :::
View the project and clone it to your development environment from the [GitHub repository](https://github.com/signalwire/digital_employees/tree/main/server/Perl_Examples/MFA).
## **The Role of send_mfa Function** The send_mfa function is a crucial component in the MFA process, designed to send a 6-digit token to the user via text message. This function is part of a Perl script that interfaces with SignalWires REST API, leveraging its capabilities to enhance user security through MFA. ### **The Workflow** At its core, the function operates within a web application framework, extracting necessary information from incoming requests and utilizing SignalWires messaging platform to dispatch the MFA token. Heres a step-by-step breakdown of how it works: 1. **Initialization**: The script begins by initializing necessary components and decoding incoming request data to extract the users phone number and other relevant information. 2. **SignalWire REST API Setup**: It then sets up the SignalWire REST API with necessary credentials, including the account SID, auth token, and the Space URL. 3. **Sending the SMS**: With everything in place, the script crafts an HTTP POST request to SignalWires MFA endpoint. This request includes the users phone number, the senders number, and parameters specifying the tokens length, validity period, and maximum attempt count. 4. **Handling the Response**: Upon receiving the response from SignalWire, the script decodes it to determine the outcome. If successful, it sends a confirmation back to the user, indicating that the 6-digit number has been sent. In case of failure, it notifies the user to try again. 5. **Broadcasting the Outcome**: Throughout the process, the script also broadcasts updates to a specified agent ID, ensuring transparency and traceability of the MFA requests status. ### **Code Snippet Analysis** The Perl script showcases the practical implementation of the send_mfa function, from initializing the web application components to sending the SMS and handling the response. Notable aspects include: - **Use of SignalWire::ML and SignalWire::RestAPI**: These modules facilitate interaction with SignalWires messaging and REST API, enabling the script to send SMS messages and handle responses effectively. - **Dynamic Response Handling**: The script dynamically adjusts the response based on the success or failure of the SMS sending operation, providing clear feedback to the user. - **Security and Flexibility**: Parameters like token_length, valid_for, and max_attempts add layers of security and flexibility, allowing for customization based on specific requirements. ## **The Role of verify_mfa Function** The verify_mfa function is designed to validate a token previously sent to the user. This step is crucial in the MFA sequence, acting as the final gatekeeper before granting access. Verification ensures that the user not only possesses the correct credentials but also holds the token sent to their device, adding an extra layer of security. ### **How It Works** The Perl script snippet provided gives us insight into how the verify_mfa function operates within a web application. Heres a breakdown of its key components and workflow: 1. **Initialization**: The script sets the stage by initializing essential components and parsing the incoming request to extract the token and other necessary data. 2. **Setting Up the SignalWire REST API**: It then prepares the SignalWire REST API for interaction, configuring it with the account SID, auth token, and Space URL. 3. **Token Verification Request**: With the API ready, the script sends a POST request to verify the token. This is the heart of the verify_mfa function, where the token provided by the user is checked against the one stored or expected by the system. 4. **Response Handling**: After the verification request, the script processes the response. If the token is valid, it confirms the verification success; otherwise, it signals a failure. 5. **Broadcasting the Outcome**: Throughout the process, updates are broadcasted to an agent ID, ensuring transparency and allowing for real-time tracking of the verification process. ### **Code Exploration** The provided Perl code illustrates the practical application of the verify_mfa function, showcasing the seamless integration of MFA verification into a web applications authentication flow. Key points include: - **Use of SignalWire::ML and SignalWire::RestAPI Modules**: These modules facilitate the scripts communication with SignalWires REST API, enabling efficient token verification. - **Dynamic Response Processing**: The script adeptly handles the verification response, adjusting its actions based on the outcome to provide immediate feedback. - **Enhanced Security Measures**: By verifying tokens through a secure API call, the verify_mfa function reinforces the security of the authentication process, ensuring only verified users proceed. ## Video walkthrough *Coming soon.* ## GitHub Check out the full project repository for the MFA demo on GitHub. - [View the project on GitHub](https://github.com/signalwire/digital_employees/tree/main/server/Perl_Examples/MFA) - [Report an issue with a demo](https://github.com/signalwire/digital_employees/issues) ## SignalWire SignalWire’s AI Agent for Voice allows you to build and deploy your own digital employee. Powered by advanced natural language processing (NLP) capabilities, your digital employee will understand caller intent, retain context, and generally behave in a way that feels “human-like”. In fact, you may find that it behaves exactly like your best employee, elevating the customer experience through efficient, intelligent, and personalized interactions. ##### Made with ❤️ by SignalWires DevEx team *Have feedback on this site? Drop us a line at devex@signalwire.com or visit the [Community Slack](https://signalwire-community.slack.com/).*