Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,210 @@
<?php
namespace Laravel\Socialite;
use ArrayAccess;
use Laravel\Socialite\Contracts\User;
abstract class AbstractUser implements ArrayAccess, User
{
/**
* The unique identifier for the user.
*
* @var mixed
*/
public $id;
/**
* The user's nickname / username.
*
* @var string
*/
public $nickname;
/**
* The user's full name.
*
* @var string
*/
public $name;
/**
* The user's e-mail address.
*
* @var string
*/
public $email;
/**
* The user's avatar image URL.
*
* @var string
*/
public $avatar;
/**
* The user's raw attributes.
*
* @var array
*/
public $user;
/**
* The user's other attributes.
*
* @var array
*/
public $attributes = [];
/**
* Get the unique identifier for the user.
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Get the nickname / username for the user.
*
* @return string|null
*/
public function getNickname()
{
return $this->nickname;
}
/**
* Get the full name of the user.
*
* @return string|null
*/
public function getName()
{
return $this->name;
}
/**
* Get the e-mail address of the user.
*
* @return string|null
*/
public function getEmail()
{
return $this->email;
}
/**
* Get the avatar / image URL for the user.
*
* @return string|null
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* Get the raw user array.
*
* @return array
*/
public function getRaw()
{
return $this->user;
}
/**
* Set the raw user array from the provider.
*
* @param array $user
* @return $this
*/
public function setRaw(array $user)
{
$this->user = $user;
return $this;
}
/**
* Map the given array onto the user's properties.
*
* @param array $attributes
* @return $this
*/
public function map(array $attributes)
{
$this->attributes = $attributes;
foreach ($attributes as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
return $this;
}
/**
* Determine if the given raw user attribute exists.
*
* @param string $offset
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return array_key_exists($offset, $this->user);
}
/**
* Get the given key from the raw user.
*
* @param string $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->user[$offset];
}
/**
* Set the given attribute on the raw user array.
*
* @param string $offset
* @param mixed $value
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->user[$offset] = $value;
}
/**
* Unset the given value from the raw user array.
*
* @param string $offset
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->user[$offset]);
}
/**
* Get a user attribute value dynamically.
*
* @param string $key
* @return void
*/
public function __get($key)
{
return $this->attributes[$key] ?? null;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Laravel\Socialite\Contracts;
interface Factory
{
/**
* Get an OAuth provider implementation.
*
* @param string $driver
* @return \Laravel\Socialite\Contracts\Provider
*/
public function driver($driver = null);
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Laravel\Socialite\Contracts;
interface Provider
{
/**
* Redirect the user to the authentication page for the provider.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Illuminate\Http\RedirectResponse
*/
public function redirect();
/**
* Get the User instance for the authenticated user.
*
* @return \Laravel\Socialite\Contracts\User
*/
public function user();
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Laravel\Socialite\Contracts;
interface User
{
/**
* Get the unique identifier for the user.
*
* @return string
*/
public function getId();
/**
* Get the nickname / username for the user.
*
* @return string|null
*/
public function getNickname();
/**
* Get the full name of the user.
*
* @return string|null
*/
public function getName();
/**
* Get the e-mail address of the user.
*
* @return string|null
*/
public function getEmail();
/**
* Get the avatar / image URL for the user.
*
* @return string|null
*/
public function getAvatar();
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Laravel\Socialite\Facades;
use Illuminate\Support\Facades\Facade;
use Laravel\Socialite\Contracts\Factory;
/**
* @method static \Laravel\Socialite\Contracts\Provider driver(string $driver = null)
* @method static \Laravel\Socialite\Two\AbstractProvider buildProvider($provider, $config)
*
* @see \Laravel\Socialite\SocialiteManager
*/
class Socialite extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return Factory::class;
}
}

View File

@@ -0,0 +1,184 @@
<?php
namespace Laravel\Socialite\One;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Laravel\Socialite\Contracts\Provider as ProviderContract;
use League\OAuth1\Client\Credentials\TokenCredentials;
use League\OAuth1\Client\Server\Server;
abstract class AbstractProvider implements ProviderContract
{
/**
* The HTTP request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* The OAuth server implementation.
*
* @var \League\OAuth1\Client\Server\Server
*/
protected $server;
/**
* A hash representing the last requested user.
*
* @var string
*/
protected $userHash;
/**
* Create a new provider instance.
*
* @param \Illuminate\Http\Request $request
* @param \League\OAuth1\Client\Server\Server $server
* @return void
*/
public function __construct(Request $request, Server $server)
{
$this->server = $server;
$this->request = $request;
}
/**
* Redirect the user to the authentication page for the provider.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function redirect()
{
$this->request->session()->put(
'oauth.temp', $temp = $this->server->getTemporaryCredentials()
);
return new RedirectResponse($this->server->getAuthorizationUrl($temp));
}
/**
* Get the User instance for the authenticated user.
*
* @return \Laravel\Socialite\One\User
*
* @throws \Laravel\Socialite\One\MissingVerifierException
*/
public function user()
{
if (! $this->hasNecessaryVerifier()) {
throw new MissingVerifierException('Invalid request. Missing OAuth verifier.');
}
$token = $this->getToken();
$user = $this->server->getUserDetails(
$token, $this->shouldBypassCache($token->getIdentifier(), $token->getSecret())
);
$instance = (new User)->setRaw($user->extra)
->setToken($token->getIdentifier(), $token->getSecret());
return $instance->map([
'id' => $user->uid,
'nickname' => $user->nickname,
'name' => $user->name,
'email' => $user->email,
'avatar' => $user->imageUrl,
]);
}
/**
* Get a Social User instance from a known access token and secret.
*
* @param string $token
* @param string $secret
* @return \Laravel\Socialite\One\User
*/
public function userFromTokenAndSecret($token, $secret)
{
$tokenCredentials = new TokenCredentials();
$tokenCredentials->setIdentifier($token);
$tokenCredentials->setSecret($secret);
$user = $this->server->getUserDetails(
$tokenCredentials, $this->shouldBypassCache($token, $secret)
);
$instance = (new User)->setRaw($user->extra)
->setToken($tokenCredentials->getIdentifier(), $tokenCredentials->getSecret());
return $instance->map([
'id' => $user->uid,
'nickname' => $user->nickname,
'name' => $user->name,
'email' => $user->email,
'avatar' => $user->imageUrl,
]);
}
/**
* Get the token credentials for the request.
*
* @return \League\OAuth1\Client\Credentials\TokenCredentials
*/
protected function getToken()
{
$temp = $this->request->session()->get('oauth.temp');
if (! $temp) {
throw new MissingTemporaryCredentialsException('Missing temporary OAuth credentials.');
}
return $this->server->getTokenCredentials(
$temp, $this->request->get('oauth_token'), $this->request->get('oauth_verifier')
);
}
/**
* Determine if the request has the necessary OAuth verifier.
*
* @return bool
*/
protected function hasNecessaryVerifier()
{
return $this->request->has(['oauth_token', 'oauth_verifier']);
}
/**
* Determine if the user information cache should be bypassed.
*
* @param string $token
* @param string $secret
* @return bool
*/
protected function shouldBypassCache($token, $secret)
{
$newHash = sha1($token.'_'.$secret);
if (! empty($this->userHash) && $newHash !== $this->userHash) {
$this->userHash = $newHash;
return true;
}
$this->userHash = $this->userHash ?: $newHash;
return false;
}
/**
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;
return $this;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Laravel\Socialite\One;
use InvalidArgumentException;
class MissingTemporaryCredentialsException extends InvalidArgumentException
{
//
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Laravel\Socialite\One;
use InvalidArgumentException;
class MissingVerifierException extends InvalidArgumentException
{
//
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Laravel\Socialite\One;
class TwitterProvider extends AbstractProvider
{
/**
* {@inheritdoc}
*/
public function user()
{
if (! $this->hasNecessaryVerifier()) {
throw new MissingVerifierException('Invalid request. Missing OAuth verifier.');
}
$user = $this->server->getUserDetails($token = $this->getToken(), $this->shouldBypassCache($token->getIdentifier(), $token->getSecret()));
$extraDetails = [
'location' => $user->location,
'description' => $user->description,
];
$instance = (new User)->setRaw(array_merge($user->extra, $user->urls, $extraDetails))
->setToken($token->getIdentifier(), $token->getSecret());
return $instance->map([
'id' => $user->uid,
'nickname' => $user->nickname,
'name' => $user->name,
'email' => $user->email,
'avatar' => $user->imageUrl,
'avatar_original' => str_replace('_normal', '', $user->imageUrl),
]);
}
/**
* Set the access level the application should request to the user account.
*
* @param string $scope
* @return void
*/
public function scope(string $scope)
{
$this->server->setApplicationScope($scope);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Laravel\Socialite\One;
use Laravel\Socialite\AbstractUser;
class User extends AbstractUser
{
/**
* The user's access token.
*
* @var string
*/
public $token;
/**
* The user's access token secret.
*
* @var string
*/
public $tokenSecret;
/**
* Set the token on the user.
*
* @param string $token
* @param string $tokenSecret
* @return $this
*/
public function setToken($token, $tokenSecret)
{
$this->token = $token;
$this->tokenSecret = $tokenSecret;
return $this;
}
}

View File

@@ -0,0 +1,256 @@
<?php
namespace Laravel\Socialite;
use Illuminate\Support\Arr;
use Illuminate\Support\Manager;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Laravel\Socialite\One\TwitterProvider;
use Laravel\Socialite\Two\BitbucketProvider;
use Laravel\Socialite\Two\FacebookProvider;
use Laravel\Socialite\Two\GithubProvider;
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;
class SocialiteManager extends Manager implements Contracts\Factory
{
/**
* The application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*
* @deprecated Will be removed in a future Socialite release.
*/
protected $app;
/**
* Get a driver instance.
*
* @param string $driver
* @return mixed
*/
public function with($driver)
{
return $this->driver($driver);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createGithubDriver()
{
$config = $this->config->get('services.github');
return $this->buildProvider(
GithubProvider::class, $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createFacebookDriver()
{
$config = $this->config->get('services.facebook');
return $this->buildProvider(
FacebookProvider::class, $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createGoogleDriver()
{
$config = $this->config->get('services.google');
return $this->buildProvider(
GoogleProvider::class, $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createLinkedinDriver()
{
$config = $this->config->get('services.linkedin');
return $this->buildProvider(
LinkedInProvider::class, $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createBitbucketDriver()
{
$config = $this->config->get('services.bitbucket');
return $this->buildProvider(
BitbucketProvider::class, $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createGitlabDriver()
{
$config = $this->config->get('services.gitlab');
return $this->buildProvider(
GitlabProvider::class, $config
)->setHost($config['host'] ?? null);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
*/
protected function createTwitterDriver()
{
$config = $this->config->get('services.twitter');
if (($config['oauth'] ?? null) === 2) {
return $this->createTwitterOAuth2Driver();
}
return new TwitterProvider(
$this->container->make('request'), new TwitterServer($this->formatConfig($config))
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createTwitterOAuth2Driver()
{
$config = $this->config->get('services.twitter') ?? $this->config->get('services.twitter-oauth-2');
return $this->buildProvider(
TwitterOAuth2Provider::class, $config
);
}
/**
* 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.
*
* @param string $provider
* @param array $config
* @return \Laravel\Socialite\Two\AbstractProvider
*/
public function buildProvider($provider, $config)
{
return new $provider(
$this->container->make('request'), $config['client_id'],
$config['client_secret'], $this->formatRedirectUrl($config),
Arr::get($config, 'guzzle', [])
);
}
/**
* Format the server configuration.
*
* @param array $config
* @return array
*/
public function formatConfig(array $config)
{
return array_merge([
'identifier' => $config['client_id'],
'secret' => $config['client_secret'],
'callback_uri' => $this->formatRedirectUrl($config),
], $config);
}
/**
* Format the callback URL, resolving a relative URI if needed.
*
* @param array $config
* @return string
*/
protected function formatRedirectUrl(array $config)
{
$redirect = value($config['redirect']);
return Str::startsWith($redirect ?? '', '/')
? $this->container->make('url')->to($redirect)
: $redirect;
}
/**
* Forget all of the resolved driver instances.
*
* @return $this
*/
public function forgetDrivers()
{
$this->drivers = [];
return $this;
}
/**
* Set the container instance used by the manager.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return $this
*/
public function setContainer($container)
{
$this->app = $container;
$this->container = $container;
$this->config = $container->make('config');
return $this;
}
/**
* Get the default driver name.
*
* @return string
*
* @throws \InvalidArgumentException
*/
public function getDefaultDriver()
{
throw new InvalidArgumentException('No Socialite driver was specified.');
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace Laravel\Socialite;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Laravel\Socialite\Contracts\Factory;
class SocialiteServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(Factory::class, function ($app) {
return new SocialiteManager($app);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [Factory::class];
}
}

View File

@@ -0,0 +1,538 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Socialite\Contracts\Provider as ProviderContract;
abstract class AbstractProvider implements ProviderContract
{
/**
* The HTTP request instance.
*
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* The HTTP Client instance.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The client ID.
*
* @var string
*/
protected $clientId;
/**
* The client secret.
*
* @var string
*/
protected $clientSecret;
/**
* The redirect URL.
*
* @var string
*/
protected $redirectUrl;
/**
* The custom parameters to be sent with the request.
*
* @var array
*/
protected $parameters = [];
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = [];
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ',';
/**
* The type of the encoding in the query.
*
* @var int Can be either PHP_QUERY_RFC3986 or PHP_QUERY_RFC1738.
*/
protected $encodingType = PHP_QUERY_RFC1738;
/**
* Indicates if the session state should be utilized.
*
* @var bool
*/
protected $stateless = false;
/**
* Indicates if PKCE should be used.
*
* @var bool
*/
protected $usesPKCE = false;
/**
* The custom Guzzle configuration options.
*
* @var array
*/
protected $guzzle = [];
/**
* The cached user instance.
*
* @var \Laravel\Socialite\Two\User|null
*/
protected $user;
/**
* Create a new provider instance.
*
* @param \Illuminate\Http\Request $request
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUrl
* @param array $guzzle
* @return void
*/
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl, $guzzle = [])
{
$this->guzzle = $guzzle;
$this->request = $request;
$this->clientId = $clientId;
$this->redirectUrl = $redirectUrl;
$this->clientSecret = $clientSecret;
}
/**
* Get the authentication URL for the provider.
*
* @param string $state
* @return string
*/
abstract protected function getAuthUrl($state);
/**
* Get the token URL for the provider.
*
* @return string
*/
abstract protected function getTokenUrl();
/**
* Get the raw user for the given access token.
*
* @param string $token
* @return array
*/
abstract protected function getUserByToken($token);
/**
* Map the raw user array to a Socialite User instance.
*
* @param array $user
* @return \Laravel\Socialite\Two\User
*/
abstract protected function mapUserToObject(array $user);
/**
* Redirect the user of the application to the provider's authentication screen.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function redirect()
{
$state = null;
if ($this->usesState()) {
$this->request->session()->put('state', $state = $this->getState());
}
if ($this->usesPKCE()) {
$this->request->session()->put('code_verifier', $this->getCodeVerifier());
}
return new RedirectResponse($this->getAuthUrl($state));
}
/**
* Build the authentication URL for the provider from the given base URL.
*
* @param string $url
* @param string $state
* @return string
*/
protected function buildAuthUrlFromBase($url, $state)
{
return $url.'?'.http_build_query($this->getCodeFields($state), '', '&', $this->encodingType);
}
/**
* Get the GET parameters for the code request.
*
* @param string|null $state
* @return array
*/
protected function getCodeFields($state = null)
{
$fields = [
'client_id' => $this->clientId,
'redirect_uri' => $this->redirectUrl,
'scope' => $this->formatScopes($this->getScopes(), $this->scopeSeparator),
'response_type' => 'code',
];
if ($this->usesState()) {
$fields['state'] = $state;
}
if ($this->usesPKCE()) {
$fields['code_challenge'] = $this->getCodeChallenge();
$fields['code_challenge_method'] = $this->getCodeChallengeMethod();
}
return array_merge($fields, $this->parameters);
}
/**
* Format the given scopes.
*
* @param array $scopes
* @param string $scopeSeparator
* @return string
*/
protected function formatScopes(array $scopes, $scopeSeparator)
{
return implode($scopeSeparator, $scopes);
}
/**
* {@inheritdoc}
*/
public function user()
{
if ($this->user) {
return $this->user;
}
if ($this->hasInvalidState()) {
throw new InvalidStateException;
}
$response = $this->getAccessTokenResponse($this->getCode());
$this->user = $this->mapUserToObject($this->getUserByToken(
$token = Arr::get($response, 'access_token')
));
return $this->user->setToken($token)
->setRefreshToken(Arr::get($response, 'refresh_token'))
->setExpiresIn(Arr::get($response, 'expires_in'))
->setApprovedScopes(explode($this->scopeSeparator, Arr::get($response, 'scope', '')));
}
/**
* Get a Social User instance from a known access token.
*
* @param string $token
* @return \Laravel\Socialite\Two\User
*/
public function userFromToken($token)
{
$user = $this->mapUserToObject($this->getUserByToken($token));
return $user->setToken($token);
}
/**
* Determine if the current request / session has a mismatching "state".
*
* @return bool
*/
protected function hasInvalidState()
{
if ($this->isStateless()) {
return false;
}
$state = $this->request->session()->pull('state');
return empty($state) || $this->request->input('state') !== $state;
}
/**
* Get the access token response for the given code.
*
* @param string $code
* @return array
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => $this->getTokenHeaders($code),
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true);
}
/**
* Get the headers for the access token request.
*
* @param string $code
* @return array
*/
protected function getTokenHeaders($code)
{
return ['Accept' => 'application/json'];
}
/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
protected function getTokenFields($code)
{
$fields = [
'grant_type' => 'authorization_code',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'code' => $code,
'redirect_uri' => $this->redirectUrl,
];
if ($this->usesPKCE()) {
$fields['code_verifier'] = $this->request->session()->pull('code_verifier');
}
return $fields;
}
/**
* Get the code from the request.
*
* @return string
*/
protected function getCode()
{
return $this->request->input('code');
}
/**
* Merge the scopes of the requested access.
*
* @param array|string $scopes
* @return $this
*/
public function scopes($scopes)
{
$this->scopes = array_unique(array_merge($this->scopes, (array) $scopes));
return $this;
}
/**
* Set the scopes of the requested access.
*
* @param array|string $scopes
* @return $this
*/
public function setScopes($scopes)
{
$this->scopes = array_unique((array) $scopes);
return $this;
}
/**
* Get the current scopes.
*
* @return array
*/
public function getScopes()
{
return $this->scopes;
}
/**
* Set the redirect URL.
*
* @param string $url
* @return $this
*/
public function redirectUrl($url)
{
$this->redirectUrl = $url;
return $this;
}
/**
* Get a instance of the Guzzle HTTP client.
*
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
{
if (is_null($this->httpClient)) {
$this->httpClient = new Client($this->guzzle);
}
return $this->httpClient;
}
/**
* Set the Guzzle HTTP client instance.
*
* @param \GuzzleHttp\Client $client
* @return $this
*/
public function setHttpClient(Client $client)
{
$this->httpClient = $client;
return $this;
}
/**
* Set the request instance.
*
* @param \Illuminate\Http\Request $request
* @return $this
*/
public function setRequest(Request $request)
{
$this->request = $request;
return $this;
}
/**
* Determine if the provider is operating with state.
*
* @return bool
*/
protected function usesState()
{
return ! $this->stateless;
}
/**
* Determine if the provider is operating as stateless.
*
* @return bool
*/
protected function isStateless()
{
return $this->stateless;
}
/**
* Indicates that the provider should operate as stateless.
*
* @return $this
*/
public function stateless()
{
$this->stateless = true;
return $this;
}
/**
* Get the string used for session state.
*
* @return string
*/
protected function getState()
{
return Str::random(40);
}
/**
* Determine if the provider uses PKCE.
*
* @return bool
*/
protected function usesPKCE()
{
return $this->usesPKCE;
}
/**
* Enables PKCE for the provider.
*
* @return $this
*/
public function enablePKCE()
{
$this->usesPKCE = true;
return $this;
}
/**
* Generates a random string of the right length for the PKCE code verifier.
*
* @return string
*/
protected function getCodeVerifier()
{
return Str::random(96);
}
/**
* Generates the PKCE code challenge based on the PKCE code verifier in the session.
*
* @return string
*/
protected function getCodeChallenge()
{
$hashed = hash('sha256', $this->request->session()->get('code_verifier'), true);
return rtrim(strtr(base64_encode($hashed), '+/', '-_'), '=');
}
/**
* Returns the hash method used to calculate the PKCE code challenge.
*
* @return string
*/
protected function getCodeChallengeMethod()
{
return 'S256';
}
/**
* Set the custom parameters of the request.
*
* @param array $parameters
* @return $this
*/
public function with(array $parameters)
{
$this->parameters = $parameters;
return $this;
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Laravel\Socialite\Two;
use Exception;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class BitbucketProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['email'];
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://bitbucket.org/site/oauth2/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://bitbucket.org/site/oauth2/access_token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://api.bitbucket.org/2.0/user', [
RequestOptions::QUERY => ['access_token' => $token],
]);
$user = json_decode($response->getBody(), true);
if (in_array('email', $this->scopes, true)) {
$user['email'] = $this->getEmailByToken($token);
}
return $user;
}
/**
* Get the email for the given access token.
*
* @param string $token
* @return string|null
*/
protected function getEmailByToken($token)
{
$emailsUrl = 'https://api.bitbucket.org/2.0/user/emails?access_token='.$token;
try {
$response = $this->getHttpClient()->get($emailsUrl);
} catch (Exception $e) {
return;
}
$emails = json_decode($response->getBody(), true);
foreach ($emails['values'] as $email) {
if ($email['type'] === 'email' && $email['is_primary'] && $email['is_confirmed']) {
return $email['email'];
}
}
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['uuid'],
'nickname' => $user['username'],
'name' => Arr::get($user, 'display_name'),
'email' => Arr::get($user, 'email'),
'avatar' => Arr::get($user, 'links.avatar.href'),
]);
}
/**
* Get the access token for the given code.
*
* @param string $code
* @return string
*/
public function getAccessToken($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::AUTH => [$this->clientId, $this->clientSecret],
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true)['access_token'];
}
}

View File

@@ -0,0 +1,210 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class FacebookProvider extends AbstractProvider implements ProviderInterface
{
/**
* The base Facebook Graph URL.
*
* @var string
*/
protected $graphUrl = 'https://graph.facebook.com';
/**
* The Graph API version for the request.
*
* @var string
*/
protected $version = 'v3.3';
/**
* The user fields being requested.
*
* @var array
*/
protected $fields = ['name', 'email', 'gender', 'verified', 'link'];
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['email'];
/**
* Display the dialog in a popup view.
*
* @var bool
*/
protected $popup = false;
/**
* Re-request a declined permission.
*
* @var bool
*/
protected $reRequest = false;
/**
* The access token that was last used to retrieve a user.
*
* @var string|null
*/
protected $lastToken;
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://www.facebook.com/'.$this->version.'/dialog/oauth', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->graphUrl.'/'.$this->version.'/oauth/access_token';
}
/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);
$data = json_decode($response->getBody(), true);
return Arr::add($data, 'expires_in', Arr::pull($data, 'expires'));
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$this->lastToken = $token;
$params = [
'access_token' => $token,
'fields' => implode(',', $this->fields),
];
if (! empty($this->clientSecret)) {
$params['appsecret_proof'] = hash_hmac('sha256', $token, $this->clientSecret);
}
$response = $this->getHttpClient()->get($this->graphUrl.'/'.$this->version.'/me', [
RequestOptions::HEADERS => [
'Accept' => 'application/json',
],
RequestOptions::QUERY => $params,
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$avatarUrl = $this->graphUrl.'/'.$this->version.'/'.$user['id'].'/picture';
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => null,
'name' => $user['name'] ?? null,
'email' => $user['email'] ?? null,
'avatar' => $avatarUrl.'?type=normal',
'avatar_original' => $avatarUrl.'?width=1920',
'profileUrl' => $user['link'] ?? null,
]);
}
/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);
if ($this->popup) {
$fields['display'] = 'popup';
}
if ($this->reRequest) {
$fields['auth_type'] = 'rerequest';
}
return $fields;
}
/**
* Set the user fields to request from Facebook.
*
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* Set the dialog to be displayed as a popup.
*
* @return $this
*/
public function asPopup()
{
$this->popup = true;
return $this;
}
/**
* Re-request permissions which were previously declined.
*
* @return $this
*/
public function reRequest()
{
$this->reRequest = true;
return $this;
}
/**
* Get the last access token used.
*
* @return string|null
*/
public function lastToken()
{
return $this->lastToken;
}
/**
* Specify which graph version should be used.
*
* @param string $version
* @return $this
*/
public function usingGraphVersion(string $version)
{
$this->version = $version;
return $this;
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Laravel\Socialite\Two;
use Exception;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class GithubProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['user:email'];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://github.com/login/oauth/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://github.com/login/oauth/access_token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$userUrl = 'https://api.github.com/user';
$response = $this->getHttpClient()->get(
$userUrl, $this->getRequestOptions($token)
);
$user = json_decode($response->getBody(), true);
if (in_array('user:email', $this->scopes, true)) {
$user['email'] = $this->getEmailByToken($token);
}
return $user;
}
/**
* Get the email for the given access token.
*
* @param string $token
* @return string|null
*/
protected function getEmailByToken($token)
{
$emailsUrl = 'https://api.github.com/user/emails';
try {
$response = $this->getHttpClient()->get(
$emailsUrl, $this->getRequestOptions($token)
);
} catch (Exception $e) {
return;
}
foreach (json_decode($response->getBody(), true) as $email) {
if ($email['primary'] && $email['verified']) {
return $email['email'];
}
}
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['login'],
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'avatar' => $user['avatar_url'],
]);
}
/**
* Get the default options for an HTTP request.
*
* @param string $token
* @return array
*/
protected function getRequestOptions($token)
{
return [
RequestOptions::HEADERS => [
'Accept' => 'application/vnd.github.v3+json',
'Authorization' => 'token '.$token,
],
];
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\RequestOptions;
class GitlabProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['read_user'];
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* The Gitlab instance host.
*
* @var string
*/
protected $host = 'https://gitlab.com';
/**
* Set the Gitlab instance host.
*
* @param string|null $host
* @return $this
*/
public function setHost($host)
{
if (! empty($host)) {
$this->host = rtrim($host, '/');
}
return $this;
}
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->host.'/oauth/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->host.'/oauth/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->host.'/api/v3/user', [
RequestOptions::QUERY => ['access_token' => $token],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['username'],
'name' => $user['name'],
'email' => $user['email'],
'avatar' => $user['avatar_url'],
]);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class GoogleProvider extends AbstractProvider implements ProviderInterface
{
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = [
'openid',
'profile',
'email',
];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://www.googleapis.com/oauth2/v4/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://www.googleapis.com/oauth2/v3/userinfo', [
RequestOptions::QUERY => [
'prettyPrint' => 'false',
],
RequestOptions::HEADERS => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
// Deprecated: Fields added to keep backwards compatibility in 4.0. These will be removed in 5.0
$user['id'] = Arr::get($user, 'sub');
$user['verified_email'] = Arr::get($user, 'email_verified');
$user['link'] = Arr::get($user, 'profile');
return (new User)->setRaw($user)->map([
'id' => Arr::get($user, 'sub'),
'nickname' => Arr::get($user, 'nickname'),
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'avatar' => $avatarUrl = Arr::get($user, 'picture'),
'avatar_original' => $avatarUrl,
]);
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Laravel\Socialite\Two;
use InvalidArgumentException;
class InvalidStateException extends InvalidArgumentException
{
//
}

View File

@@ -0,0 +1,122 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class LinkedInProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['r_liteprofile', 'r_emailaddress'];
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://www.linkedin.com/oauth/v2/authorization', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://www.linkedin.com/oauth/v2/accessToken';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$basicProfile = $this->getBasicProfile($token);
$emailAddress = $this->getEmailAddress($token);
return array_merge($basicProfile, $emailAddress);
}
/**
* Get the basic profile fields for the user.
*
* @param string $token
* @return array
*/
protected function getBasicProfile($token)
{
$response = $this->getHttpClient()->get('https://api.linkedin.com/v2/me', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
'X-RestLi-Protocol-Version' => '2.0.0',
],
RequestOptions::QUERY => [
'projection' => '(id,firstName,lastName,profilePicture(displayImage~:playableStreams))',
],
]);
return (array) json_decode($response->getBody(), true);
}
/**
* Get the email address for the user.
*
* @param string $token
* @return array
*/
protected function getEmailAddress($token)
{
$response = $this->getHttpClient()->get('https://api.linkedin.com/v2/emailAddress', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
'X-RestLi-Protocol-Version' => '2.0.0',
],
RequestOptions::QUERY => [
'q' => 'members',
'projection' => '(elements*(handle~))',
],
]);
return (array) Arr::get((array) json_decode($response->getBody(), true), 'elements.0.handle~');
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$preferredLocale = Arr::get($user, 'firstName.preferredLocale.language').'_'.Arr::get($user, 'firstName.preferredLocale.country');
$firstName = Arr::get($user, 'firstName.localized.'.$preferredLocale);
$lastName = Arr::get($user, 'lastName.localized.'.$preferredLocale);
$images = (array) Arr::get($user, 'profilePicture.displayImage~.elements', []);
$avatar = Arr::first($images, function ($image) {
return $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] === 100;
});
$originalAvatar = Arr::first($images, function ($image) {
return $image['data']['com.linkedin.digitalmedia.mediaartifact.StillImage']['storageSize']['width'] === 800;
});
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => null,
'name' => $firstName.' '.$lastName,
'first_name' => $firstName,
'last_name' => $lastName,
'email' => Arr::get($user, 'emailAddress'),
'avatar' => Arr::get($avatar, 'identifiers.0.identifier'),
'avatar_original' => Arr::get($originalAvatar, 'identifiers.0.identifier'),
]);
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Laravel\Socialite\Two;
interface ProviderInterface
{
/**
* Redirect the user to the authentication page for the provider.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirect();
/**
* Get the User instance for the authenticated user.
*
* @return \Laravel\Socialite\Two\User
*/
public function user();
}

View File

@@ -0,0 +1,111 @@
<?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'];
/**
* 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;
}
}

View File

@@ -0,0 +1,107 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
class TwitterProvider extends AbstractProvider
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['users.read', 'tweet.read'];
/**
* Indicates if PKCE should be used.
*
* @var bool
*/
protected $usesPKCE = true;
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* The query encoding format.
*
* @var int
*/
protected $encodingType = PHP_QUERY_RFC3986;
/**
* {@inheritdoc}
*/
public function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://twitter.com/i/oauth2/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return 'https://api.twitter.com/2/oauth2/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://api.twitter.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');
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['username'],
'name' => $user['name'],
'avatar' => $user['profile_image_url'],
]);
}
/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
RequestOptions::HEADERS => ['Accept' => 'application/json'],
RequestOptions::AUTH => [$this->clientId, $this->clientSecret],
RequestOptions::FORM_PARAMS => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);
if ($this->isStateless()) {
$fields['state'] = 'state';
}
return $fields;
}
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Laravel\Socialite\Two;
use Laravel\Socialite\AbstractUser;
class User extends AbstractUser
{
/**
* The user's access token.
*
* @var string
*/
public $token;
/**
* The refresh token that can be exchanged for a new access token.
*
* @var string
*/
public $refreshToken;
/**
* The number of seconds the access token is valid for.
*
* @var int
*/
public $expiresIn;
/**
* The scopes the user authorized. The approved scopes may be a subset of the requested scopes.
*
* @var array
*/
public $approvedScopes;
/**
* Set the token on the user.
*
* @param string $token
* @return $this
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Set the refresh token required to obtain a new access token.
*
* @param string $refreshToken
* @return $this
*/
public function setRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;
return $this;
}
/**
* Set the number of seconds the access token is valid for.
*
* @param int $expiresIn
* @return $this
*/
public function setExpiresIn($expiresIn)
{
$this->expiresIn = $expiresIn;
return $this;
}
/**
* Set the scopes that were approved by the user during authentication.
*
* @param array $approvedScopes
* @return $this
*/
public function setApprovedScopes($approvedScopes)
{
$this->approvedScopes = $approvedScopes;
return $this;
}
}