Laravel 5.5
我在创建自定义 oAuth 提供程序时也遇到了这个问题。经过长时间的研究,我通过创建MySocialServiceProvider
需要扩展的自定义类来实现Laravel\Socialite\SocialiteServiceProvider
。请通过以下所有代码并使用适当的配置进行设置,它肯定会起作用。
我的目录结构如下图所示
![在此处输入图像描述](https://i.stack.imgur.com/llmBV.png)
MySocialServiceProvider.php
<?php
namespace App\Providers;
use Laravel\Socialite\SocialiteServiceProvider;
class MySocialServiceProvider extends SocialiteServiceProvider
{
public function register()
{
$this->app->bind('Laravel\Socialite\Contracts\Factory', function ($app) {
return new MySocialManager($app);
});
}
}
我们必须创建一个 Manger 类,其中包含以下内容
MySocialManager.php
<?php
namespace App\Providers;
use App\Auth\SocialiteFooDriver;
use Laravel\Socialite\SocialiteManager;
class MySocialManager extends SocialiteManager
{
protected function createFooDriver()
{
$config = $this->app['config']['services.foo'];
return $this->buildProvider(
SocialiteFooDriver::class, $config
);
}
}
我们应该创建一个自定义驱动程序,由MySocialManger
SocialiteFooDriver.php
<?php
namespace App\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;
class SocialiteFooDriver extends AbstractProvider implements ProviderInterface
{
/**
* Foo API endpoint.
*
* @var string
*/
// protected $apiUrl = 'https://auth.foobar.com';
protected $apiUrl = '';
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl)
{
parent::__construct($request, $clientId, $clientSecret, $redirectUrl);
$this->apiUrl = config('services.foo.url');
}
/**
* The scopes being requested.
*
* @var array
*/
protected $scopes = ['openid email profile user_role user_full_name'];
/**
* {@inheritdoc}
*/
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->apiUrl.'/oauth2/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getTokenUrl()
{
return $this->apiUrl.'/oauth2/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$userUrl = $this->apiUrl.'/oauth2/UserInfo?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 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 mapUserToObject(array $user)
{
return (new User())->setRaw($user)->map([
'id' => $user['sub'],
'nickname' => $user['preferred_username'],
'name' => Arr::get($user, 'name'),
'email' => Arr::get($user, 'email'),
'avatar' => $user['avatar'],
]);
}
/**
* Get the default options for an HTTP request.
*
* @return array
*/
protected function getRequestOptions()
{
return [
'headers' => [
//'Accept' => 'application/vnd.github.v3+json',
],
];
}
}
最后我们应该在 config/services.php 中添加配置值
'foo' => [
'client_id' => 'XXXXXXXX',
'client_secret' => 'YYYYYYYY',
'redirect' => 'http://example.com/login/foo/callback/',
'url' => 'https://auth.foobar.com',
],
不要忘记使用我们的新提供商更新 config/app.php
'providers' => [
//...
\App\Providers\MySocialServiceProvider::class
]