Skip to content
This repository has been archived by the owner on Dec 2, 2021. It is now read-only.

Latest commit

 

History

History
54 lines (43 loc) · 1.37 KB

backup_codes.md

File metadata and controls

54 lines (43 loc) · 1.37 KB

Backup Codes

Backup codes are one-time authentication codes, which can be used instead of the actual codes. They're meant as emergency codes, when the authentication device is not available and you have to pass the two-factor authentication process.

Backup codes have to be made available via the user object. To enable the feature, the user entity has to implement Scheb\TwoFactorBundle\Model\BackupCodeInterface. Here's an example:

namespace Acme\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\BackupCodeInterface;

class User implements BackupCodeInterface
{

    /**
     * @ORM\Column(type="json_array")
     */
    private $backupCodes;

    // [...]

    /**
     * Check if it is a valid backup code
     *
     * @param string $code
     * @return boolean
     */
    public function isBackupCode($code)
    {
        return in_array($code, $this->backupCodes);
    }

    /**
     * Invalidate a backup code
     *
     * @param string $code
     */
    public function invalidateBackupCode($code)
    {
        $key = array_search($code, $this->backupCodes);
        if($key !== false){
            unset($this->backupCodes[$key]);
        }
    }
}

The example assumes that there are already codes generated for that user. In addition to this, you should implement the backup code (re-)generation like you prefer it.