1 | <?php |
||
12 | abstract class AbstractProvider extends BaseProvider implements ProviderInterface |
||
13 | { |
||
14 | use ConfigTrait; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | 1 | */ |
|
19 | protected $credentialsResponseBody; |
||
20 | 1 | ||
21 | public static function serviceContainerKey($providerName) |
||
22 | { |
||
23 | 7 | return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName; |
|
24 | } |
||
25 | 7 | ||
26 | /** |
||
27 | * @return \SocialiteProviders\Manager\OAuth2\User |
||
28 | */ |
||
29 | public function user() |
||
30 | { |
||
31 | if ($this->hasInvalidState()) { |
||
32 | throw new InvalidStateException(); |
||
33 | 1 | } |
|
34 | |||
35 | 1 | $user = $this->mapUserToObject($this->getUserByToken( |
|
36 | 1 | $token = $this->getAccessToken($this->getCode()) |
|
37 | 1 | )); |
|
38 | 1 | ||
39 | $user->setToken($token); |
||
40 | 1 | ||
41 | if ($user instanceof User) { |
||
42 | return $user->setAccessTokenResponseBody($this->credentialsResponseBody); |
||
43 | } |
||
44 | |||
45 | return $user; |
||
46 | 5 | } |
|
47 | |||
48 | 5 | /** |
|
49 | 2 | * Get the access token for the given code. |
|
50 | * |
||
51 | * @param string $code |
||
52 | 3 | * |
|
53 | 3 | * @return string |
|
54 | 3 | */ |
|
55 | public function getAccessToken($code) |
||
56 | 3 | { |
|
57 | $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body'; |
||
58 | 3 | ||
59 | 3 | $response = $this->getHttpClient()->post($this->getTokenUrl(), [ |
|
60 | 'headers' => ['Accept' => 'application/json'], |
||
61 | $postKey => $this->getTokenFields($code), |
||
62 | ]); |
||
63 | |||
64 | $this->credentialsResponseBody = json_decode($response->getBody(), true); |
||
65 | |||
66 | return $this->parseAccessToken($response->getBody()); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get the access token from the token response body. |
||
71 | * |
||
72 | 3 | * @param string $body |
|
73 | * @return string |
||
74 | 3 | */ |
|
75 | protected function parseAccessToken($body) |
||
76 | 3 | { |
|
77 | 3 | return json_decode($body, true)['access_token']; |
|
78 | 3 | } |
|
79 | 3 | ||
80 | $this->getAccessTokenResponse($this->getCode()) |
||
96 |