PHPackages                             beastbytes/yii-otp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. beastbytes/yii-otp

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

beastbytes/yii-otp
==================

Library that simplifies using One Time Passwords (OTP) (HOTP or TOTP algorithm) in Yii3 applications

013PHP

Since Dec 7Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/beastbytes/yii-otp)[ Packagist](https://packagist.org/packages/beastbytes/yii-otp)[ RSS](/packages/beastbytes-yii-otp/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

BeastBytes Yii OTP
==================

[](#beastbytes-yii-otp)

BeastBytes Yii OTP simplifies integrating Two-Factor Authentication (2FA) using either **HOTP** (HMAC One-Time Password - [RFC 4226](https://datatracker.ietf.org/doc/html/rfc4226)) or **TOTP** (Time-based One-Time Password - [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238)) into Yii3 applications.

Requirements
------------

[](#requirements)

- PHP 8.2 or higher.

Installation
------------

[](#installation)

```
composer require beastbytes/yii-otp
```

or add the following to the 'require' section composer.json:

```
"beastbytes/yii-otp": ""
```

Usage
-----

[](#usage)

The application interacts with either `HtopService` or `TotpService`.

**NOTE:** Code examples show only the core functionality; they do not show dependency injections, support methods, etc.

### Configuration

[](#configuration)

The default configuration is for **TOTP** and is compatible with authenticator apps such as Google Authenticator, Aegis, etc.

### Enable OTP

[](#enable-otp)

```
// Otp Controller
public function enable(
    CurrentUser $currentUser,
    FormHydrator $formHydrator,
    ServerRequestInterface $request,
): ResponseInterface
{
    $formModel = new OtpForm($this->otpService);

    if ($formHydrator->populateFromPostAndValidate($formModel, $request)) {
        $this->redirct('ShowBackupCodes');
    }

    ['qrCode', 'secret'] = $this->otpService->createOtp($currentUser->getId());

    return $this->viewRenderer->render(
        'enable2faView',
        [
            'formModel' => $formModel,
            'qrCode' => $qrCode,
            'secret' => $secret,
        ]
    );
}
```

```
// enable OTP View
Either scan the QR Code or manually enter the 2FA code into
    your 2FA app, then enter the  OTP code generated by the app.
