set double authentication : create, send link, usable with FreeOTP (opensource) AEgis (opensource) and Google Authenticator 1. OTP : prevent direct access without going throught the 2FA mechanism 2. Go back to the url where you were disconnected 3. Cosmetic
73 lines
1.9 KiB
PHP
73 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Noalyss;
|
|
|
|
/*
|
|
* This file is part of NOALYSS.
|
|
*
|
|
* NOALYSS is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* NOALYSS is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with NOALYSS; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*/
|
|
// Copyright Author Dany De Bontridder danydb@aevalys.eu 22/10/23
|
|
|
|
|
|
/**
|
|
* @file
|
|
* @brief class using OTP
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* @brief generate OTP
|
|
*/
|
|
use chillerlan\Authenticator\{
|
|
Authenticator,
|
|
AuthenticatorOptions
|
|
};
|
|
use chillerlan\Authenticator\Authenticators\AuthenticatorInterface;
|
|
|
|
class OTP {
|
|
|
|
private $authenticator;
|
|
|
|
function __construct() {
|
|
$options = new AuthenticatorOptions;
|
|
$options->secret_length = 32;
|
|
$options->algorithm = AuthenticatorInterface::ALGO_SHA512;
|
|
$options->digits=6;
|
|
$this->authenticator = new Authenticator($options);
|
|
}
|
|
/**
|
|
* @brief build a secret key and returns it
|
|
* @return string random string of 32
|
|
*/
|
|
function build_secret() {
|
|
|
|
$secret = $this->authenticator->createSecret();
|
|
return $secret;
|
|
|
|
}
|
|
|
|
/**
|
|
* @brief compute a code for auth. for the user passed in parameter
|
|
* @param $user (\Noalyss_User)
|
|
*/
|
|
function compute_code($secret)
|
|
{
|
|
|
|
$this->authenticator->setSecret($secret);
|
|
return $this->authenticator->code();
|
|
}
|
|
|
|
}
|