forked from wilminator/dokuwiki-plugin-twofactor
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProvider.php
224 lines (196 loc) · 5.5 KB
/
Provider.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace dokuwiki\plugin\twofactor;
use dokuwiki\Extension\ActionPlugin;
use dokuwiki\Extension\AuthPlugin;
use dokuwiki\Form\Form;
use dokuwiki\Utf8\PhpString;
/**
* Baseclass for all second factor providers
*/
abstract class Provider extends ActionPlugin
{
/** @var Settings */
protected $settings;
/** @var string */
protected $providerID;
/** @inheritdoc */
public function register(\Doku_Event_Handler $controller)
{
$controller->register_hook(
'PLUGIN_TWOFACTOR_PROVIDER_REGISTER',
'AFTER',
$this,
'registerSelf',
null,
Manager::EVENT_PRIORITY - 1 // providers first
);
}
/**
* Register this class as a twofactor provider
*
* @param \Doku_Event $event
* @return void
*/
public function registerSelf(\Doku_Event $event)
{
$event->data[$this->getProviderID()] = $this;
}
/**
* Initializes the provider for the given user
* @param string $user Current user
*/
public function init($user)
{
$this->settings = new Settings($this->getProviderID(), $user);
}
// region Introspection methods
/**
* The user data for the current user
* @return array (user=>'login', name=>'full name', mail=>'[email protected]', grps=>['group1', 'group2',...])
*/
public function getUserData()
{
/** @var AuthPlugin $auth */
global $auth;
$user = $this->settings->getUser();
$userdata = $auth->getUserData($user);
if (!$userdata) throw new \RuntimeException('2fa: Failed to get user details from auth backend');
$userdata['user'] = $user;
return $userdata;
}
/**
* The ID of this provider
*
* @return string
*/
public function getProviderID()
{
if (!$this->providerID) {
$this->providerID = $this->getPluginName();
}
return $this->providerID;
}
/**
* Pretty Label for this provider
*
* @return string
*/
public function getLabel()
{
return PhpString::ucfirst($this->providerID);
}
// endregion
// region Configuration methods
/**
* Clear all settings
*/
public function reset()
{
$this->settings->purge();
}
/**
* Has this provider been fully configured and verified by the user and thus can be used
* for authentication?
*
* @return bool
*/
abstract public function isConfigured();
/**
* Render the configuration form
*
* This method should add the needed form elements to (re)configure the provider.
* The contents of the form may change depending on the current settings.
*
* No submit button should be added - this is handled by the main plugin.
*
* @param Form $form The initial form to add elements to
* @return Form
*/
abstract public function renderProfileForm(Form $form);
/**
* Handle any input data
*
* @return void
*/
abstract public function handleProfileForm();
// endregion
// region OTP methods
/**
* Create and store a new secret for this provider
*
* @return string the new secret
* @throws \Exception when no suitable random source is available
*/
public function initSecret()
{
$ga = new GoogleAuthenticator();
$secret = $ga->createSecret();
$this->settings->set('secret', $secret);
return $secret;
}
/**
* Get the secret for this provider
*
* @return string
*/
public function getSecret()
{
return $this->settings->get('secret');
}
/**
* Generate an auth code
*
* @return string
* @throws \Exception when no code can be created
*/
public function generateCode()
{
$secret = $this->settings->get('secret');
if (!$secret) throw new \Exception('No secret for provider ' . $this->getProviderID());
$ga = new GoogleAuthenticator();
return $ga->getCode($secret);
}
/**
* Check the given code
*
* @param string $code
* @param bool $usermessage should a message about the failed code be shown to the user?
* @return bool
* @throws \RuntimeException when no code can be created
*/
public function checkCode($code, $usermessage = true)
{
$secret = $this->settings->get('secret');
if (!$secret) throw new \RuntimeException('No secret for provider ' . $this->getProviderID());
$ga = new GoogleAuthenticator();
$ok = $ga->verifyCode($secret, $code, $this->getTolerance());
if (!$ok && $usermessage) {
msg((Manager::getInstance())->getLang('codefail'), -1);
}
return $ok;
}
/**
* The tolerance to be used when verifying codes
*
* This is the allowed time drift in 30 second units (8 means 4 minutes before or after)
* Different providers may want to use different tolerances by overriding this method.
*
* @return int
*/
public function getTolerance()
{
return 2;
}
/**
* Transmits the code to the user
*
* If a provider does not transmit anything (eg. TOTP) simply
* return the message.
*
* @param string $code The code to transmit
* @return string Informational message for the user
* @throw \Exception when the message can't be sent
*/
abstract public function transmitMessage($code);
// endregion
}