forked from SocialiteProviders/Slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provider.php
150 lines (130 loc) · 4.1 KB
/
Provider.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
<?php
namespace SocialiteProviders\Slack;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
/**
* Unique Provider Identifier.
*/
public const IDENTIFIER = 'SLACK';
/**
* {@inheritdoc}
*/
public function getScopes()
{
// No scopes is valid with Slack
return [];
}
/**
* Middleware that throws exceptions for non successful slack api calls
* "http_error" request option is set to true.
*
* @return callable Returns a function that accepts the next handler.
*/
private function getSlackApiErrorMiddleware()
{
return function (callable $handler) {
return function ($request, array $options) use ($handler) {
if (empty($options['http_errors'])) {
return $handler($request, $options);
}
return $handler($request, $options)->then(
function (ResponseInterface $response) use ($request) {
$body = json_decode($response->getBody()->getContents(), true);
$response->getBody()->rewind();
if ($body['ok']) {
return $response;
}
throw RequestException::create($request, $response);
}
);
};
};
}
/**
* {@inheritdoc}
*/
protected function getHttpClient()
{
$handler = HandlerStack::create();
$handler->push($this->getSlackApiErrorMiddleware(), 'slack_api_errors');
if (is_null($this->httpClient)) {
$this->httpClient = new Client(['handler' => $handler]);
}
return $this->httpClient;
}
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(
'https://slack.com/oauth/v2/authorize',
$state
);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://slack.com/api/oauth.v2.access';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
try {
$response = $this->getHttpClient()->get(
'https://slack.com/api/users.identity',
[
'headers' => [
'Authorization' => 'Bearer '.$token,
],
]
);
} catch (RequestException $exception) {
// Getting user informations requires the "identity.*" scopes, however we might want to not add them to the
// scope list for various reasons. Instead of throwing an exception on this error, we return an empty user.
if ($exception->hasResponse()) {
$data = json_decode($exception->getResponse()->getBody()->getContents(), true);
if (Arr::get($data, 'error') === 'missing_scope') {
return [];
}
}
throw $exception;
}
return json_decode($response->getBody()->getContents(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => Arr::get($user, 'user.id'),
'name' => Arr::get($user, 'user.name'),
'email' => Arr::get($user, 'user.email'),
'avatar' => Arr::get($user, 'user.image_192'),
'organization_id' => Arr::get($user, 'team.id'),
]);
}
/**
* Get the access token from the token response body.
*
* @param array $body
*
* @return string
*/
protected function parseAccessToken($body)
{
return Arr::get($body, 'authed_user.access_token');
}
}