-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProvider.php
More file actions
165 lines (130 loc) · 4.93 KB
/
Provider.php
File metadata and controls
165 lines (130 loc) · 4.93 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace SocialiteProviders\Slack;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\InvalidStateException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'SLACK';
protected array $userScopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar'];
public function scopes($scopes)
{
$this->userScopes = array_unique(array_merge($this->userScopes, (array) $scopes));
return $this;
}
public function botScopes($scopes)
{
$this->scopes = array_unique(array_merge($this->scopes, (array) $scopes));
return $this;
}
public function setUserScopes($scopes)
{
$this->userScopes = (array) $scopes;
return $this;
}
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);
if ($this->shouldUseOpenIdConnect()) {
$fields['scope'] = $this->formatScopes($this->userScopes, ' ');
} else {
$fields['user_scope'] = $this->formatScopes($this->userScopes, $this->scopeSeparator);
$fields['scope'] = $this->formatScopes($this->scopes, $this->scopeSeparator);
}
return $fields;
}
public function user()
{
if ($this->user) {
return $this->user;
}
if ($this->hasInvalidState()) {
throw new InvalidStateException;
}
$response = $this->getAccessTokenResponse($this->getCode());
if ($this->shouldUseOpenIdConnect()) {
$token = Arr::get($response, 'access_token');
} else {
$token = Arr::get($response, 'authed_user.access_token');
}
$user = $this->getUserByToken($token);
/** @var User $userInstance */
$userInstance = $this->userInstance($response, $user);
$userInstance->setAccessTokenResponseBody($response);
return $userInstance;
}
protected function mapUserToObject(array $user)
{
if ($this->usesOpenIdScopes()) {
$attributes = [
'id' => Arr::get($user, 'sub'),
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'picture'),
'organization_id' => Arr::get($user, 'https://slack.com/team_id'),
];
} else {
$attributes = [
'id' => Arr::get($user, 'user.id'),
'name' => Arr::get($user, 'user.name'),
'email' => Arr::get($user, 'user.email'),
'avatar' => Arr::get($user, 'user.image_512'),
'organization_id' => Arr::get($user, 'team.id'),
];
}
return (new User)->setRaw($user)->map($attributes);
}
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);
return json_decode((string) $response->getBody(), true);
}
public function getAuthUrl($state): string
{
if ($this->shouldUseOpenIdConnect()) {
$url = 'https://slack.com/openid/connect/authorize';
} else {
$url = 'https://slack.com/oauth/v2/authorize';
}
return $this->buildAuthUrlFromBase($url, $state);
}
protected function getTokenUrl(): string
{
if ($this->shouldUseOpenIdConnect()) {
return 'https://slack.com/api/openid.connect.token';
}
return 'https://slack.com/api/oauth.v2.access';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
if ($this->usesOpenIdScopes()) {
$url = 'https://slack.com/api/openid.connect.userInfo';
} else {
$url = 'https://slack.com/api/users.identity';
}
$response = $this->getHttpClient()->get($url, [
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
]);
return json_decode((string) $response->getBody(), true);
}
protected function shouldUseOpenIdConnect(): bool
{
return $this->usesOpenIdScopes() && empty($this->scopes);
}
protected function usesOpenIdScopes(): bool
{
$openidScopes = ['openid', 'email', 'profile'];
$identityScopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar'];
$hasOpenIdScopes = ! empty(array_intersect($this->userScopes, $openidScopes));
$hasIdentityScopes = ! empty(array_intersect($this->userScopes, $identityScopes));
return $hasOpenIdScopes && ! $hasIdentityScopes;
}
}