Skip to content

Commit

Permalink
Add support for Slack driver (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk authored Jul 8, 2023
1 parent e9a18f0 commit f5996f4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Laravel\Socialite\Two\GitlabProvider;
use Laravel\Socialite\Two\GoogleProvider;
use Laravel\Socialite\Two\LinkedInProvider;
use Laravel\Socialite\Two\SlackProvider;
use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;

Expand Down Expand Up @@ -154,6 +155,20 @@ protected function createTwitterOAuth2Driver()
);
}

/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createSlackDriver()
{
$config = $this->config->get('services.slack');

return $this->buildProvider(
SlackProvider::class, $config
);
}

/**
* Build an OAuth 2 provider instance.
*
Expand Down
58 changes: 58 additions & 0 deletions src/Two/SlackProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Laravel\Socialite\Two;

use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

class SlackProvider extends AbstractProvider
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['identity.basic', 'identity.email', 'identity.team', 'identity.avatar'];

/**
* {@inheritdoc}
*/
public function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://slack.com/oauth/authorize', $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://slack.com/api/oauth.access';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://slack.com/api/users.identity', [
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
]);

return json_decode($response->getBody(), 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_512'),
'organization_id' => Arr::get($user, 'team.id'),
]);
}
}

0 comments on commit f5996f4

Please sign in to comment.