Add ability to load an API key in via fixtures.
This commit is contained in:
parent
419d524744
commit
edd21605c0
|
@ -10,8 +10,10 @@ COMPOSER_PLUGIN_MODE=false
|
|||
# Populate these!
|
||||
INIT_BASE_URL=docker.local
|
||||
INIT_INSTANCE_NAME=local development
|
||||
INIT_DEMO_API_KEY=
|
||||
INIT_ADMIN_USERNAME=
|
||||
INIT_ADMIN_PASSWORD=
|
||||
INIT_ADMIN_API_KEY=
|
||||
INIT_MUSIC_PATH=/var/azuracast/www/util/fixtures/init_music
|
||||
|
||||
#
|
||||
|
|
|
@ -9,6 +9,8 @@ use Doctrine\ORM\Mapping as ORM;
|
|||
*/
|
||||
class ApiKey implements \JsonSerializable
|
||||
{
|
||||
public const SEPARATOR = ':';
|
||||
|
||||
use Traits\TruncateStrings;
|
||||
|
||||
/**
|
||||
|
@ -45,9 +47,16 @@ class ApiKey implements \JsonSerializable
|
|||
*/
|
||||
protected $comment;
|
||||
|
||||
public function __construct(User $user)
|
||||
public function __construct(User $user, $key = null)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
if (null !== $key) {
|
||||
[$identifier, $verifier] = explode(self::SEPARATOR, $key);
|
||||
|
||||
$this->id = $identifier;
|
||||
$this->verifier = $this->hashVerifier($verifier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,7 +73,7 @@ class ApiKey implements \JsonSerializable
|
|||
$verifier = substr($random_str, 16, 32);
|
||||
|
||||
$this->id = $identifier;
|
||||
$this->verifier = hash('sha512', $verifier);
|
||||
$this->verifier = $this->hashVerifier($verifier);
|
||||
|
||||
return [$identifier, $verifier];
|
||||
}
|
||||
|
@ -84,8 +93,7 @@ class ApiKey implements \JsonSerializable
|
|||
*/
|
||||
public function verify($verifier): bool
|
||||
{
|
||||
$verifier_to_compare = hash('sha512', $verifier);
|
||||
return hash_equals($this->verifier, $verifier_to_compare);
|
||||
return hash_equals($this->verifier, $this->hashVerifier($verifier));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,4 +127,13 @@ class ApiKey implements \JsonSerializable
|
|||
'comment' => $this->comment,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $original
|
||||
* @return string The hashed verifier.
|
||||
*/
|
||||
protected function hashVerifier($original): string
|
||||
{
|
||||
return hash('sha512', $original);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
namespace App\Entity\Fixture;
|
||||
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use App\Entity;
|
||||
|
||||
class ApiKey extends AbstractFixture implements DependentFixtureInterface
|
||||
{
|
||||
public function load(ObjectManager $em)
|
||||
{
|
||||
$demo_api_key = getenv('INIT_DEMO_API_KEY');
|
||||
|
||||
if (!empty($demo_api_key) && $this->hasReference('demo_user')) {
|
||||
/** @var Entity\User $demo_user */
|
||||
$demo_user = $this->getReference('demo_user');
|
||||
|
||||
$api_key = new Entity\ApiKey($demo_user, $demo_api_key);
|
||||
$api_key->setComment('Demo User');
|
||||
|
||||
$em->persist($api_key);
|
||||
}
|
||||
|
||||
$admin_api_key = getenv('INIT_ADMIN_API_KEY');
|
||||
|
||||
if (!empty($admin_api_key) && $this->hasReference('admin_user')) {
|
||||
/** @var Entity\User $admin_user */
|
||||
$admin_user = $this->getReference('admin_user');
|
||||
|
||||
$api_key = new Entity\ApiKey($admin_user, $admin_api_key);
|
||||
$api_key->setComment('Administrator');
|
||||
|
||||
$em->persist($api_key);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
}
|
||||
|
||||
public function getDependencies()
|
||||
{
|
||||
return [
|
||||
User::class,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -17,6 +17,8 @@ class User extends AbstractFixture implements DependentFixtureInterface
|
|||
$demo_user->getRoles()->add($this->getReference('demo_role'));
|
||||
$em->persist($demo_user);
|
||||
|
||||
$this->addReference('demo_user', $demo_user);
|
||||
|
||||
$admin_username = getenv('INIT_ADMIN_USERNAME');
|
||||
$admin_password = getenv('INIT_ADMIN_PASSWORD');
|
||||
|
||||
|
@ -36,6 +38,8 @@ class User extends AbstractFixture implements DependentFixtureInterface
|
|||
}
|
||||
|
||||
$em->persist($admin_user);
|
||||
|
||||
$this->addReference('admin_user', $admin_user);
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
|
|
|
@ -16,7 +16,7 @@ $assets->load('clipboard');
|
|||
<p><?=__('Your full API key is below:') ?></p>
|
||||
|
||||
<div class="well">
|
||||
<code id="api_key"><?=$key_identifier ?>:<?=$key_verifier ?></code>
|
||||
<code id="api_key"><?=$key_identifier ?><?=\App\Entity\ApiKey::SEPARATOR ?><?=$key_verifier ?></code>
|
||||
<div class="buttons">
|
||||
<button class="btn btn-copy btn-default btn-sm" data-clipboard-target="#api_key"><?=__('Copy to Clipboard') ?></button>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue