Skip to content

Instantly share code, notes, and snippets.

@WyattCast44
Last active March 23, 2019 21:58
Show Gist options
  • Save WyattCast44/f5987509f2f285a079e62e9f276cab01 to your computer and use it in GitHub Desktop.
Save WyattCast44/f5987509f2f285a079e62e9f276cab01 to your computer and use it in GitHub Desktop.
<?php
namespace App\OAuth;
use Exception;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\User;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
class Provider extends AbstractProvider implements ProviderInterface
{
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(config('services.provider.auth_url'), $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return config('services.provider.token_url');
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$userUrl = config('services.provider.user_url');
$response = $this->getHttpClient()->get(
$userUrl,
$this->getRequestOptions($token)
);
$user = json_decode($response->getBody(), true);
return $user;
}
/**
* Get the email for the given access token.
*
* @param string $token
* @return string|null
*/
protected function getEmailByToken($token)
{
$emailsUrl = config('services.provider.email_url');
try {
$response = $this->getHttpClient()->get(
$emailsUrl,
$this->getRequestOptions($token)
);
} catch (Exception $e) {
return;
}
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject($user)
{
return (new User)->setRaw($user)->map(
[
'id' => $user['id'],
'name' => Arr::get($user, 'name'),
'nickname' => Arr::get($user, 'nickname'),
'email' => Arr::get($user, 'email'),
]
);
}
/**
* Get the default options for an HTTP request.
*
* @return array
*/
protected function getRequestOptions($token)
{
return [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token
],
];
}
/**
* Get the POST fields for the token request.
*
* @param string $code
* @return array
*/
protected function getTokenFields($code)
{
return [
'grant_type' => 'authorization_code',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'code' => $code,
'redirect_uri' => $this->redirectUrl,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment