/
AbstractProvider.php
127 lines (108 loc) · 3.05 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
<?php
namespace SocialiteProviders\Manager\OAuth2;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider as BaseProvider;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\ConfigTrait;
use SocialiteProviders\Manager\Contracts\OAuth2\ProviderInterface;
use SocialiteProviders\Manager\SocialiteWasCalled;
abstract class AbstractProvider extends BaseProvider implements ProviderInterface
{
use ConfigTrait;
/**
* @var array
*/
protected $credentialsResponseBody;
/**
* The cached user instance.
*
* @var \SocialiteProviders\Manager\OAuth2\User|null
*/
protected $user;
/**
* @param string $providerName
*
* @return string
*/
public static function serviceContainerKey($providerName)
{
return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX . $providerName;
}
/**
* @return \SocialiteProviders\Manager\OAuth2\User
*
* @throws \Laravel\Socialite\Two\InvalidStateException
*/
public function user()
{
if ($this->user) {
return $this->user;
}
if ($this->hasInvalidState()) {
throw new InvalidStateException();
}
$response = $this->getAccessTokenResponse($this->getCode());
$this->credentialsResponseBody = $response;
$this->user = $this->mapUserToObject($this->getUserByToken(
$token = $this->parseAccessToken($response)
));
if ($this->user instanceof User) {
$this->user->setAccessTokenResponseBody($this->credentialsResponseBody);
}
return $this->user->setToken($token)
->setRefreshToken($this->parseRefreshToken($response))
->setExpiresIn($this->parseExpiresIn($response))
->setApprovedScopes($this->parseApprovedScopes($response));
}
/**
* Get the access token from the token response body.
*
* @param array $body
*
* @return string
*/
protected function parseAccessToken($body)
{
return Arr::get($body, 'access_token');
}
/**
* Get the refresh token from the token response body.
*
* @param array $body
*
* @return string
*/
protected function parseRefreshToken($body)
{
return Arr::get($body, 'refresh_token');
}
/**
* Get the expires in from the token response body.
*
* @param array $body
*
* @return string
*/
protected function parseExpiresIn($body)
{
return Arr::get($body, 'expires_in');
}
/**
* Get the approved scopes from the token response body.
*
* @param array $body
*
* @return array
*/
protected function parseApprovedScopes($body)
{
$scopesRaw = Arr::get($body, 'scope', null);
if (!is_array($scopesRaw) && !is_string($scopesRaw)) {
return [];
}
if (is_array($scopesRaw)) {
return $scopesRaw;
}
return explode($this->scopeSeparator, Arr::get($body, 'scope', ''));
}
}