63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Others\Authentik\Providers;
|
|
|
|
use Laravel\Socialite\Two\AbstractProvider;
|
|
use Laravel\Socialite\Two\User;
|
|
|
|
class AuthentikProvider extends AbstractProvider
|
|
{
|
|
public $scopes = ['openid', 'email', 'profile'];
|
|
protected $scopeSeparator = ' ';
|
|
|
|
public function getAuthUrl($state)
|
|
{
|
|
return $this->buildAuthUrlFromBase(
|
|
$this->getBaseUrl() . '/application/o/authorize/',
|
|
$state
|
|
);
|
|
}
|
|
|
|
public function getTokenUrl()
|
|
{
|
|
return $this->getBaseUrl() . '/application/o/token/';
|
|
}
|
|
|
|
public function getUserByToken($token)
|
|
{
|
|
$response = $this->getHttpClient()->get(
|
|
$this->getBaseUrl() . '/application/o/userinfo/',
|
|
['headers' => ['Authorization' => 'Bearer ' . $token]]
|
|
);
|
|
|
|
return json_decode($response->getBody(), true);
|
|
}
|
|
|
|
public function mapUserToObject(array $user)
|
|
{
|
|
return (new User)->setRaw($user)->map([
|
|
'id' => $user['sub'],
|
|
'name' => $user['name'] ?? $user['preferred_username'] ?? $user['sub'],
|
|
'email' => $user['email'] ?? $user['sub'] . '@authentik.local',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Obtenir l'URL de base d'Authentik depuis la configuration
|
|
*/
|
|
private function getBaseUrl()
|
|
{
|
|
// Récupérer depuis la configuration de l'extension
|
|
$extension = \App\Models\Extension::where('extension', 'Authentik')->first();
|
|
if ($extension && $extension->settings) {
|
|
$baseUrl = $extension->settings->where('key', 'base_url')->first();
|
|
if ($baseUrl) {
|
|
return $baseUrl->value;
|
|
}
|
|
}
|
|
|
|
// Fallback vers l'ancienne configuration
|
|
return config('settings.oauth_authentik_base_url');
|
|
}
|
|
}
|