Skip to content

Files

Latest commit

bdfb7b6 · Dec 9, 2024

History

History
75 lines (55 loc) · 1.97 KB

two-factor-authentication.md

File metadata and controls

75 lines (55 loc) · 1.97 KB

Two-Factor Authentication (2FA)

Initialize 2FA API client:

    use Infobip\Api\TfaApi;
    use Infobip\Configuration;

    $configuration = new Configuration(
        host: 'your-base-url',
        apiKey: 'your-api-key'
    );
    
    $tfaApi = new TfaApi(config: $configuration);

Before sending one-time PIN codes you need to set up application and message template.

Application setup

The application represents your service. It’s good practice to have separate applications for separate services.

    $tfaApplication = $tfaApi->createTfaApplication(
        new TfaApplicationRequest(name: '2FA application')
    );

    $appId = $tfaApplication->getApplicationId();

Message template setup

Message template is the message body with the PIN placeholder that is sent to end users.

    $tfaMessageTemplate = $tfaApi
        ->createTfaMessageTemplate(
            $appId,
            new TfaCreateMessageRequest(
                messageText: 'Your pin is {{pin}}',
                pinType: TfaPinType::NUMERIC,
                pinLength: 4
            )
        );
    
    $messageId = $tfaMessageTemplate->getMessageId();

Send 2FA code via SMS

After setting up the application and message template, you can start generating and sending PIN codes via SMS to the provided destination address.

   $sendCodeResponse = $tfaApi
        ->sendTfaPinCodeOverSms(
            new TfaStartAuthenticationRequest(
                applicationId: $appId,
                messageId: $messageId,
                to: '41793026727'
                from: 'InfoSMS',
            )
        );

    $isSuccessful = $sendCodeResponse->getSmsStatus() === "MESSAGE_SENT";
    $pinId = $sendCodeResponse->getPinId();

Verify phone number

Verify a phone number to confirm successful 2FA authentication.

    $verifyResponse = $tfaApi->verifyTfaPhoneNumber($pinId, new TfaVerifyPinRequest(pin: '1598'));
    $verified = $verifyResponse->getVerified();