Skip to content

Commit

Permalink
Merge branch '5.x'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
driesvints committed Sep 4, 2023
2 parents 4643204 + 1e81f39 commit 1038463
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 5 deletions.
22 changes: 21 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
# Release Notes

## [Unreleased](https://github.com/laravel/socialite/compare/v5.6.1...master)
## [Unreleased](https://github.com/laravel/socialite/compare/v5.8.1...master)

## [v5.8.1](https://github.com/laravel/socialite/compare/v5.8.0...v5.8.1) - 2023-08-21

- Fix phpstan issues in Twitter and Slack drivers by [@alecpl](https://github.com/alecpl) in https://github.com/laravel/socialite/pull/653

## [v5.8.0](https://github.com/laravel/socialite/compare/v5.7.0...v5.8.0) - 2023-07-14

- Update Slack provider to use v2 API and allow Bot tokens by [@jbrooksuk](https://github.com/jbrooksuk) in https://github.com/laravel/socialite/pull/645

## [v5.7.0](https://github.com/laravel/socialite/compare/v5.6.3...v5.7.0) - 2023-07-08

- Add support for Slack driver by [@jbrooksuk](https://github.com/jbrooksuk) in https://github.com/laravel/socialite/pull/644

## [v5.6.3](https://github.com/laravel/socialite/compare/v5.6.2...v5.6.3) - 2023-06-06

- Add buildProvider method on DockBlock for IDE support by @emrancu in https://github.com/laravel/socialite/pull/643

## [v5.6.2](https://github.com/laravel/socialite/compare/v5.6.1...v5.6.2) - 2023-05-29

- Fix unable to use updated config object when using Laravel Octane by @aprokopenko in https://github.com/laravel/socialite/pull/639

## [v5.6.1](https://github.com/laravel/socialite/compare/v5.6.0...v5.6.1) - 2023-01-20

Expand Down
1 change: 1 addition & 0 deletions src/Facades/Socialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @method static \Laravel\Socialite\Contracts\Provider driver(string $driver = null)
* @method static \Laravel\Socialite\Two\AbstractProvider buildProvider($provider, $config)
*
* @see \Laravel\Socialite\SocialiteManager
*/
Expand Down
16 changes: 16 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 Expand Up @@ -222,6 +237,7 @@ public function setContainer($container)
{
$this->app = $container;
$this->container = $container;
$this->config = $container->make('config');

return $this;
}
Expand Down
1 change: 1 addition & 0 deletions src/Two/GithubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nodeId' => $user['node_id'],
'nickname' => $user['login'],
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
Expand Down
111 changes: 111 additions & 0 deletions src/Two/SlackProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Laravel\Socialite\Two;

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

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

/**
* The key used for scopes.
*
* @var string
*/
protected $scopeKey = 'user_scope';

/**
* Indicate that the requested token should be for a bot user.
*
* @return $this
*/
public function asBotUser()
{
$this->scopeKey = 'scope';

return $this;
}

/**
* {@inheritdoc}
*/
public 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)
{
$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'),
]);
}

/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);

if ($this->scopeKey === 'user_scope') {
$fields['scope'] = '';
$fields['user_scope'] = $this->formatScopes($this->scopes, $this->scopeSeparator);
}

return $fields;
}

/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);

$result = json_decode($response->getBody(), true);

if ($this->scopeKey === 'user_scope') {
return $result['authed_user'];
}

return $result;
}
}
2 changes: 1 addition & 1 deletion src/Two/TwitterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;

class TwitterProvider extends AbstractProvider
class TwitterProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
Expand Down
16 changes: 13 additions & 3 deletions tests/LinkedInProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class LinkedInProviderTest extends TestCase
{
Expand All @@ -25,15 +26,24 @@ public function test_it_can_map_a_user_without_an_email_address()
$request = m::mock(Request::class);
$request->allows('input')->with('code')->andReturns('fake-code');

$stream = m::mock(StreamInterface::class);
$stream->allows('__toString')->andReturns(json_encode(['access_token' => 'fake-token']));

$accessTokenResponse = m::mock(ResponseInterface::class);
$accessTokenResponse->allows('getBody')->andReturns(json_encode(['access_token' => 'fake-token']));
$accessTokenResponse->allows('getBody')->andReturns($stream);

$basicProfileStream = m::mock(StreamInterface::class);
$basicProfileStream->allows('__toString')->andReturns(json_encode(['id' => $userId = 1]));

$basicProfileResponse = m::mock(ResponseInterface::class);
$basicProfileResponse->allows('getBody')->andReturns(json_encode(['id' => $userId = 1]));
$basicProfileResponse->allows('getBody')->andReturns($basicProfileStream);

$emailAddressStream = m::mock(StreamInterface::class);
$emailAddressStream->allows('__toString')->andReturns(json_encode(['elements' => []]));

// Make sure email address response contains no values.
$emailAddressResponse = m::mock(ResponseInterface::class);
$emailAddressResponse->allows('getBody')->andReturns(json_encode(['elements' => []]));
$emailAddressResponse->allows('getBody')->andReturns($emailAddressStream);

$guzzle = m::mock(Client::class);
$guzzle->expects('post')->andReturns($accessTokenResponse);
Expand Down

0 comments on commit 1038463

Please sign in to comment.