amber-laravel/app/Logic/OpenIDLogic.php
2024-07-24 00:40:56 +08:00

57 lines
1.4 KiB
PHP

<?php
namespace App\Logic;
use GuzzleHttp\Client;
class OpenIDLogic
{
public string $auth_url;
public string $token_url;
public string $user_url;
public string $jwks_url;
public array $jwks = [];
/**
* Create a new class instance.
*/
public function __construct()
{
$http = new Client();
$cache_key = 'oauth_discovery';
if (cache()->has($cache_key)) {
$oauth_discovery = cache()->get($cache_key);
} else {
// lock 防止重复请求
$oauth_discovery = cache()->remember($cache_key, 3600, function () use ($http) {
$response = $http->get(config('oauth.discovery'));
return json_decode($response->getBody(), true);
});
}
$this->auth_url = $oauth_discovery['authorization_endpoint'];
$this->token_url = $oauth_discovery['token_endpoint'];
$this->user_url = $oauth_discovery['userinfo_endpoint'];
$this->jwks_url = $oauth_discovery['jwks_uri'];
$cache_key = 'oauth_discovery_jwks';
if (cache()->has($cache_key)) {
$this->jwks = cache()->get($cache_key);
} else {
$this->jwks = cache()->remember($cache_key, 3600, function () use ($http) {
$response = $http->get($this->jwks_url);
return json_decode($response->getBody(), true);
});
}
}
}