Skip to content

Commit f5996f4

Browse files
authored
Add support for Slack driver (#644)
1 parent e9a18f0 commit f5996f4

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/SocialiteManager.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Laravel\Socialite\Two\GitlabProvider;
1414
use Laravel\Socialite\Two\GoogleProvider;
1515
use Laravel\Socialite\Two\LinkedInProvider;
16+
use Laravel\Socialite\Two\SlackProvider;
1617
use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider;
1718
use League\OAuth1\Client\Server\Twitter as TwitterServer;
1819

@@ -154,6 +155,20 @@ protected function createTwitterOAuth2Driver()
154155
);
155156
}
156157

158+
/**
159+
* Create an instance of the specified driver.
160+
*
161+
* @return \Laravel\Socialite\Two\AbstractProvider
162+
*/
163+
protected function createSlackDriver()
164+
{
165+
$config = $this->config->get('services.slack');
166+
167+
return $this->buildProvider(
168+
SlackProvider::class, $config
169+
);
170+
}
171+
157172
/**
158173
* Build an OAuth 2 provider instance.
159174
*

src/Two/SlackProvider.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Laravel\Socialite\Two;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use Illuminate\Support\Arr;
7+
8+
class SlackProvider extends AbstractProvider
9+
{
10+
/**
11+
* The scopes being requested.
12+
*
13+
* @var array
14+
*/
15+
protected $scopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar'];
16+
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function getAuthUrl($state)
21+
{
22+
return $this->buildAuthUrlFromBase('https://slack.com/oauth/authorize', $state);
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function getTokenUrl()
29+
{
30+
return 'https://slack.com/api/oauth.access';
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
protected function getUserByToken($token)
37+
{
38+
$response = $this->getHttpClient()->get('https://slack.com/api/users.identity', [
39+
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
40+
]);
41+
42+
return json_decode($response->getBody(), true);
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function mapUserToObject(array $user)
49+
{
50+
return (new User)->setRaw($user)->map([
51+
'id' => Arr::get($user, 'user.id'),
52+
'name' => Arr::get($user, 'user.name'),
53+
'email' => Arr::get($user, 'user.email'),
54+
'avatar' => Arr::get($user, 'user.image_512'),
55+
'organization_id' => Arr::get($user, 'team.id'),
56+
]);
57+
}
58+
}

0 commit comments

Comments
 (0)