From ef8e78084e7e5a544bb81c6ace717662c8277385 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 6 Apr 2018 11:48:01 +0200 Subject: [PATCH] app/controllers/AuthController.php: in oauthAction() use the new implementation of OAuth. --- app/controllers/AuthController.php | 31 ++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/app/controllers/AuthController.php b/app/controllers/AuthController.php index bc4edae..b3a84ba 100644 --- a/app/controllers/AuthController.php +++ b/app/controllers/AuthController.php @@ -40,16 +40,31 @@ class AuthController extends ControllerBase $this->view->form = $form; } - public function oauthAction() + public function oauthAction($provider_name) { - $response = $this->oauth->perform(); + $client = $this->getDI()->get('oauth', [ $provider_name ]); - if (is_array($response)) { - $this->auth->loginOauth($response['auth']); - $this->response->redirect('/'); - } else { - $this->flash->message('error', 'Failed to authenticate.'); - $this->response->redirect('/login'); + $code = $this->request->get('code'); + $state = $this->request->get('state'); + + // Have code. Authenticate and fetch data. + if (strlen($code) > 0) { + + try { + // NOTE: Should pass $state here also. + $data = $client->authenticate($code); + + $this->auth->loginOauth($data); + $this->response->redirect('/'); + } catch(\Exception $e) { + $this->flash->message('error', 'Failed to authenticate.'); + $this->response->redirect('/login'); + } + } + // No code + else { + // redirect to provider and acquire code. + $this->response->redirect($client->getAuthorizationUrl(), true); } }