10
10
11
11
namespace hiqdev \yii2 \mfa \controllers ;
12
12
13
+ use hiqdev \yii2 \mfa \base \ApiMfaIdentityInterface ;
13
14
use hiqdev \yii2 \mfa \base \MfaIdentityInterface ;
15
+ use hiqdev \yii2 \mfa \behaviors \OauthLoginBehavior ;
14
16
use hiqdev \yii2 \mfa \forms \InputForm ;
15
17
use Yii ;
16
18
use yii \filters \AccessControl ;
19
+ use yii \filters \ContentNegotiator ;
20
+ use yii \filters \VerbFilter ;
21
+ use yii \web \Response ;
17
22
18
23
/**
19
24
* TOTP controller.
20
25
* Time-based One Time Password.
21
26
*/
22
27
class TotpController extends \yii \web \Controller
23
28
{
29
+ public $ enableCsrfValidation = false ;
30
+
24
31
public function behaviors ()
25
32
{
26
33
return array_merge (parent ::behaviors (), [
34
+ 'filterApi ' => [
35
+ 'class ' => OauthLoginBehavior::class,
36
+ 'only ' => ['api-temporary-secret ' , 'api-disable ' , 'api-enable ' ],
37
+ ],
27
38
'access ' => [
28
39
'class ' => AccessControl::class,
29
40
'denyCallback ' => [$ this , 'denyCallback ' ],
@@ -36,12 +47,27 @@ public function behaviors()
36
47
],
37
48
// @ - authenticated
38
49
[
39
- 'actions ' => ['enable ' , 'disable ' , 'toggle ' ],
50
+ 'actions ' => ['enable ' , 'disable ' , 'toggle ' , ' api-temporary-secret ' , ' api-disable ' , ' api-enable ' ],
40
51
'roles ' => ['@ ' ],
41
52
'allow ' => true ,
42
53
],
43
54
],
44
55
],
56
+ 'verbFilter ' => [
57
+ 'class ' => VerbFilter::class,
58
+ 'actions ' => [
59
+ 'api-temporary-secret ' => ['POST ' ],
60
+ 'api-enable ' => ['POST ' ],
61
+ 'api-disable ' => ['POST ' ],
62
+ ],
63
+ ],
64
+ 'contentNegotiator ' => [
65
+ 'class ' => ContentNegotiator::class,
66
+ 'only ' => ['api-temporary-secret ' , 'api-disable ' , 'api-enable ' ],
67
+ 'formats ' => [
68
+ 'application/json ' => Response::FORMAT_JSON ,
69
+ ],
70
+ ],
45
71
]);
46
72
}
47
73
@@ -159,4 +185,54 @@ public function goBack($defaultUrl = null)
159
185
160
186
return parent ::goBack ($ defaultUrl );
161
187
}
188
+
189
+ public function actionApiEnable ()
190
+ {
191
+ /** @var ApiMfaIdentityInterface $identity */
192
+ $ identity = \Yii::$ app ->user ->identity ;
193
+ $ secret = $ identity ->getTotpSecret ();
194
+ if (!empty ($ secret )) {
195
+ return ['_error ' => 'mfa already enabled ' . $ secret ];
196
+ }
197
+
198
+ if (!$ this ->module ->getTotp ()->verifyCode ($ identity ->getTemporarySecret (), $ this ->request ->post ('code ' , '' ))) {
199
+ return ['_error ' => 'invalid totp code ' ];
200
+ }
201
+
202
+ $ identity ->setTotpSecret ($ identity ->getTemporarySecret ());
203
+ $ identity ->setTemporarySecret (null );
204
+ $ identity ->save ();
205
+
206
+ return ['id ' => $ identity ->getId ()];
207
+ }
208
+
209
+ public function actionApiDisable ()
210
+ {
211
+ /** @var ApiMfaIdentityInterface $identity */
212
+ $ identity = \Yii::$ app ->user ->identity ;
213
+ $ secret = $ identity ->getTotpSecret ();
214
+ if (empty ($ secret )) {
215
+ return ['_error ' => 'mfa disabled, enable first ' ];
216
+ }
217
+
218
+ if (!$ this ->module ->getTotp ()->verifyCode ($ secret , $ this ->request ->post ('code ' , '' ))) {
219
+ return ['_error ' => 'invalid totp code ' ];
220
+ }
221
+
222
+ $ identity ->setTotpSecret ('' );
223
+ $ identity ->save ();
224
+
225
+ return ['id ' => $ identity ->getId ()];
226
+ }
227
+
228
+ public function actionApiTemporarySecret ()
229
+ {
230
+ /** @var ApiMfaIdentityInterface $identity */
231
+ $ identity = \Yii::$ app ->user ->identity ;
232
+ $ secret = $ this ->module ->getTotp ()->getSecret ();
233
+ $ identity ->setTemporarySecret ($ secret );
234
+ $ identity ->save ();
235
+
236
+ return ['secret ' => $ secret ];
237
+ }
162
238
}
0 commit comments