150 lines
4.8 KiB
PHP
150 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\UserOauthBundle\ResourceOwner;
|
|
|
|
use HWI\Bundle\OAuthBundle\OAuth\RequestDataStorageInterface;
|
|
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
|
|
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
|
|
use KupShop\AgeVerifyBundle\Utils\AgeVerifyUtil;
|
|
use Query\Operator;
|
|
use Symfony\Component\Security\Http\HttpUtils;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class MojeIDResourceOwner extends GenericOAuth2ResourceOwner implements ICustomResourceOwner
|
|
{
|
|
// pro testování lze pouzit testovaci ucet https://mojeid.regtest.nic.cz/index.html
|
|
public const SANDBOX_URL = 'https://mojeid.regtest.nic.cz/oidc/';
|
|
public const PROD_URL = 'https://mojeid.cz/oidc/';
|
|
|
|
private ?AgeVerifyUtil $ageVerifyUtil = null;
|
|
|
|
public function __construct(
|
|
HttpClientInterface $httpClient,
|
|
HttpUtils $httpUtils,
|
|
array $options,
|
|
string $name,
|
|
RequestDataStorageInterface $storage,
|
|
) {
|
|
if (!isLocalDevelopment()) {
|
|
$options['client_id'] = $this->getClientId();
|
|
$options['client_secret'] = $this->getClientSecret();
|
|
}
|
|
$options['access_token_url'] = $this->getUrl('token/');
|
|
$options['authorization_url'] = $this->getUrl('authorization/');
|
|
$options['infos_url'] = $this->getUrl('userinfo/');
|
|
|
|
parent::__construct($httpClient, $httpUtils, $options, $name, $storage);
|
|
}
|
|
|
|
protected function getUrl($path): string
|
|
{
|
|
if (isDevelopment()) {
|
|
return self::SANDBOX_URL.$path;
|
|
}
|
|
|
|
return self::PROD_URL.$path;
|
|
}
|
|
|
|
protected function getClientId()
|
|
{
|
|
if (isLocalDevelopment()) {
|
|
return $this->options['client_id'];
|
|
}
|
|
$settings = \Settings::getDefault();
|
|
|
|
return $settings['oauth']['mojeid']['client_id'] ?? '';
|
|
}
|
|
|
|
protected function getClientSecret()
|
|
{
|
|
if (isLocalDevelopment()) {
|
|
return $this->options['client_secret'];
|
|
}
|
|
|
|
$settings = \Settings::getDefault();
|
|
|
|
return $settings['oauth']['mojeid']['client_secret'] ?? '';
|
|
}
|
|
|
|
protected function doGetTokenRequest($url, array $parameters = [])
|
|
{
|
|
$parameters['client_id'] = $this->getClientId();
|
|
$parameters['client_secret'] = $this->getClientSecret();
|
|
|
|
return $this->httpRequest($url, http_build_query($parameters, '', '&'));
|
|
}
|
|
|
|
public function getAuthorizationUrl($redirectUri, array $extraParameters = [])
|
|
{
|
|
return parent::getAuthorizationUrl($redirectUri, [
|
|
'claims' => json_encode([
|
|
'id_token' => [
|
|
'birthdate' => [
|
|
'essential' => true,
|
|
],
|
|
'name' => [
|
|
'essential' => true,
|
|
],
|
|
'given_name' => [
|
|
'essential' => true,
|
|
],
|
|
'family_name' => [
|
|
'essential' => true,
|
|
],
|
|
'email' => [
|
|
'essential' => true,
|
|
],
|
|
'address' => [
|
|
'essential' => false,
|
|
],
|
|
'mojeid_valid' => ['essential' => true],
|
|
],
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public function updateUserData(\User $user, UserResponseInterface $response)
|
|
{
|
|
$data = [];
|
|
if ($birthdate = $response->getBirthdate()) {
|
|
$data['birthdate'] = $birthdate;
|
|
}
|
|
|
|
if ($name = $response->getFirstName()) {
|
|
$data['name'] = $name;
|
|
}
|
|
|
|
if ($surname = $response->getLastName()) {
|
|
$data['surname'] = $surname;
|
|
}
|
|
|
|
if ($address = $response->getAddress()) {
|
|
$data['street'] = $address['street_address'] ?? '';
|
|
$data['city'] = $address['locality'] ?? '';
|
|
$data['zip'] = $address['postal_code'] ?? '';
|
|
$data['country'] = $address['country'] ?? '';
|
|
}
|
|
|
|
if (!empty($data)) {
|
|
sqlQueryBuilder()->update('users')
|
|
->directValues($data)
|
|
->where(Operator::equals(['id' => $user->id]))
|
|
->execute();
|
|
}
|
|
|
|
if ($birthdate) {
|
|
$date = \DateTime::createFromFormat('Y-m-d', $birthdate)->add(\DateInterval::createFromDateString('+18YEARS'));
|
|
$this->ageVerifyUtil?->setVerificationData(legalAge: $date <= (new \DateTime()) ? 'Y' : 'N', type: 'mojeid', userId: $user->id);
|
|
}
|
|
}
|
|
|
|
#[Required]
|
|
public function setAgeVerifyUtil(?AgeVerifyUtil $ageVerifyUtil): void
|
|
{
|
|
$this->ageVerifyUtil = $ageVerifyUtil;
|
|
}
|
|
}
|