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

Add X Provider #712

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Laravel\Socialite\Two\SlackOpenIdProvider;
use Laravel\Socialite\Two\SlackProvider;
use Laravel\Socialite\Two\TwitterProvider as TwitterOAuth2Provider;
use Laravel\Socialite\Two\XProvider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;

class SocialiteManager extends Manager implements Contracts\Factory
Expand Down Expand Up @@ -171,6 +172,20 @@ protected function createTwitterOAuth2Driver()
);
}

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

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

/**
* Create an instance of the specified driver.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Two/XProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Laravel\Socialite\Two;

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

class XProvider extends TwitterProvider
{
/**
* {@inheritdoc}
*/
public function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://x.com/i/oauth2/authorize', $state);
}

/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://api.x.com/2/oauth2/token';
}

/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://api.x.com/2/users/me', [
RequestOptions::HEADERS => ['Authorization' => 'Bearer '.$token],
RequestOptions::QUERY => ['user.fields' => 'profile_image_url'],
]);

return Arr::get(json_decode($response->getBody(), true), 'data');
}
}