1 | <?php |
||
34 | class TwoFactorChallengeController extends Controller { |
||
35 | |||
36 | /** @var Manager */ |
||
37 | private $twoFactorManager; |
||
38 | |||
39 | /** @var IUserSession */ |
||
40 | private $userSession; |
||
41 | |||
42 | /** @var ISession */ |
||
43 | private $session; |
||
44 | |||
45 | /** @var IURLGenerator */ |
||
46 | private $urlGenerator; |
||
47 | |||
48 | /** |
||
49 | * @param string $appName |
||
50 | * @param IRequest $request |
||
51 | * @param Manager $twoFactorManager |
||
52 | * @param IUserSession $userSession |
||
53 | * @param ISession $session |
||
54 | * @param IURLGenerator $urlGenerator |
||
55 | */ |
||
56 | public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, |
||
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | protected function getLogoutAttribute() { |
||
71 | |||
72 | /** |
||
73 | * @NoAdminRequired |
||
74 | * @NoCSRFRequired |
||
75 | * |
||
76 | * @param string $redirect_url |
||
77 | * @return TemplateResponse |
||
78 | */ |
||
79 | public function selectChallenge($redirect_url) { |
||
90 | |||
91 | /** |
||
92 | * @NoAdminRequired |
||
93 | * @NoCSRFRequired |
||
94 | * @UseSession |
||
95 | * |
||
96 | * @param string $challengeProviderId |
||
97 | * @param string $redirect_url |
||
98 | * @return TemplateResponse |
||
99 | */ |
||
100 | public function showChallenge($challengeProviderId, $redirect_url) { |
||
101 | $user = $this->userSession->getUser(); |
||
102 | $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
||
103 | if (is_null($provider)) { |
||
104 | return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
||
105 | } |
||
106 | |||
107 | if ($this->session->exists('two_factor_auth_error')) { |
||
108 | $this->session->remove('two_factor_auth_error'); |
||
109 | $error = true; |
||
110 | } else { |
||
111 | $error = false; |
||
112 | } |
||
113 | //Attempt to get custom ContentSecurityPolicy(CSP) from 2FA provider |
||
114 | if ($provider instanceof \OCP\Authentication\TwoFactorAuth\IProvider2) { |
||
115 | $csp = $provider->getCSP(); |
||
116 | } |
||
117 | $tmpl = $provider->getTemplate($user); |
||
118 | $tmpl->assign('redirect_url', $redirect_url); |
||
119 | $data = [ |
||
120 | 'error' => $error, |
||
121 | 'provider' => $provider, |
||
122 | 'logout_attribute' => $this->getLogoutAttribute(), |
||
123 | 'template' => $tmpl->fetchPage(), |
||
124 | ]; |
||
125 | //Generate the response and add the custom CSP (if defined) |
||
126 | $response = new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest'); |
||
127 | if (!is_null($csp)) { |
||
128 | $response->setContentSecurityPolicy($csp); |
||
129 | } |
||
130 | return $response; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @NoAdminRequired |
||
135 | * @NoCSRFRequired |
||
136 | * @UseSession |
||
137 | * |
||
138 | * @param string $challengeProviderId |
||
139 | * @param string $challenge |
||
140 | * @param string $redirect_url |
||
141 | * @return RedirectResponse |
||
142 | */ |
||
143 | public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) { |
||
163 | |||
164 | } |
||
165 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: