Newer
Older
namespace QUI\Auth\Google2Fa;
use QUI;
use PragmaRX\Google2FA\Google2FA;
use QUI\Users\AuthInterface;
use QUI\Users\User;
use QUI\Auth\Google2Fa\Exception as Google2FaException;
use QUI\Security;
/**
* Class Auth
*
* Authentication handler for Google Authenticator
*
* @package QUI\Authe\Google2Fa
*/
class Auth implements AuthInterface
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* Google2FA class
*
* @var Google2FA
*/
protected $Google2FA = null;
/**
* User that is to be authenticated
*
* @var User
*/
protected $User = null;
/**
* Auth Constructor.
*
* @param string|array|integer $user - name of the user, or user id
*
* @throws QUI\Auth\Google2Fa\Exception
*/
public function __construct($user = '')
{
$this->User = QUI::getUsers()->getUserByName($user);
$this->Google2FA = new Google2FA();
}
/**
* @param null|\QUI\Locale $Locale
* @return string
*/
public function getTitle($Locale = null)
{
if (is_null($Locale)) {
$Locale = QUI::getLocale();
}
return $Locale->get('quiqqer/authgoogle2fa', 'google2fa.title');
}
/**
* Authenticate the user
*
*
* @throws QUI\Auth\Google2Fa\Exception
*/
if (!is_array($authData)
|| !isset($authData['code'])
) {
throw new Google2FaException(array(
'quiqqer/authgoogle2fa',
'exception.auth.wrong.auth.code'
));
}
$authCode = $authData['code'];
$authSecrets = json_decode($this->User->getAttribute('quiqqer.auth.google2fa.secrets'), true);
// if no secret keys have been generated -> automatically authenticate the user
if (empty($authSecrets)) {
return;
}
foreach ($authSecrets as $k => $secretData) {
if ($this->Google2FA->verifyKey(trim($secretData['key']), $authCode)) {
return;
}
// if key did not work check for recovery keys
foreach ($secretData['recoveryKeys'] as $k2 => $recoveryKey) {
$recoveryKey = trim(Security::decrypt($recoveryKey));
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
continue;
}
// remove recovery key from list indefinitely
unset($secretData['recoveryKeys'][$k2]);
$authSecrets[$k] = $secretData;
$this->User->setAttribute('quiqqer.auth.google2fa.secrets', json_encode($authSecrets));
$this->User->save();
return;
}
}
throw new Google2FaException(array(
'quiqqer/authgoogle2fa',
'exception.auth.wrong.auth.code'
));
}
/**
* Return the user object
*
* @return \QUI\Interfaces\Users\User
*/
public function getUser()
{
return $this->User;
}
/**
* Return the quiqqer user id
*
* @return integer|boolean
*/
public function getUserId()
{
return $this->User->getId();
}
/**
* Generate 16-bit (encrypted) recovery keys as alternative logins
*
* @param int $count (optional) - number of key [default: 10]
* @return array
*/
public static function generateRecoveryKeys($count = 10)
{
$recoveryKeys = array();
$Google2FA = new Google2FA();
for ($i = 0; $i < $count; $i++) {
$recoveryKeys[] = Security::encrypt(mb_substr(md5($Google2FA->generateSecretKey(16)), 0, 10));
}
return $recoveryKeys;
}
/**
* @return \QUI\Control
*/
public static function getLoginControl()
{
return new QUI\Auth\Google2Fa\Controls\Login();
}
/**
* @return \QUI\Control
*/
public static function getRegisterControl()
/**
* @return \QUI\Control
*/
public static function getSettingsControl()
{
return new QUI\Auth\Google2Fa\Controls\Settings();
}
/**
* @return \QUI\Control
*/
public static function getPasswordResetControl()
{
return null;