82 lines
1.8 KiB
PHP
82 lines
1.8 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace VeruA\DomainObjects;
|
|
|
|
use VeruA\DomainObjects\{
|
|
DomainObject,
|
|
Owner
|
|
};
|
|
use VeruA\DomainObjects\ValueObjects\{
|
|
Key,
|
|
IntKey,
|
|
Varchar,
|
|
DateTime
|
|
};
|
|
|
|
/**
|
|
* A Token DomainObject
|
|
*
|
|
* @property ValueObjects\Intkey $ownerId The pk of the Owner the token belongs to
|
|
* @property Owner $owner The Owner of the token
|
|
* @property ValueObjects\Varchar $string The token string
|
|
* @property ValueObjects\DateTime $creationTime The script or binary
|
|
*
|
|
* @author norb
|
|
*/
|
|
class Token extends DomainObject
|
|
{
|
|
use \VeruA\Log;
|
|
|
|
public function __construct($data=null)
|
|
{
|
|
parent::__construct($data);
|
|
|
|
if ($data instanceof Key)
|
|
{
|
|
$this->set('idOwner', $data);
|
|
$this->markNew();
|
|
$this->createToken();
|
|
}
|
|
|
|
$this->log()->debug("Values after instantiation", $this->data);
|
|
}
|
|
|
|
protected function fields(array ...$superFields): array
|
|
{
|
|
return parent::fields([
|
|
'id' => IntKey::class,
|
|
'idOwner' => IntKey::class,
|
|
'owner' => Owner::class,
|
|
'string' => Varchar::class,
|
|
'creationTime' => DateTime::class,
|
|
], ...$superFields);
|
|
}
|
|
|
|
/**
|
|
* Returns a random generated token consisting of letters and numbers
|
|
*
|
|
* @param int $length The character length of the token
|
|
* @return string The token
|
|
*/
|
|
public function createToken(int $length = 64): string
|
|
{
|
|
$this->string = bin2hex(random_bytes($length/2));
|
|
$this->creationTime = new \DateTimeImmutable();
|
|
return $this->string;
|
|
}
|
|
|
|
/**
|
|
* Checks if the token is still valid
|
|
*
|
|
* @return true if creationTime not older than 14 days
|
|
*/
|
|
public function good(): bool
|
|
{
|
|
return ($this->creationTime->value()->diff(new \DateTimeImmutable())->days < 14);
|
|
}
|
|
|
|
}
|
|
|
|
/* jEdit buffer local properties {{{
|
|
* :folding=explicit:collapseFolds=1:
|
|
}}}*/
|