2015-05-22 07:26:49 +00:00
|
|
|
<?php
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Entity;
|
2015-05-22 07:26:49 +00:00
|
|
|
|
2019-08-14 23:50:53 +00:00
|
|
|
use App\Annotations\AuditLog;
|
2018-12-22 00:01:04 +00:00
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
2019-09-04 18:00:51 +00:00
|
|
|
use Exception;
|
|
|
|
use JsonSerializable;
|
2018-12-22 00:01:04 +00:00
|
|
|
|
2015-05-22 07:26:49 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Table(name="api_keys")
|
2019-09-29 07:50:24 +00:00
|
|
|
* @ORM\Entity()
|
2019-08-14 23:50:53 +00:00
|
|
|
*
|
|
|
|
* @AuditLog\Auditable
|
2015-05-22 07:26:49 +00:00
|
|
|
*/
|
2019-09-04 18:00:51 +00:00
|
|
|
class ApiKey implements JsonSerializable
|
2015-05-22 07:26:49 +00:00
|
|
|
{
|
2019-07-10 19:55:17 +00:00
|
|
|
public const SEPARATOR = ':';
|
|
|
|
|
2018-04-06 21:29:27 +00:00
|
|
|
use Traits\TruncateStrings;
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="id", type="string", length=16)
|
|
|
|
* @ORM\Id
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id;
|
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="verifier", type="string", length=128, nullable=false)
|
2019-08-15 19:01:00 +00:00
|
|
|
*
|
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
2018-02-06 11:09:05 +00:00
|
|
|
protected $verifier;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="user_id", type="integer")
|
2019-08-15 19:01:00 +00:00
|
|
|
*
|
|
|
|
* @AuditLog\AuditIgnore()
|
|
|
|
*
|
2017-08-17 18:28:48 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2018-02-06 11:09:05 +00:00
|
|
|
protected $user_id;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\ManyToOne(targetEntity="User", inversedBy="api_keys", fetch="EAGER")
|
2018-12-22 00:01:04 +00:00
|
|
|
* @ORM\JoinColumns({
|
|
|
|
* @ORM\JoinColumn(name="user_id", referencedColumnName="uid", onDelete="CASCADE")
|
2018-02-06 11:09:05 +00:00
|
|
|
* })
|
|
|
|
* @var User
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
2018-02-06 11:09:05 +00:00
|
|
|
protected $user;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-20 11:33:49 +00:00
|
|
|
* @ORM\Column(name="comment", type="string", length=255, nullable=true)
|
2018-02-06 11:09:05 +00:00
|
|
|
* @var string|null
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
2018-02-06 11:09:05 +00:00
|
|
|
protected $comment;
|
2017-08-17 18:28:48 +00:00
|
|
|
|
2019-07-10 23:44:23 +00:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string|null $key An existing API key to import (if one exists).
|
|
|
|
*/
|
2020-04-17 05:37:52 +00:00
|
|
|
public function __construct(User $user, ?string $key = null)
|
2018-02-06 11:09:05 +00:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2019-07-10 19:55:17 +00:00
|
|
|
|
|
|
|
if (null !== $key) {
|
|
|
|
[$identifier, $verifier] = explode(self::SEPARATOR, $key);
|
|
|
|
|
|
|
|
$this->id = $identifier;
|
|
|
|
$this->verifier = $this->hashVerifier($verifier);
|
|
|
|
}
|
2017-01-24 00:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:00:51 +00:00
|
|
|
/**
|
|
|
|
* @param string $original
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2019-09-04 18:00:51 +00:00
|
|
|
* @return string The hashed verifier.
|
|
|
|
*/
|
|
|
|
protected function hashVerifier(string $original): string
|
|
|
|
{
|
|
|
|
return hash('sha512', $original);
|
|
|
|
}
|
|
|
|
|
2017-08-17 18:28:48 +00:00
|
|
|
/**
|
2018-02-06 11:09:05 +00:00
|
|
|
* Generate a unique identifier and return both the identifier and verifier.
|
|
|
|
*
|
|
|
|
* @return array [identifier, verifier]
|
2019-09-04 18:00:51 +00:00
|
|
|
* @throws Exception
|
2017-08-17 18:28:48 +00:00
|
|
|
*/
|
2018-02-06 11:09:05 +00:00
|
|
|
public function generate(): array
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2018-02-06 11:09:05 +00:00
|
|
|
$random_str = hash('sha256', random_bytes(32));
|
|
|
|
|
|
|
|
$identifier = substr($random_str, 0, 16);
|
|
|
|
$verifier = substr($random_str, 16, 32);
|
|
|
|
|
|
|
|
$this->id = $identifier;
|
2019-07-10 19:55:17 +00:00
|
|
|
$this->verifier = $this->hashVerifier($verifier);
|
2018-02-06 11:09:05 +00:00
|
|
|
|
|
|
|
return [$identifier, $verifier];
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
public function getId(): string
|
2017-01-24 00:17:50 +00:00
|
|
|
{
|
2018-02-06 11:09:05 +00:00
|
|
|
return $this->id;
|
2017-01-24 00:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 07:26:49 +00:00
|
|
|
/**
|
2018-02-06 11:09:05 +00:00
|
|
|
* Verify an incoming API key against the verifier on this record.
|
|
|
|
*
|
2019-12-07 00:57:50 +00:00
|
|
|
* @param string $verifier
|
|
|
|
*
|
2018-02-06 11:09:05 +00:00
|
|
|
* @return bool
|
2015-05-22 07:26:49 +00:00
|
|
|
*/
|
2020-04-17 05:37:52 +00:00
|
|
|
public function verify(string $verifier): bool
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2019-07-10 19:55:17 +00:00
|
|
|
return hash_equals($this->verifier, $this->hashVerifier($verifier));
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2015-05-22 07:26:49 +00:00
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
public function getUser(): User
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2018-02-06 11:09:05 +00:00
|
|
|
return $this->user;
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2015-05-22 07:26:49 +00:00
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
/**
|
2019-08-14 23:50:53 +00:00
|
|
|
* @AuditLog\AuditIdentifier
|
2018-02-06 11:09:05 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getComment(): ?string
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2018-02-06 11:09:05 +00:00
|
|
|
return $this->comment;
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2015-05-22 07:26:49 +00:00
|
|
|
|
2018-02-06 11:09:05 +00:00
|
|
|
public function setComment(?string $comment): void
|
2017-08-17 18:28:48 +00:00
|
|
|
{
|
2020-03-29 07:16:41 +00:00
|
|
|
$this->comment = $this->truncateString($comment);
|
2017-08-17 18:28:48 +00:00
|
|
|
}
|
2019-01-04 17:32:11 +00:00
|
|
|
|
|
|
|
public function jsonSerialize()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'comment' => $this->comment,
|
|
|
|
];
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|