1010use Magento \Backend \Model \Auth \Session ;
1111use Magento \Backend \App \Action ;
1212use Magento \Framework \App \Action \HttpGetActionInterface ;
13+ use Magento \Framework \Controller \Result \RedirectFactory ;
14+ use Magento \Framework \Message \ManagerInterface ;
1315use Magento \Framework \View \Result \PageFactory ;
1416use Magento \TwoFactorAuth \Api \TfaInterface ;
1517use Magento \TwoFactorAuth \Api \UserConfigManagerInterface ;
@@ -48,28 +50,46 @@ class Auth extends AbstractAction implements HttpGetActionInterface
4850 */
4951 private $ tokenVerifier ;
5052
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+
5166 /**
5267 * @param Action\Context $context
5368 * @param Session $session
5469 * @param PageFactory $pageFactory
5570 * @param UserConfigManagerInterface $userConfigManager
5671 * @param TfaInterface $tfa
5772 * @param HtmlAreaTokenVerifier $tokenVerifier
73+ * @param DuoSecurity $duoSecurity
5874 */
5975 public function __construct (
6076 Action \Context $ context ,
6177 Session $ session ,
6278 PageFactory $ pageFactory ,
6379 UserConfigManagerInterface $ userConfigManager ,
6480 TfaInterface $ tfa ,
65- HtmlAreaTokenVerifier $ tokenVerifier
81+ HtmlAreaTokenVerifier $ tokenVerifier ,
82+ DuoSecurity $ duoSecurity
6683 ) {
6784 parent ::__construct ($ context );
6885 $ this ->tfa = $ tfa ;
6986 $ this ->session = $ session ;
7087 $ this ->pageFactory = $ pageFactory ;
7188 $ this ->userConfigManager = $ userConfigManager ;
7289 $ this ->tokenVerifier = $ tokenVerifier ;
90+ $ this ->duoSecurity = $ duoSecurity ;
91+ $ this ->messageManager = $ context ->getMessageManager ();
92+ $ this ->resultRedirectFactory = $ context ->getResultRedirectFactory ();
7393 }
7494
7595 /**
@@ -87,8 +107,31 @@ private function getUser()
87107 */
88108 public function execute ()
89109 {
110+ $ user = $ this ->getUser ();
111+ if (!$ user ) {
112+ $ this ->messageManager ->addErrorMessage (__ ('User session not found. ' ));
113+ }
90114 $ 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 ;
92135 }
93136
94137 /**
0 commit comments