bifurqué depuis D3Public/oxtotp
96 lignes
3.3 KiB
PHP
96 lignes
3.3 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace D3\Totp\Application\Model\Webauthn;
|
||
|
|
||
|
use D3\Totp\Modules\Application\Model\d3_totp_user;
|
||
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
||
|
use Nyholm\Psr7Server\ServerRequestCreator;
|
||
|
use OxidEsales\Eshop\Application\Model\User;
|
||
|
use OxidEsales\Eshop\Core\Registry;
|
||
|
use Webauthn\PublicKeyCredentialCreationOptions;
|
||
|
use Webauthn\PublicKeyCredentialRpEntity;
|
||
|
use Webauthn\PublicKeyCredentialSource;
|
||
|
use Webauthn\Server;
|
||
|
|
||
|
class Webauthn
|
||
|
{
|
||
|
public const SESSION_CREATIONS_OPTIONS = 'd3WebAuthnCreationOptions';
|
||
|
public const SESSION_USERENTITY = 'd3WebAuthnUserEntity';
|
||
|
|
||
|
public function getCreationOptions()
|
||
|
{
|
||
|
/** @var d3_totp_user $user */
|
||
|
$user = oxNew(User::class);
|
||
|
$user->load('oxdefaultadmin');
|
||
|
$userEntity = $user->d3GetWebauthnUserEntity();
|
||
|
|
||
|
Registry::getSession()->setVariable(self::SESSION_USERENTITY, $userEntity);
|
||
|
|
||
|
$credentialSourceRepository = new PublicKeyCredentials();
|
||
|
$credentialSources = $credentialSourceRepository->findAllForUserEntity($userEntity);
|
||
|
$excludeCredentials = array_map(function (PublicKeyCredentialSource $credential) {
|
||
|
return $credential->getPublicKeyCredentialDescriptor();
|
||
|
}, $credentialSources);
|
||
|
|
||
|
$server = $this->getServer();
|
||
|
$publicKeyCredentialCreationOptions = $server->generatePublicKeyCredentialCreationOptions(
|
||
|
$userEntity,
|
||
|
PublicKeyCredentialCreationOptions::ATTESTATION_CONVEYANCE_PREFERENCE_NONE,
|
||
|
$excludeCredentials
|
||
|
);
|
||
|
|
||
|
Registry::getSession()->setVariable(self::SESSION_CREATIONS_OPTIONS, $publicKeyCredentialCreationOptions);
|
||
|
|
||
|
return json_encode($publicKeyCredentialCreationOptions);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return Server
|
||
|
*/
|
||
|
public function getServer()
|
||
|
{
|
||
|
$rpEntity = new PublicKeyCredentialRpEntity(
|
||
|
Registry::getConfig()->getActiveShop()->getFieldData('oxname'),
|
||
|
preg_replace('/(^www\.)(.*)/mi', '$2', $_SERVER['HTTP_HOST'])
|
||
|
);
|
||
|
|
||
|
return new Server($rpEntity, new PublicKeyCredentials());
|
||
|
}
|
||
|
|
||
|
public function saveAuthn(string $credential)
|
||
|
{
|
||
|
try {
|
||
|
$psr17Factory = new Psr17Factory();
|
||
|
$creator = new ServerRequestCreator(
|
||
|
$psr17Factory,
|
||
|
$psr17Factory,
|
||
|
$psr17Factory,
|
||
|
$psr17Factory
|
||
|
);
|
||
|
$serverRequest = $creator->fromGlobals();
|
||
|
|
||
|
$publicKeyCredentialSource = $this->getServer()->loadAndCheckAttestationResponse(
|
||
|
html_entity_decode($credential),
|
||
|
Registry::getSession()->getVariable(self::SESSION_CREATIONS_OPTIONS),
|
||
|
$serverRequest
|
||
|
);
|
||
|
|
||
|
dumpvar($publicKeyCredentialSource);
|
||
|
dumpvar(serialize($publicKeyCredentialSource));
|
||
|
dumpvar(unserialize(serialize($publicKeyCredentialSource)));
|
||
|
echo "<hr>";
|
||
|
dumpvar(bin2hex(serialize($publicKeyCredentialSource)));
|
||
|
dumpvar(unserialize(hex2bin(bin2hex(serialize($publicKeyCredentialSource)))));
|
||
|
|
||
|
$pkCredential = oxNew(PublicKeyCredentials::class);
|
||
|
$pkCredential->saveCredentialSource($publicKeyCredentialSource);
|
||
|
} catch (\Exception $e) {
|
||
|
dumpvar($e->getMessage());
|
||
|
dumpvar($e);
|
||
|
|
||
|
die();
|
||
|
}
|
||
|
}
|
||
|
}
|