1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AdamPaterson\OAuth2\Client\Provider; |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\AbstractProvider; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Slack |
12
|
|
|
* |
13
|
|
|
* @author Adam Paterson <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @package AdamPaterson\OAuth2\Client\Provider |
16
|
|
|
*/ |
17
|
|
|
class Slack extends AbstractProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns the base URL for authorizing a client. |
21
|
|
|
* |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
6 |
|
public function getBaseAuthorizationUrl() |
25
|
|
|
{ |
26
|
6 |
|
return 'https://slack.com/oauth/authorize'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns the base URL for requesting an access token. |
31
|
|
|
* |
32
|
|
|
* @param array $params |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
37
|
|
|
{ |
38
|
12 |
|
return 'https://slack.com/api/oauth.access'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Returns the URL for requesting the resource owner's details. |
43
|
|
|
* |
44
|
|
|
* @param AccessToken $token |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
49
|
|
|
{ |
50
|
3 |
|
$authorizedUser = $this->getAuthorizedUser($token); |
51
|
|
|
|
52
|
|
|
$params = [ |
53
|
3 |
|
'token' => $token->getToken(), |
54
|
3 |
|
'user' => $authorizedUser->getId() |
55
|
1 |
|
]; |
56
|
|
|
|
57
|
3 |
|
return 'https://slack.com/api/users.info?'.http_build_query($params); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $token |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
9 |
|
public function getAuthorizedUserTestUrl($token) |
66
|
|
|
{ |
67
|
9 |
|
return 'https://slack.com/api/auth.test?token=' . $token; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Checks a provider response for errors. |
72
|
|
|
* |
73
|
|
|
* @throws IdentityProviderException |
74
|
|
|
* |
75
|
|
|
* @param ResponseInterface $response |
76
|
|
|
* @param array|string $data Parsed response data |
77
|
|
|
* |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
12 |
|
protected function checkResponse(ResponseInterface $response, $data) |
81
|
|
|
{ |
82
|
12 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create new resources owner using the generated access token. |
86
|
|
|
* |
87
|
|
|
* @param array $response |
88
|
|
|
* @param AccessToken $token |
89
|
|
|
* |
90
|
|
|
* @return SlackResourceOwner |
91
|
|
|
*/ |
92
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
93
|
|
|
{ |
94
|
3 |
|
return new SlackResourceOwner($response); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
6 |
|
protected function getDefaultScopes() |
101
|
|
|
{ |
102
|
6 |
|
return []; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param AccessToken $token |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
6 |
|
public function fetchAuthorizedUserDetails(AccessToken $token) |
111
|
|
|
{ |
112
|
6 |
|
$url = $this->getAuthorizedUserTestUrl($token); |
113
|
|
|
|
114
|
6 |
|
$request = $this->getAuthenticatedRequest(self::METHOD_GET, $url, $token); |
115
|
|
|
|
116
|
|
|
// Keep compatibility with League\OAuth2\Client v1 |
117
|
6 |
|
if (!method_exists($this, 'getParsedResponse')) { |
118
|
|
|
return $this->getResponse($request); |
119
|
|
|
} |
120
|
|
|
|
121
|
6 |
|
return $this->getParsedResponse($request); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param AccessToken $token |
126
|
|
|
* |
127
|
|
|
* @return SlackAuthorizedUser |
128
|
|
|
*/ |
129
|
6 |
|
public function getAuthorizedUser(AccessToken $token) |
130
|
|
|
{ |
131
|
6 |
|
$response = $this->fetchAuthorizedUserDetails($token); |
132
|
|
|
|
133
|
6 |
|
return $this->createAuthorizedUser($response); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $response |
138
|
|
|
* |
139
|
|
|
* @return SlackAuthorizedUser |
140
|
|
|
*/ |
141
|
6 |
|
protected function createAuthorizedUser($response) |
142
|
|
|
{ |
143
|
6 |
|
return new SlackAuthorizedUser($response); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|