2023-11-20 23:25:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers\Socialite;
|
|
|
|
|
|
|
|
use GuzzleHttp\RequestOptions;
|
2024-07-20 14:32:35 +02:00
|
|
|
use Illuminate\Http\Request;
|
2024-09-25 08:40:08 +02:00
|
|
|
use Laravel\Socialite\Two\ProviderInterface;
|
2023-11-20 23:25:36 +01:00
|
|
|
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
|
|
|
|
use SocialiteProviders\Manager\OAuth2\User;
|
|
|
|
|
2024-09-25 08:40:08 +02:00
|
|
|
class OpenId extends AbstractProvider implements ProviderInterface
|
2023-11-20 23:25:36 +01:00
|
|
|
{
|
|
|
|
public const IDENTIFIER = 'OPENID';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected $scopes = ['openid profile email'];
|
|
|
|
|
2024-07-20 14:32:35 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl, $guzzle = [])
|
|
|
|
{
|
2024-09-25 08:40:08 +02:00
|
|
|
$guzzle = array_merge([
|
2024-07-20 14:32:35 +02:00
|
|
|
'proxy' => config('2fauth.config.outgoingProxy')
|
2024-09-25 08:40:08 +02:00
|
|
|
], $guzzle);
|
|
|
|
|
|
|
|
parent::__construct($request, $clientId, $clientSecret, $redirectUrl, $guzzle);
|
2024-07-20 14:32:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-20 23:25:36 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function additionalConfigKeys()
|
|
|
|
{
|
|
|
|
return ['token_url', 'authorize_url', 'userinfo_url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function getAuthUrl($state)
|
|
|
|
{
|
|
|
|
return $this->buildAuthUrlFromBase($this->getConfig('authorize_url'), $state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function getTokenUrl()
|
|
|
|
{
|
|
|
|
return $this->getConfig('token_url');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function getUserByToken($token)
|
|
|
|
{
|
|
|
|
$response = $this->getHttpClient()->get($this->getConfig('userinfo_url'), [
|
|
|
|
RequestOptions::HEADERS => [
|
2023-12-20 16:55:58 +01:00
|
|
|
'Authorization' => 'Bearer ' . $token,
|
2023-11-20 23:25:36 +01:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return json_decode((string) $response->getBody(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function mapUserToObject(array $user)
|
|
|
|
{
|
|
|
|
return (new User())->setRaw($user)->map([
|
|
|
|
'email' => $user['email'] ?? null,
|
|
|
|
'email_verified' => $user['email_verified'] ?? null,
|
|
|
|
'name' => $user['name'] ?? null,
|
|
|
|
'given_name' => $user['given_name'] ?? null,
|
|
|
|
'family_name' => $user['family_name'] ?? null,
|
|
|
|
'preferred_username' => $user['preferred_username'] ?? null,
|
|
|
|
'nickname' => $user['nickname'] ?? null,
|
|
|
|
'groups' => $user['groups'] ?? null,
|
|
|
|
'id' => $user['sub'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|