1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocialiteProviders\Manager\OAuth1; |
4
|
|
|
|
5
|
|
|
use SocialiteProviders\Manager\SocialiteWasCalled; |
6
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
7
|
|
|
|
8
|
|
|
abstract class AbstractProvider extends \Laravel\Socialite\One\AbstractProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Indicates if the session state should be utilized. |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
protected $stateless = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $credentialsResponseBody; |
21
|
|
|
|
22
|
|
|
public static function serviceContainerKey($providerName) |
23
|
|
|
{ |
24
|
|
|
return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Config $config |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function config(Config $config) |
33
|
|
|
{ |
34
|
|
|
$config = $config->get(); |
35
|
|
|
$this->clientId = $config['client_id']; |
|
|
|
|
36
|
|
|
$this->redirectUrl = $config['redirect']; |
|
|
|
|
37
|
|
|
$this->clientSecret = $config['client_secret']; |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function user() |
46
|
|
|
{ |
47
|
|
|
if (!$this->hasNecessaryVerifier()) { |
48
|
|
|
throw new \InvalidArgumentException('Invalid request. Missing OAuth verifier.'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$token = $this->getToken(); |
52
|
|
|
$tokenCredentials = $token['tokenCredentials']; |
53
|
|
|
|
54
|
|
|
$user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials)); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret()); |
57
|
|
|
|
58
|
|
|
if ($user instanceof User) { |
59
|
|
|
parse_str($token['credentialsResponseBody'], $credentialsResponseBody); |
60
|
|
|
|
61
|
|
|
if (!$credentialsResponseBody || !is_array($credentialsResponseBody)) { |
62
|
|
|
throw new CredentialsException('Unable to parse token credentials response.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$user->setAccessTokenResponseBody($credentialsResponseBody); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $user; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Redirect the user to the authentication page for the provider. |
73
|
|
|
* |
74
|
|
|
* @return RedirectResponse |
75
|
|
|
*/ |
76
|
|
|
public function redirect() |
77
|
|
|
{ |
78
|
|
|
if (!$this->isStateless()) { |
79
|
|
|
$this->request->getSession()->set( |
80
|
|
|
'oauth.temp', $temp = $this->server->getTemporaryCredentials() |
81
|
|
|
); |
82
|
|
|
} else { |
83
|
|
|
$temp = $this->server->getTemporaryCredentials(); |
84
|
|
|
setcookie('oauth_temp', serialize($temp)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return new RedirectResponse($this->server->getAuthorizationUrl($temp)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the token credentials for the request. |
92
|
|
|
* |
93
|
|
|
* @return \League\OAuth1\Client\Credentials\TokenCredentials |
94
|
|
|
*/ |
95
|
|
|
protected function getToken() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if (!$this->isStateless()) { |
98
|
|
|
$temp = $this->request->getSession()->get('oauth.temp'); |
99
|
|
|
|
100
|
|
|
return $this->server->getTokenCredentials( |
101
|
|
|
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier') |
102
|
|
|
); |
103
|
|
|
} else { |
104
|
|
|
$temp = unserialize($_COOKIE['oauth_temp']); |
105
|
|
|
|
106
|
|
|
return $this->server->getTokenCredentials( |
107
|
|
|
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier') |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Indicates that the provider should operate as stateless. |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function stateless() |
118
|
|
|
{ |
119
|
|
|
$this->stateless = true; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Determine if the provider is operating as stateless. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
protected function isStateless() |
130
|
|
|
{ |
131
|
|
|
return $this->stateless; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public static function additionalConfigKeys() |
135
|
|
|
{ |
136
|
|
|
return []; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: