11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2020 Adobe
4+ * All Rights Reserved .
55 */
6+
67declare (strict_types=1 );
78
89namespace Magento \TwoFactorAuth \Controller \Adminhtml \Duo ;
910
1011use Magento \Backend \Model \Auth \Session ;
1112use Magento \Backend \App \Action ;
1213use Magento \Framework \App \Action \HttpGetActionInterface ;
14+ use Magento \Framework \Controller \Result \RedirectFactory ;
15+ use Magento \Framework \Message \ManagerInterface ;
1316use Magento \Framework \View \Result \PageFactory ;
1417use Magento \TwoFactorAuth \Api \TfaInterface ;
1518use Magento \TwoFactorAuth \Api \UserConfigManagerInterface ;
@@ -48,28 +51,46 @@ class Auth extends AbstractAction implements HttpGetActionInterface
4851 */
4952 private $ tokenVerifier ;
5053
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+
5167 /**
5268 * @param Action\Context $context
5369 * @param Session $session
5470 * @param PageFactory $pageFactory
5571 * @param UserConfigManagerInterface $userConfigManager
5672 * @param TfaInterface $tfa
5773 * @param HtmlAreaTokenVerifier $tokenVerifier
74+ * @param DuoSecurity $duoSecurity
5875 */
5976 public function __construct (
6077 Action \Context $ context ,
6178 Session $ session ,
6279 PageFactory $ pageFactory ,
6380 UserConfigManagerInterface $ userConfigManager ,
6481 TfaInterface $ tfa ,
65- HtmlAreaTokenVerifier $ tokenVerifier
82+ HtmlAreaTokenVerifier $ tokenVerifier ,
83+ DuoSecurity $ duoSecurity
6684 ) {
6785 parent ::__construct ($ context );
6886 $ this ->tfa = $ tfa ;
6987 $ this ->session = $ session ;
7088 $ this ->pageFactory = $ pageFactory ;
7189 $ this ->userConfigManager = $ userConfigManager ;
7290 $ this ->tokenVerifier = $ tokenVerifier ;
91+ $ this ->duoSecurity = $ duoSecurity ;
92+ $ this ->messageManager = $ context ->getMessageManager ();
93+ $ this ->resultRedirectFactory = $ context ->getResultRedirectFactory ();
7394 }
7495
7596 /**
@@ -87,8 +108,27 @@ private function getUser()
87108 */
88109 public function execute ()
89110 {
111+ $ user = $ this ->getUser ();
112+ if (!$ user ) {
113+ $ this ->messageManager ->addErrorMessage (__ ('User session not found. ' ));
114+ }
90115 $ 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 ;
92132 }
93133
94134 /**
0 commit comments