Skip to content

Instantly share code, notes, and snippets.

@rslhdyt
Last active June 25, 2019 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rslhdyt/1852f6aafb5de7220c6a27b734c7caa9 to your computer and use it in GitHub Desktop.
Save rslhdyt/1852f6aafb5de7220c6a27b734c7caa9 to your computer and use it in GitHub Desktop.
[Socialite Custom Driver Test] Mock socialite with custom driver #laravel #php #socialite #mock #phpunit
// app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\FooApp\FooAppSocialite;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'foo_app',
function ($app) use ($socialite) {
$config = $app['config']['services']['foo_app'];
return $socialite->buildProvider(FooAppSocialite::class, $config);
}
);
}
}
// app/Services/FooApp/FooAppSocialite.php
<?php
namespace App\Services\FooApp;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class FooAppSocialite extends AbstractProvider implements ProviderInterface
{
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase(config('services.foo_app.url') . '/oauth/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return config('services.foo_app.sso_url') . '/oauth/token';
}
/**
* {@inheritdoc}
*/
public function getAccessToken($code)
{
$response = $this->getHttpClient()->post($this->getTokenUrl(), [
'headers' => ['Authorization' => 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret)],
'body' => $this->getTokenFields($code),
]);
return json_decode($response->getBody(), true)['access_token'];
}
public function updateUser($user, $params)
{
$response = $this->getHttpClient()->put(
config('services.foo_app.url') . '/api/v1/users/' . $user->provider_id,
[
'headers' => ['Authorization' => 'Bearer ' . $user->provider_token],
'form_params' => $params,
]
);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function getTokenFields($code)
{
return array_add(
parent::getTokenFields($code),
'grant_type',
'authorization_code'
);
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(config('services.foo_app.sso_url') . '/api/v1/auth/user', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($response->getBody(), true);
}
/**
* {@inheritdoc}
*/
protected function formatScopes(array $scopes, $scopeSeparator = ' ')
{
return implode($scopeSeparator, $scopes);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
$user = $user['data'];
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'name' => $user['name'],
'username' => $user['username'],
'email' => $user['email'],
'avatar' => !empty($user['images']) ? $user['images'][0]['url'] : null,
'status' => $user['status'],
]);
}
}
// config/services.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Stripe, Mailgun, SparkPost and others. This file provides a sane
| default location for this type of information, allowing packages
| to have a conventional place to find your various credentials.
|
*/
....
'foo_app' => [
'url' => env('FOO_APP_URL'),
'client_id' => env('FOO_APP_CLIENT_ID'),
'client_secret' => env('FOO_APP_CLIENT_SECRET'),
'redirect' => env('FOO_APP_CALLBACK_URL', 'http://fooapp.local/oauth/callback'),
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment