/
GenericOauth2TypeController.php
133 lines (117 loc) · 3.92 KB
/
GenericOauth2TypeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Concrete\Core\Authentication\Type\OAuth\OAuth2;
use Concrete\Core\Authentication\LoginException;
use Concrete\Core\Authentication\Type\OAuth\GenericOauthTypeController;
use Concrete\Core\Routing\RedirectResponse;
use OAuth\Common\Exception\Exception;
use OAuth\Common\Http\Exception\TokenResponseException;
use OAuth\OAuth2\Service\AbstractService;
use Concrete\Core\User\User;
abstract class GenericOauth2TypeController extends GenericOauthTypeController
{
/** @var AbstractService */
protected $service;
public function handle_authentication_attempt()
{
$url = $this->getService()->getAuthorizationUri($this->getAdditionalRequestParameters());
id(new RedirectResponse((string) $url))->send();
exit;
}
public function handle_authentication_callback()
{
$user = $this->app->make(User::class);
if ($user && !$user->isError() && $user->isLoggedIn()) {
$this->handle_attach_callback();
}
try {
$code = \Request::getInstance()->get('code');
$token = $this->getService()->requestAccessToken($code);
$this->setToken($token);
} catch (TokenResponseException $e) {
$this->showError(t('Failed authentication: %s', $e->getMessage()));
exit;
}
if ($token) {
try {
$user = $this->attemptAuthentication();
if ($user) {
return $this->completeAuthentication($user);
} else {
$this->showError(
t('No local user account associated with this user, please log in with a local account and connect your account from your user profile.'));
}
} catch (LoginException $e) {
$this->showError($e->getMessage());
} catch (Exception $e) {
$this->showError($e->getMessage());
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
$this->showError(t('An unexpected error occurred.'));
}
} else {
$this->showError(t('Failed to complete authentication.'));
}
exit;
}
public function handle_attach_attempt()
{
$url = $this->getService()->getAuthorizationUri($this->getAdditionalRequestParameters());
id(new RedirectResponse((string) $url))->send();
exit;
}
public function handle_attach_callback()
{
$user = $this->app->make(User::class);
if (!$user->isRegistered()) {
id(new RedirectResponse(\URL::to('')))->send();
exit;
}
try {
$code = \Request::getInstance()->get('code');
$token = $this->getService()->requestAccessToken($code);
} catch (TokenResponseException $e) {
$this->showError(t('Failed authentication: %s', $e->getMessage()));
exit;
}
if ($token) {
if ($this->bindUser($user, $this->getExtractor(true)->getUniqueId())) {
$this->showSuccess(t('Successfully attached.'));
exit;
}
}
$this->showError(t('Unable to attach user.'));
exit;
}
/**
* @return \OAuth\OAuth2\Service\AbstractService
*/
public function getService()
{
return parent::getService();
}
public function view()
{
// Nothing here.
}
/**
* Method used to clean up.
* This method must be defined, if it isn't needed, leave it blank.
*
* @param \User $u
*/
public function deauthenticate(User $u)
{
// Nothing to do here.
}
/**
* Test user authentication status.
*
* @param \User $u
*
* @return bool Returns true if user is authenticated, false if not
*/
public function isAuthenticated(User $u)
{
return $u->isLoggedIn();
}
}