forked from osTicket/osTicket-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.auth2fa.php
168 lines (135 loc) · 5.6 KB
/
class.auth2fa.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
167
168
<?php
require_once INCLUDE_DIR . 'class.export.php';
class Auth2FABackend extends TwoFactorAuthenticationBackend {
static $id = "auth.agent";
static $name = "Authenticator";
static $desc = /* @trans */ 'Verification codes are located in the Authenticator app of your choice on your phone';
static $custom_issuer;
var $secretKey;
protected function getSetupOptions() {
global $thisstaff;
$auth2FA = new Auth2FABackend;
$qrCodeURL = $auth2FA->getQRCode($thisstaff);
if ($auth2FA->validateQRCode($thisstaff)) {
return array(
'' => new FreeTextField(array(
'configuration' => array(
'content' => sprintf(
'<input type="hidden" name="email" value="%s" />
<em>Use an Authenticator application on your phone to scan
the QR Code below. If you lose the QR Code
on the app, you will need to have your 2FA configurations reset by
a helpdesk Administrator.</em>
</br>
<tr>
<td>
<img src="%s" alt="QR Code" />
</td>
</tr>',
$thisstaff->getEmail(), $qrCodeURL),
)
)),
);
}
}
protected function getInputOptions() {
return array(
'token' => new TextboxField(array(
'id'=>1, 'label'=>__('Verification Code'), 'required'=>true, 'default'=>'',
'validator'=>'number',
'hint'=>__('Please enter the code from your Authenticator app'),
'configuration'=>array(
'size'=>40, 'length'=>40,
'autocomplete' => 'one-time-code',
'inputmode' => 'numeric',
'pattern' => '[0-9]*',
'validator-error' => __('Invalid Code format'),
),
)),
);
}
function validate($form, $user) {
// Make sure form is valid and token exists
if (!($form->isValid()
&& ($clean=$form->getClean())
&& $clean['token']))
return false;
if (!$this->validateLoginCode($clean['token']))
return false;
// upstream validation might throw an exception due to expired token
// or too many attempts (timeout). It's the responsibility of the
// caller to catch and handle such exceptions.
$secretKey = $this->getSecretKey();
if (!$this->_validate($secretKey))
return false;
// Validator doesn't do house cleaning - it's our responsibility
$this->onValidate($user);
return true;
}
function send($user) {
global $cfg;
// Get backend configuration for this user
if (!$cfg || !($info = $user->get2FAConfig($this->getId())))
return false;
// get configuration
$config = $info['config'];
// Generate Secret Key
if (!$this->secretKey)
$this->secretKey = $this->getSecretKey($user);
$this->store($this->secretKey);
return true;
}
function store($secretKey) {
global $thisstaff;
$store = &$_SESSION['_2fa'][$this->getId()];
$store = ['otp' => $secretKey, 'time' => time(), 'strikes' => 0];
if ($thisstaff) {
$config = array('config' => array('key' => $secretKey, 'external2fa' => true));
$_config = new Config('staff.'.$thisstaff->getId());
$_config->set($this->getId(), JsonDataEncoder::encode($config));
$thisstaff->_config = $_config->getInfo();
$errors['err'] = '';
}
return $store;
}
function validateLoginCode($code) {
$auth2FA = new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
$secretKey = $this->getSecretKey();
return $auth2FA->checkCode($secretKey, $code);
}
function getSecretKey($staff=false) {
if (!$staff) {
$s = StaffAuthenticationBackend::getUser();
$staff = Staff::lookup($s->getId());
}
if (!$token = ConfigItem::getConfigsByNamespace('staff.'.$staff->getId(), static::$id)) {
$auth2FA = new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
$this->secretKey = $auth2FA->generateSecret();
$this->store($this->secretKey);
}
$key = $token->value ?: $this->secretKey;
if (strpos($key, 'config')) {
$key = json_decode($key, true);
$key = $key['config']['key'];
}
return $key;
}
function getQRCode($staff=false) {
$staffEmail = $staff->getEmail();
$secretKey = $this->getSecretKey($staff);
$title = preg_replace('/[^A-Za-z0-9]/', '', self::$custom_issuer ?: __('osTicket'));
return \Sonata\GoogleAuthenticator\GoogleQrUrl::generate($staffEmail, $secretKey, $title);
}
function validateQRCode($staff=false) {
$auth2FA = new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
$secretKey = $this->getSecretKey($staff);
$code = self::getCode();
return $auth2FA->checkCode($secretKey, $code);
}
static function getCode() {
$auth2FA = new \Sonata\GoogleAuthenticator\GoogleAuthenticator();
$self = new Auth2FABackend();
$secretKey = $self->getSecretKey();
return $auth2FA->getCode($secretKey);
}
}