2018-02-06 11:09:05 +00:00
|
|
|
<?php
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Entity\Repository;
|
2018-02-06 11:09:05 +00:00
|
|
|
|
2020-02-06 02:35:13 +00:00
|
|
|
use App\Doctrine\Repository;
|
2020-04-17 05:37:52 +00:00
|
|
|
use App\Entity;
|
2020-07-08 07:03:50 +00:00
|
|
|
use InvalidArgumentException;
|
2018-02-06 11:09:05 +00:00
|
|
|
|
2018-11-12 16:59:15 +00:00
|
|
|
class ApiKeyRepository extends Repository
|
2018-02-06 11:09:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Given an API key string in the format `identifier:verifier`, find and authenticate an API key.
|
|
|
|
*
|
2019-01-31 17:54:17 +00:00
|
|
|
* @param string $key_string
|
2019-09-20 16:44:38 +00:00
|
|
|
*
|
2018-02-06 11:09:05 +00:00
|
|
|
* @return Entity\User|null
|
|
|
|
*/
|
2020-04-17 05:37:52 +00:00
|
|
|
public function authenticate(string $key_string): ?Entity\User
|
2018-02-06 11:09:05 +00:00
|
|
|
{
|
2019-09-29 07:50:24 +00:00
|
|
|
[$key_identifier, $key_verifier] = explode(':', $key_string);
|
2018-02-06 11:09:05 +00:00
|
|
|
|
2020-04-17 05:37:52 +00:00
|
|
|
if (empty($key_identifier) || empty($key_verifier)) {
|
2020-07-08 07:03:50 +00:00
|
|
|
throw new InvalidArgumentException('API key is not in a valid format.');
|
2020-04-17 05:37:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 07:50:24 +00:00
|
|
|
$api_key = $this->repository->find($key_identifier);
|
2018-02-06 11:09:05 +00:00
|
|
|
|
|
|
|
if ($api_key instanceof Entity\ApiKey) {
|
|
|
|
return ($api_key->verify($key_verifier))
|
|
|
|
? $api_key->getUser()
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|