10
10
use Magento \Backend \Model \Auth \Session ;
11
11
use Magento \Backend \App \Action ;
12
12
use Magento \Framework \App \Action \HttpGetActionInterface ;
13
+ use Magento \Framework \Controller \Result \RedirectFactory ;
14
+ use Magento \Framework \Message \ManagerInterface ;
13
15
use Magento \Framework \View \Result \PageFactory ;
14
16
use Magento \TwoFactorAuth \Api \TfaInterface ;
15
17
use Magento \TwoFactorAuth \Api \UserConfigManagerInterface ;
@@ -48,28 +50,46 @@ class Auth extends AbstractAction implements HttpGetActionInterface
48
50
*/
49
51
private $ tokenVerifier ;
50
52
53
+ /**
54
+ * @var DuoSecurity
55
+ */
56
+ private $ duoSecurity ;
57
+ /**
58
+ * @var ManagerInterface
59
+ */
60
+ protected $ messageManager ;
61
+ /**
62
+ * @var RedirectFactory
63
+ */
64
+ protected $ resultRedirectFactory ;
65
+
51
66
/**
52
67
* @param Action\Context $context
53
68
* @param Session $session
54
69
* @param PageFactory $pageFactory
55
70
* @param UserConfigManagerInterface $userConfigManager
56
71
* @param TfaInterface $tfa
57
72
* @param HtmlAreaTokenVerifier $tokenVerifier
73
+ * @param DuoSecurity $duoSecurity
58
74
*/
59
75
public function __construct (
60
76
Action \Context $ context ,
61
77
Session $ session ,
62
78
PageFactory $ pageFactory ,
63
79
UserConfigManagerInterface $ userConfigManager ,
64
80
TfaInterface $ tfa ,
65
- HtmlAreaTokenVerifier $ tokenVerifier
81
+ HtmlAreaTokenVerifier $ tokenVerifier ,
82
+ DuoSecurity $ duoSecurity
66
83
) {
67
84
parent ::__construct ($ context );
68
85
$ this ->tfa = $ tfa ;
69
86
$ this ->session = $ session ;
70
87
$ this ->pageFactory = $ pageFactory ;
71
88
$ this ->userConfigManager = $ userConfigManager ;
72
89
$ this ->tokenVerifier = $ tokenVerifier ;
90
+ $ this ->duoSecurity = $ duoSecurity ;
91
+ $ this ->messageManager = $ context ->getMessageManager ();
92
+ $ this ->resultRedirectFactory = $ context ->getResultRedirectFactory ();
73
93
}
74
94
75
95
/**
@@ -87,8 +107,31 @@ private function getUser()
87
107
*/
88
108
public function execute ()
89
109
{
110
+ $ user = $ this ->getUser ();
111
+ if (!$ user ) {
112
+ $ this ->messageManager ->addErrorMessage (__ ('User session not found. ' ));
113
+ }
90
114
$ this ->userConfigManager ->setDefaultProvider ((int )$ this ->getUser ()->getId (), DuoSecurity::CODE );
91
- return $ this ->pageFactory ->create ();
115
+
116
+ $ username = $ this ->getUser ()->getUserName ();
117
+ $ state = $ this ->duoSecurity ->generateDuoState ();
118
+ $ this ->session ->setDuoState ($ state );
119
+ $ response = $ this ->duoSecurity ->initiateAuth ($ username , $ state );
120
+ if ($ response ['status ' ] === 'open ' ) {
121
+ // If fail mode is "open", skip the Duo prompt.
122
+ $ this ->messageManager ->addErrorMessage ($ response ['message ' ]);
123
+ }
124
+ if ($ response ['status ' ] === 'closed ' ) {
125
+ // If fail mode is "closed", show an error message.
126
+ $ this ->messageManager ->addErrorMessage ($ response ['message ' ]);
127
+ }
128
+
129
+ $ resultPage = $ this ->pageFactory ->create ();
130
+ $ block = $ resultPage ->getLayout ()->getBlock ('content ' );
131
+ if ($ block ) {
132
+ $ block ->setData ('auth_url ' , $ response ['redirect_url ' ]);
133
+ }
134
+ return $ resultPage ;
92
135
}
93
136
94
137
/**
0 commit comments