Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add Slack OpenID provider #704

Merged
15 changes: 15 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laravel\Socialite\Two\GoogleProvider;
use Laravel\Socialite\Two\LinkedInOpenIdProvider;
use Laravel\Socialite\Two\LinkedInProvider;
use Laravel\Socialite\Two\SlackOpenIdProvider;
use Laravel\Socialite\Two\SlackProvider;
use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;
Expand Down Expand Up @@ -184,6 +185,20 @@ protected function createSlackDriver()
);
}

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

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

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

namespace Laravel\Socialite\Two;

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

class SlackOpenIdProvider extends AbstractProvider implements ProviderInterface
{
protected $scopes = ['openid', 'email', 'profile'];

protected $scopeSeparator = ' ';

protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://slack.com/openid/connect/authorize', $state);
}

protected function getTokenUrl()
{
return 'https://slack.com/api/openid.connect.token';
}

protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://slack.com/api/openid.connect.userInfo', [
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
]);

return json_decode($response->getBody(), true);
}

protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => Arr::get($user, 'sub'),
'nickname' => null,
'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'),
]);
}
}