1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocialiteProviders\StackExchange; |
4
|
|
|
|
5
|
|
|
use SocialiteProviders\Manager\OAuth2\User; |
6
|
|
|
use Laravel\Socialite\Two\ProviderInterface; |
7
|
|
|
use SocialiteProviders\Manager\OAuth2\AbstractProvider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* https://api.stackexchange.com/docs/authentication |
11
|
|
|
* Class Provider. |
12
|
|
|
*/ |
13
|
|
|
class Provider extends AbstractProvider implements ProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Unique Provider Identifier. |
17
|
|
|
*/ |
18
|
|
|
const IDENTIFIER = 'STACKEXCHANGE'; |
19
|
|
|
|
20
|
|
|
protected $version = '2.2'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
protected function getAuthUrl($state) |
26
|
|
|
{ |
27
|
|
|
return $this->buildAuthUrlFromBase('https://stackexchange.com/oauth', $state); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function buildAuthUrlFromBase($url, $state) |
34
|
|
|
{ |
35
|
|
|
// https://api.stackexchange.com/docs/authentication |
36
|
|
|
|
37
|
|
|
$session = $this->request->getSession(); |
|
|
|
|
38
|
|
|
|
39
|
|
|
return $url.'?'.http_build_query( |
40
|
|
|
[ |
41
|
|
|
'client_id' => $this->clientId, |
42
|
|
|
'redirect_uri' => $this->redirectUrl, |
43
|
|
|
'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator), |
44
|
|
|
'state' => $state, |
45
|
|
|
], |
46
|
|
|
'', |
47
|
|
|
'&', |
48
|
|
|
$this->encodingType |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
protected function getTokenUrl() |
56
|
|
|
{ |
57
|
|
|
return 'https://stackexchange.com/oauth/access_token'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function getAccessTokenResponse($code) |
64
|
|
|
{ |
65
|
|
|
$response = $this->getHttpClient()->post($this->getTokenUrl(), [ |
66
|
|
|
'headers' => ['Accept' => 'application/json'], |
67
|
|
|
'form_params' => $this->getTokenFields($code), |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
parse_str($response->getBody()->getContents(), $data); |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function getUserByToken($token) |
79
|
|
|
{ |
80
|
|
|
// https://api.stackexchange.com/docs/me |
81
|
|
|
$response = $this->getHttpClient()->get( |
82
|
|
|
'https://api.stackexchange.com/'.$this->version. |
83
|
|
|
'/me?'.http_build_query( |
84
|
|
|
[ |
85
|
|
|
'site' => $this->getFromConfig('site'), |
86
|
|
|
'access_token' => $token, |
87
|
|
|
'key' => $this->getFromConfig('key'), |
88
|
|
|
] |
89
|
|
|
), |
90
|
|
|
[ |
91
|
|
|
'headers' => [ |
92
|
|
|
'Accept' => 'application/json', |
93
|
|
|
], |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
return json_decode($response->getBody()->getContents(), true); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $arrayKey |
102
|
|
|
*/ |
103
|
|
|
protected function getFromConfig($arrayKey) |
104
|
|
|
{ |
105
|
|
|
return app()['config']['services.stackexchange'][$arrayKey]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
protected function mapUserToObject(array $user) |
112
|
|
|
{ |
113
|
|
|
return (new User())->setRaw($user)->map( |
114
|
|
|
[ |
115
|
|
|
'id' => $user['items'][0]['account_id'], |
116
|
|
|
'nickname' => $user['items'][0]['display_name'], |
117
|
|
|
'name' => $user['items'][0]['display_name'], |
118
|
|
|
'avatar' => $user['items'][0]['profile_image'], |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.