@@ -37,147 +37,147 @@ |
||
37 | 37 | |
38 | 38 | class TwoFactorChallengeController extends Controller { |
39 | 39 | |
40 | - /** @var Manager */ |
|
41 | - private $twoFactorManager; |
|
42 | - |
|
43 | - /** @var IUserSession */ |
|
44 | - private $userSession; |
|
45 | - |
|
46 | - /** @var ISession */ |
|
47 | - private $session; |
|
48 | - |
|
49 | - /** @var IURLGenerator */ |
|
50 | - private $urlGenerator; |
|
51 | - |
|
52 | - /** |
|
53 | - * @param string $appName |
|
54 | - * @param IRequest $request |
|
55 | - * @param Manager $twoFactorManager |
|
56 | - * @param IUserSession $userSession |
|
57 | - * @param ISession $session |
|
58 | - * @param IURLGenerator $urlGenerator |
|
59 | - */ |
|
60 | - public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, |
|
61 | - ISession $session, IURLGenerator $urlGenerator) { |
|
62 | - parent::__construct($appName, $request); |
|
63 | - $this->twoFactorManager = $twoFactorManager; |
|
64 | - $this->userSession = $userSession; |
|
65 | - $this->session = $session; |
|
66 | - $this->urlGenerator = $urlGenerator; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @return string |
|
71 | - */ |
|
72 | - protected function getLogoutAttribute() { |
|
73 | - return OC_User::getLogoutAttribute(); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @NoAdminRequired |
|
78 | - * @NoCSRFRequired |
|
79 | - * |
|
80 | - * @param string $redirect_url |
|
81 | - * @return TemplateResponse |
|
82 | - */ |
|
83 | - public function selectChallenge($redirect_url) { |
|
84 | - $user = $this->userSession->getUser(); |
|
85 | - $providers = $this->twoFactorManager->getProviders($user); |
|
86 | - $backupProvider = $this->twoFactorManager->getBackupProvider($user); |
|
87 | - |
|
88 | - $data = [ |
|
89 | - 'providers' => $providers, |
|
90 | - 'backupProvider' => $backupProvider, |
|
91 | - 'redirect_url' => $redirect_url, |
|
92 | - 'logout_attribute' => $this->getLogoutAttribute(), |
|
93 | - ]; |
|
94 | - return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest'); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @NoAdminRequired |
|
99 | - * @NoCSRFRequired |
|
100 | - * @UseSession |
|
101 | - * |
|
102 | - * @param string $challengeProviderId |
|
103 | - * @param string $redirect_url |
|
104 | - * @return TemplateResponse|RedirectResponse |
|
105 | - */ |
|
106 | - public function showChallenge($challengeProviderId, $redirect_url) { |
|
107 | - $user = $this->userSession->getUser(); |
|
108 | - $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
109 | - if (is_null($provider)) { |
|
110 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
111 | - } |
|
112 | - |
|
113 | - $backupProvider = $this->twoFactorManager->getBackupProvider($user); |
|
114 | - if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) { |
|
115 | - // Don't show the backup provider link if we're already showing that provider's challenge |
|
116 | - $backupProvider = null; |
|
117 | - } |
|
118 | - |
|
119 | - $errorMessage = ''; |
|
120 | - $error = false; |
|
121 | - if ($this->session->exists('two_factor_auth_error')) { |
|
122 | - $this->session->remove('two_factor_auth_error'); |
|
123 | - $error = true; |
|
124 | - $errorMessage = $this->session->get("two_factor_auth_error_message"); |
|
125 | - $this->session->remove('two_factor_auth_error_message'); |
|
126 | - } |
|
127 | - $tmpl = $provider->getTemplate($user); |
|
128 | - $tmpl->assign('redirect_url', $redirect_url); |
|
129 | - $data = [ |
|
130 | - 'error' => $error, |
|
131 | - 'error_message' => $errorMessage, |
|
132 | - 'provider' => $provider, |
|
133 | - 'backupProvider' => $backupProvider, |
|
134 | - 'logout_attribute' => $this->getLogoutAttribute(), |
|
135 | - 'redirect_url' => $redirect_url, |
|
136 | - 'template' => $tmpl->fetchPage(), |
|
137 | - ]; |
|
138 | - return new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest'); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @NoAdminRequired |
|
143 | - * @NoCSRFRequired |
|
144 | - * @UseSession |
|
145 | - * |
|
146 | - * @UserRateThrottle(limit=5, period=100) |
|
147 | - * |
|
148 | - * @param string $challengeProviderId |
|
149 | - * @param string $challenge |
|
150 | - * @param string $redirect_url |
|
151 | - * @return RedirectResponse |
|
152 | - */ |
|
153 | - public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) { |
|
154 | - $user = $this->userSession->getUser(); |
|
155 | - $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
156 | - if (is_null($provider)) { |
|
157 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
158 | - } |
|
159 | - |
|
160 | - try { |
|
161 | - if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) { |
|
162 | - if (!is_null($redirect_url)) { |
|
163 | - return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url))); |
|
164 | - } |
|
165 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
166 | - } |
|
167 | - } catch (TwoFactorException $e) { |
|
168 | - /* |
|
40 | + /** @var Manager */ |
|
41 | + private $twoFactorManager; |
|
42 | + |
|
43 | + /** @var IUserSession */ |
|
44 | + private $userSession; |
|
45 | + |
|
46 | + /** @var ISession */ |
|
47 | + private $session; |
|
48 | + |
|
49 | + /** @var IURLGenerator */ |
|
50 | + private $urlGenerator; |
|
51 | + |
|
52 | + /** |
|
53 | + * @param string $appName |
|
54 | + * @param IRequest $request |
|
55 | + * @param Manager $twoFactorManager |
|
56 | + * @param IUserSession $userSession |
|
57 | + * @param ISession $session |
|
58 | + * @param IURLGenerator $urlGenerator |
|
59 | + */ |
|
60 | + public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, |
|
61 | + ISession $session, IURLGenerator $urlGenerator) { |
|
62 | + parent::__construct($appName, $request); |
|
63 | + $this->twoFactorManager = $twoFactorManager; |
|
64 | + $this->userSession = $userSession; |
|
65 | + $this->session = $session; |
|
66 | + $this->urlGenerator = $urlGenerator; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @return string |
|
71 | + */ |
|
72 | + protected function getLogoutAttribute() { |
|
73 | + return OC_User::getLogoutAttribute(); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @NoAdminRequired |
|
78 | + * @NoCSRFRequired |
|
79 | + * |
|
80 | + * @param string $redirect_url |
|
81 | + * @return TemplateResponse |
|
82 | + */ |
|
83 | + public function selectChallenge($redirect_url) { |
|
84 | + $user = $this->userSession->getUser(); |
|
85 | + $providers = $this->twoFactorManager->getProviders($user); |
|
86 | + $backupProvider = $this->twoFactorManager->getBackupProvider($user); |
|
87 | + |
|
88 | + $data = [ |
|
89 | + 'providers' => $providers, |
|
90 | + 'backupProvider' => $backupProvider, |
|
91 | + 'redirect_url' => $redirect_url, |
|
92 | + 'logout_attribute' => $this->getLogoutAttribute(), |
|
93 | + ]; |
|
94 | + return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest'); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @NoAdminRequired |
|
99 | + * @NoCSRFRequired |
|
100 | + * @UseSession |
|
101 | + * |
|
102 | + * @param string $challengeProviderId |
|
103 | + * @param string $redirect_url |
|
104 | + * @return TemplateResponse|RedirectResponse |
|
105 | + */ |
|
106 | + public function showChallenge($challengeProviderId, $redirect_url) { |
|
107 | + $user = $this->userSession->getUser(); |
|
108 | + $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
109 | + if (is_null($provider)) { |
|
110 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
111 | + } |
|
112 | + |
|
113 | + $backupProvider = $this->twoFactorManager->getBackupProvider($user); |
|
114 | + if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) { |
|
115 | + // Don't show the backup provider link if we're already showing that provider's challenge |
|
116 | + $backupProvider = null; |
|
117 | + } |
|
118 | + |
|
119 | + $errorMessage = ''; |
|
120 | + $error = false; |
|
121 | + if ($this->session->exists('two_factor_auth_error')) { |
|
122 | + $this->session->remove('two_factor_auth_error'); |
|
123 | + $error = true; |
|
124 | + $errorMessage = $this->session->get("two_factor_auth_error_message"); |
|
125 | + $this->session->remove('two_factor_auth_error_message'); |
|
126 | + } |
|
127 | + $tmpl = $provider->getTemplate($user); |
|
128 | + $tmpl->assign('redirect_url', $redirect_url); |
|
129 | + $data = [ |
|
130 | + 'error' => $error, |
|
131 | + 'error_message' => $errorMessage, |
|
132 | + 'provider' => $provider, |
|
133 | + 'backupProvider' => $backupProvider, |
|
134 | + 'logout_attribute' => $this->getLogoutAttribute(), |
|
135 | + 'redirect_url' => $redirect_url, |
|
136 | + 'template' => $tmpl->fetchPage(), |
|
137 | + ]; |
|
138 | + return new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest'); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @NoAdminRequired |
|
143 | + * @NoCSRFRequired |
|
144 | + * @UseSession |
|
145 | + * |
|
146 | + * @UserRateThrottle(limit=5, period=100) |
|
147 | + * |
|
148 | + * @param string $challengeProviderId |
|
149 | + * @param string $challenge |
|
150 | + * @param string $redirect_url |
|
151 | + * @return RedirectResponse |
|
152 | + */ |
|
153 | + public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) { |
|
154 | + $user = $this->userSession->getUser(); |
|
155 | + $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
156 | + if (is_null($provider)) { |
|
157 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
158 | + } |
|
159 | + |
|
160 | + try { |
|
161 | + if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) { |
|
162 | + if (!is_null($redirect_url)) { |
|
163 | + return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url))); |
|
164 | + } |
|
165 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
166 | + } |
|
167 | + } catch (TwoFactorException $e) { |
|
168 | + /* |
|
169 | 169 | * The 2FA App threw an TwoFactorException. Now we display more |
170 | 170 | * information to the user. The exception text is stored in the |
171 | 171 | * session to be used in showChallenge() |
172 | 172 | */ |
173 | - $this->session->set('two_factor_auth_error_message', $e->getMessage()); |
|
174 | - } |
|
175 | - |
|
176 | - $this->session->set('two_factor_auth_error', true); |
|
177 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [ |
|
178 | - 'challengeProviderId' => $provider->getId(), |
|
179 | - 'redirect_url' => $redirect_url, |
|
180 | - ])); |
|
181 | - } |
|
173 | + $this->session->set('two_factor_auth_error_message', $e->getMessage()); |
|
174 | + } |
|
175 | + |
|
176 | + $this->session->set('two_factor_auth_error', true); |
|
177 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [ |
|
178 | + 'challengeProviderId' => $provider->getId(), |
|
179 | + 'redirect_url' => $redirect_url, |
|
180 | + ])); |
|
181 | + } |
|
182 | 182 | |
183 | 183 | } |