update 1.0.8.0

Commits for version update
This commit is contained in:
Manish Verma
2016-10-17 12:02:27 +05:30
parent dec927987b
commit 76e85db070
9674 changed files with 495757 additions and 58922 deletions

View File

@@ -0,0 +1,183 @@
<?php
namespace Laravel\Socialite;
use ArrayAccess;
abstract class AbstractUser implements ArrayAccess, Contracts\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;
/**
* Get the unique identifier for the user.
*
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* Get the nickname / username for the user.
*
* @return string
*/
public function getNickname()
{
return $this->nickname;
}
/**
* Get the full name of the user.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get the e-mail address of the user.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Get the avatar / image URL for the user.
*
* @return string
*/
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)
{
foreach ($attributes as $key => $value) {
$this->{$key} = $value;
}
return $this;
}
/**
* Determine if the given raw user attribute exists.
*
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->user);
}
/**
* Get the given key from the raw user.
*
* @param string $offset
* @return mixed
*/
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
*/
public function offsetSet($offset, $value)
{
$this->user[$offset] = $value;
}
/**
* Unset the given value from the raw user array.
*
* @param string $offset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->user[$offset]);
}
}

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
*/
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
*/
public function getNickname();
/**
* Get the full name of the user.
*
* @return string
*/
public function getName();
/**
* Get the e-mail address of the user.
*
* @return string
*/
public function getEmail();
/**
* Get the avatar / image URL for the user.
*
* @return string
*/
public function getAvatar();
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Laravel\Socialite\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Laravel\Socialite\SocialiteManager
*/
class Socialite extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'Laravel\Socialite\Contracts\Factory';
}
}

View File

@@ -0,0 +1,113 @@
<?php
namespace Laravel\Socialite\One;
use Illuminate\Http\Request;
use InvalidArgumentException;
use League\OAuth1\Client\Server\Server;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Laravel\Socialite\Contracts\Provider as ProviderContract;
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;
/**
* 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 \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirect()
{
$this->request->session()->set(
'oauth.temp', $temp = $this->server->getTemporaryCredentials()
);
return new RedirectResponse($this->server->getAuthorizationUrl($temp));
}
/**
* Get the User instance for the authenticated user.
*
* @throws \InvalidArgumentException
* @return \Laravel\Socialite\One\User
*/
public function user()
{
if (! $this->hasNecessaryVerifier()) {
throw new InvalidArgumentException('Invalid request. Missing OAuth verifier.');
}
$user = $this->server->getUserDetails($token = $this->getToken());
$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 the token credentials for the request.
*
* @return \League\OAuth1\Client\Credentials\TokenCredentials
*/
protected function getToken()
{
$temp = $this->request->session()->get('oauth.temp');
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') && $this->request->has('oauth_verifier');
}
/**
* 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,8 @@
<?php
namespace Laravel\Socialite\One;
class BitbucketProvider extends AbstractProvider
{
//
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Laravel\Socialite\One;
use InvalidArgumentException;
class TwitterProvider extends AbstractProvider
{
/**
* {@inheritdoc}
*/
public function user()
{
if (! $this->hasNecessaryVerifier()) {
throw new InvalidArgumentException('Invalid request. Missing OAuth verifier.');
}
$user = $this->server->getUserDetails($token = $this->getToken());
$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),
]);
}
}

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,150 @@
<?php
namespace Laravel\Socialite;
use InvalidArgumentException;
use Illuminate\Support\Manager;
use Laravel\Socialite\One\TwitterProvider;
use Laravel\Socialite\One\BitbucketProvider;
use League\OAuth1\Client\Server\Twitter as TwitterServer;
use League\OAuth1\Client\Server\Bitbucket as BitbucketServer;
class SocialiteManager extends Manager implements Contracts\Factory
{
/**
* 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->app['config']['services.github'];
return $this->buildProvider(
'Laravel\Socialite\Two\GithubProvider', $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createFacebookDriver()
{
$config = $this->app['config']['services.facebook'];
return $this->buildProvider(
'Laravel\Socialite\Two\FacebookProvider', $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createGoogleDriver()
{
$config = $this->app['config']['services.google'];
return $this->buildProvider(
'Laravel\Socialite\Two\GoogleProvider', $config
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\Two\AbstractProvider
*/
protected function createLinkedinDriver()
{
$config = $this->app['config']['services.linkedin'];
return $this->buildProvider(
'Laravel\Socialite\Two\LinkedInProvider', $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->app['request'], $config['client_id'],
$config['client_secret'], $config['redirect']
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\One\AbstractProvider
*/
protected function createTwitterDriver()
{
$config = $this->app['config']['services.twitter'];
return new TwitterProvider(
$this->app['request'], new TwitterServer($this->formatConfig($config))
);
}
/**
* Create an instance of the specified driver.
*
* @return \Laravel\Socialite\One\AbstractProvider
*/
protected function createBitbucketDriver()
{
$config = $this->app['config']['services.bitbucket'];
return new BitbucketProvider(
$this->app['request'], new BitbucketServer($this->formatConfig($config))
);
}
/**
* 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' => $config['redirect'],
], $config);
}
/**
* Get the default driver name.
*
* @throws \InvalidArgumentException
*
* @return string
*/
public function getDefaultDriver()
{
throw new InvalidArgumentException('No Socialite driver was specified.');
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Laravel\Socialite;
use Illuminate\Support\ServiceProvider;
class SocialiteServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton('Laravel\Socialite\Contracts\Factory', function ($app) {
return new SocialiteManager($app);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['Laravel\Socialite\Contracts\Factory'];
}
}

View File

@@ -0,0 +1,407 @@
<?php
namespace Laravel\Socialite\Two;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use GuzzleHttp\ClientInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
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;
/**
* Create a new provider instance.
*
* @param \Illuminate\Http\Request $request
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUrl
* @return void
*/
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl)
{
$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 \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function redirect()
{
$state = null;
if ($this->usesState()) {
$this->request->session()->set('state', $state = Str::random(40));
}
return new RedirectResponse($this->getAuthUrl($state));
}
/**
* Get the authentication URL for the provider.
*
* @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->scopes, $this->scopeSeparator),
'response_type' => 'code',
];
if ($this->usesState()) {
$fields['state'] = $state;
}
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->hasInvalidState()) {
throw new InvalidStateException;
}
$response = $this->getAccessTokenResponse($this->getCode());
$user = $this->mapUserToObject($this->getUserByToken(
$token = Arr::get($response, 'access_token')
));
return $user->setToken($token)
->setRefreshToken(Arr::get($response, 'refresh_token'))
->setExpiresIn(Arr::get($response, 'expires_in'));
}
/**
* 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;
}
//dd($this->request->all());
$state = $this->request->input('state');
\Session::put('state', $state);
//$state = $this->request->session()->pull('state');
return ! (strlen($state) > 0 && $this->request->input('state') === $state);
}
/**
* Get the access token response for the given code.
*
* @param string $code
* @return array
*/
public function getAccessTokenResponse($code)
{
$postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Accept' => 'application/json'],
$postKey => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true);
}
/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
protected function getTokenFields($code)
{
return [
'client_id' => $this->clientId, 'client_secret' => $this->clientSecret,
'code' => $code, 'redirect_uri' => $this->redirectUrl,
];
}
/**
* Get the code from the request.
*
* @return string
*/
protected function getCode()
{
return $this->request->input('code');
}
/**
* Set the scopes of the requested access.
*
* @param array $scopes
* @return $this
*/
public function scopes(array $scopes)
{
$this->scopes = array_unique(array_merge($this->scopes, $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();
}
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;
}
/**
* 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,174 @@
<?php
namespace Laravel\Socialite\Two;
use Illuminate\Support\Arr;
use GuzzleHttp\ClientInterface;
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 = 'v2.6';
/**
* The user fields being requested.
*
* @var array
*/
protected $fields = ['name', 'email', 'gender', 'verified'];
/**
* 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;
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase('https://www.facebook.com/'.$this->version.'/dialog/oauth', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->graphUrl.'/oauth/access_token';
}
/**
* {@inheritdoc}
*/
public function getAccessTokenResponse($code)
{
$postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
$postKey => $this->getTokenFields($code),
]);
$data = [];
parse_str($response->getBody(), $data);
return Arr::add($data, 'expires_in', Arr::pull($data, 'expires'));
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$meUrl = $this->graphUrl.'/'.$this->version.'/me?access_token='.$token.'&fields='.implode(',', $this->fields);
if (! empty($this->clientSecret)) {
$appSecretProof = hash_hmac('sha256', $token, $this->clientSecret);
$meUrl .= '&appsecret_proof='.$appSecretProof;
}
$response = $this->getHttpClient()->get($meUrl, [
'headers' => [
'Accept' => 'application/json',
],
]);
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' => isset($user['name']) ? $user['name'] : null,
'email' => isset($user['email']) ? $user['email'] : null, 'avatar' => $avatarUrl.'?type=normal',
'avatar_original' => $avatarUrl.'?width=1920',
]);
}
/**
* {@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;
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Laravel\Socialite\Two;
use Exception;
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?access_token='.$token;
$response = $this->getHttpClient()->get(
$userUrl, $this->getRequestOptions()
);
$user = json_decode($response->getBody(), true);
if (in_array('user:email', $this->scopes)) {
$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?access_token='.$token;
try {
$response = $this->getHttpClient()->get(
$emailsUrl, $this->getRequestOptions()
);
} 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.
*
* @return array
*/
protected function getRequestOptions()
{
return [
'headers' => [
'Accept' => 'application/vnd.github.v3+json',
],
];
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Laravel\Socialite\Two;
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://accounts.google.com/o/oauth2/token';
}
/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
protected function getTokenFields($code)
{
return array_add(
parent::getTokenFields($code), 'grant_type', 'authorization_code'
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get('https://www.googleapis.com/plus/v1/people/me?', [
'query' => [
'prettyPrint' => 'false',
],
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'], 'nickname' => Arr::get($user, 'nickname'), 'name' => $user['displayName'],
'email' => $user['emails'][0]['value'], 'avatar' => Arr::get($user, 'image')['url'],
]);
}
}

View File

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

View File

@@ -0,0 +1,104 @@
<?php
namespace Laravel\Socialite\Two;
use Illuminate\Support\Arr;
class LinkedInProvider extends AbstractProvider implements ProviderInterface
{
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['r_basicprofile', 'r_emailaddress'];
/**
* The separating character for the requested scopes.
*
* @var string
*/
protected $scopeSeparator = ' ';
/**
* The fields that are included in the profile.
*
* @var array
*/
protected $fields = [
'id', 'first-name', 'last-name', 'formatted-name',
'email-address', 'headline', 'location', 'industry',
'public-profile-url', 'picture-url', 'picture-urls::(original)',
];
/**
* {@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';
}
/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
protected function getTokenFields($code)
{
return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$fields = implode(',', $this->fields);
$url = 'https://api.linkedin.com/v1/people/~:('.$fields.')';
$response = $this->getHttpClient()->get($url, [
'headers' => [
'x-li-format' => 'json',
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'], 'nickname' => null, 'name' => Arr::get($user, 'formattedName'),
'email' => Arr::get($user, 'emailAddress'), 'avatar' => Arr::get($user, 'pictureUrl'),
'avatar_original' => Arr::get($user, 'pictureUrls.values.0'),
]);
}
/**
* Set the user fields to request from LinkedIn.
*
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
}

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,68 @@
<?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;
/**
* 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;
}
}