/
AbstractProvider.php
192 lines (158 loc) · 4.87 KB
/
AbstractProvider.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace SocialiteProviders\Manager\OAuth1;
use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\One\AbstractProvider as BaseProvider;
use League\OAuth1\Client\Credentials\TokenCredentials;
use SocialiteProviders\Manager\ConfigTrait;
use SocialiteProviders\Manager\Contracts\ConfigInterface as Config;
use SocialiteProviders\Manager\Contracts\OAuth1\ProviderInterface;
use SocialiteProviders\Manager\SocialiteWasCalled;
abstract class AbstractProvider extends BaseProvider implements ProviderInterface
{
use ConfigTrait;
/**
* Indicates if the session state should be utilized.
*
* @var bool
*/
protected $stateless = true;
/**
* @var array
*/
protected $credentialsResponseBody;
/**
* @param string $providerName
*
* @return string
*/
public static function serviceContainerKey($providerName)
{
return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName;
}
/**
* {@inheritdoc}
*/
public function user()
{
if (!$this->hasNecessaryVerifier()) {
throw new \InvalidArgumentException('Invalid request. Missing OAuth verifier.');
}
$token = $this->getToken();
$tokenCredentials = $token['tokenCredentials'];
$user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
$user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
if ($user instanceof User) {
parse_str($token['credentialsResponseBody'], $credentialsResponseBody);
if (!$credentialsResponseBody || !is_array($credentialsResponseBody)) {
throw new CredentialsException('Unable to parse token credentials response.');
}
$user->setAccessTokenResponseBody($credentialsResponseBody);
}
return $user;
}
/**
* {@inheritdoc}
*/
public function userFromTokenAndSecret($token, $secret)
{
$tokenCredentials = new TokenCredentials();
$tokenCredentials->setIdentifier($token);
$tokenCredentials->setSecret($secret);
$user = $this->mapUserToObject((array) $this->server->getUserDetails($tokenCredentials));
$user->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
return $user;
}
/**
* Redirect the user to the authentication page for the provider.
*
* @return RedirectResponse
*/
public function redirect()
{
if (!$this->isStateless()) {
$this->request->getSession()->put(
'oauth.temp', $temp = $this->server->getTemporaryCredentials()
);
} else {
$temp = $this->server->getTemporaryCredentials();
$this->request->session()->put('oauth_temp', serialize($temp));
}
return new RedirectResponse($this->server->getAuthorizationUrl($temp));
}
/**
* Indicates that the provider should operate as stateless.
*
* @param mixed $stateless
*
* @return $this
*/
public function stateless($stateless = true)
{
$this->stateless = $stateless;
return $this;
}
/**
* Set the scopes of the requested access.
*
* @param array $scopes
*
* @return $this
*/
public function scopes(array $scopes)
{
$this->server = $this->server->scopes($scopes);
return $this;
}
/**
* Set the custom parameters of the request.
*
* @param array $parameters
*
* @return $this
*/
public function with(array $parameters)
{
$this->server = $this->server->with($parameters);
return $this;
}
/**
* @param Config $config
*
* @return $this
*/
public function setConfig(Config $config)
{
$this->config = $this->server->setConfig($config);
return $this;
}
/**
* Get the token credentials for the request.
*
* @return \League\OAuth1\Client\Credentials\TokenCredentials
*/
protected function getToken()
{
if (!$this->isStateless()) {
$temp = $this->request->getSession()->get('oauth.temp');
return $this->server->getTokenCredentials(
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
);
}
$temp = unserialize($this->request->session()->get('oauth_temp'));
return $this->server->getTokenCredentials(
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
);
}
/**
* Determine if the provider is operating as stateless.
*
* @return bool
*/
protected function isStateless()
{
if (defined('SOCIALITEPROVIDERS_STATELESS')) {
return true;
}
return $this->stateless;
}
}