1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2020 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \TwoFactorAuth \Controller \Adminhtml \Duo ;
9
10
10
11
use Magento \Backend \Model \Auth \Session ;
11
12
use Magento \Backend \App \Action ;
12
13
use Magento \Framework \App \Action \HttpGetActionInterface ;
14
+ use Magento \Framework \Controller \Result \RedirectFactory ;
15
+ use Magento \Framework \Message \ManagerInterface ;
13
16
use Magento \Framework \View \Result \PageFactory ;
14
17
use Magento \TwoFactorAuth \Api \TfaInterface ;
15
18
use Magento \TwoFactorAuth \Api \UserConfigManagerInterface ;
@@ -48,28 +51,46 @@ class Auth extends AbstractAction implements HttpGetActionInterface
48
51
*/
49
52
private $ tokenVerifier ;
50
53
54
+ /**
55
+ * @var DuoSecurity
56
+ */
57
+ private $ duoSecurity ;
58
+ /**
59
+ * @var ManagerInterface
60
+ */
61
+ protected $ messageManager ;
62
+ /**
63
+ * @var RedirectFactory
64
+ */
65
+ protected $ resultRedirectFactory ;
66
+
51
67
/**
52
68
* @param Action\Context $context
53
69
* @param Session $session
54
70
* @param PageFactory $pageFactory
55
71
* @param UserConfigManagerInterface $userConfigManager
56
72
* @param TfaInterface $tfa
57
73
* @param HtmlAreaTokenVerifier $tokenVerifier
74
+ * @param DuoSecurity $duoSecurity
58
75
*/
59
76
public function __construct (
60
77
Action \Context $ context ,
61
78
Session $ session ,
62
79
PageFactory $ pageFactory ,
63
80
UserConfigManagerInterface $ userConfigManager ,
64
81
TfaInterface $ tfa ,
65
- HtmlAreaTokenVerifier $ tokenVerifier
82
+ HtmlAreaTokenVerifier $ tokenVerifier ,
83
+ DuoSecurity $ duoSecurity
66
84
) {
67
85
parent ::__construct ($ context );
68
86
$ this ->tfa = $ tfa ;
69
87
$ this ->session = $ session ;
70
88
$ this ->pageFactory = $ pageFactory ;
71
89
$ this ->userConfigManager = $ userConfigManager ;
72
90
$ this ->tokenVerifier = $ tokenVerifier ;
91
+ $ this ->duoSecurity = $ duoSecurity ;
92
+ $ this ->messageManager = $ context ->getMessageManager ();
93
+ $ this ->resultRedirectFactory = $ context ->getResultRedirectFactory ();
73
94
}
74
95
75
96
/**
@@ -87,8 +108,27 @@ private function getUser()
87
108
*/
88
109
public function execute ()
89
110
{
111
+ $ user = $ this ->getUser ();
112
+ if (!$ user ) {
113
+ $ this ->messageManager ->addErrorMessage (__ ('User session not found. ' ));
114
+ }
90
115
$ this ->userConfigManager ->setDefaultProvider ((int )$ this ->getUser ()->getId (), DuoSecurity::CODE );
91
- return $ this ->pageFactory ->create ();
116
+
117
+ $ username = $ this ->getUser ()->getUserName ();
118
+ $ state = $ this ->duoSecurity ->generateDuoState ();
119
+ $ this ->session ->setDuoState ($ state );
120
+ $ response = $ this ->duoSecurity ->initiateAuth ($ username , $ state );
121
+ if ($ response ['status ' ] === 'failure ' ) {
122
+ // if health check fails, skip the Duo prompt and choose different 2FA.
123
+ $ this ->messageManager ->addErrorMessage ($ response ['message ' ]);
124
+ }
125
+
126
+ $ resultPage = $ this ->pageFactory ->create ();
127
+ $ block = $ resultPage ->getLayout ()->getBlock ('content ' );
128
+ if ($ block ) {
129
+ $ block ->setData ('auth_url ' , $ response ['redirect_url ' ]);
130
+ }
131
+ return $ resultPage ;
92
132
}
93
133
94
134
/**
0 commit comments