/
GenericOauth1aTypeController.php
109 lines (96 loc) · 3.34 KB
/
GenericOauth1aTypeController.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
<?php
namespace Concrete\Core\Authentication\Type\OAuth\OAuth1a;
use Concrete\Core\Authentication\Type\OAuth\GenericOauthTypeController;
use Concrete\Core\Routing\RedirectResponse;
use OAuth\Common\Exception\Exception;
use Concrete\Core\User\User;
abstract class GenericOauth1aTypeController extends GenericOauthTypeController
{
public function handle_authentication_attempt()
{
$token = $this->getService()->requestRequestToken();
$url = $this->getService()->getAuthorizationUri(array('oauth_token' => $token->getRequestToken()));
id(new RedirectResponse((string) $url))->send();
exit;
}
public function handle_authentication_callback()
{
$user = new User();
if ($user && !$user->isError() && $user->isLoggedIn()) {
$this->handle_attach_callback();
}
$token = \Request::getInstance()->get('oauth_token');
$verifier = \Request::getInstance()->get('oauth_verifier');
$token = $this->getService()->requestAccessToken($token, $verifier);
$this->setToken($token);
if ($token) {
try {
$user = $this->attemptAuthentication();
if ($user) {
return $this->completeAuthentication($user)->send();
} 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 (Exception $e) {
$this->showError($e->getMessage());
} catch (\Exception $e) {
$this->showError(t('An unexpected error occurred.'));
}
} else {
$this->showError(t('Failed to complete authentication.'));
}
exit;
}
public function handle_attach_attempt()
{
$token = $this->getService()->requestRequestToken();
$url = $this->getService()->getAuthorizationUri(array('oauth_token' => $token->getRequestToken()));
id(new RedirectResponse((string) $url))->send();
exit;
}
public function handle_attach_callback()
{
$user = new User();
if (!$user->isLoggedIn()) {
id(new RedirectResponse(\URL::to('')))->send();
exit;
}
$token = \Request::getInstance()->get('oauth_token');
$verifier = \Request::getInstance()->get('oauth_verifier');
$token = $this->getService()->requestAccessToken($token, $verifier);
if ($token) {
if ($this->bindUser($user, $this->getExtractor(true)->getUniqueId())) {
$this->showSuccess(t('Successfully attached.'));
exit;
}
}
$this->showError(t('Unable to attach user.'));
exit;
}
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();
}
}