Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token require fix when requireToken is set to false #7

Merged
merged 4 commits into from
Feb 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 36 additions & 30 deletions src/TokenAuthorizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,14 @@ public function setStrategy(VerificationStrategy $strategy): void
*/
public function beforeProcess(Endpoint $endpoint, array $params, string $action, string $method): ?Response
{
if ($this->strategy->isActive() === false) {
return null;
}
$token = $params['token'] ?? null;
if ($token === null) {
throw new \InvalidArgumentException('Parameter "token" is required.');
}
if (is_string($token) === false) {
throw new \InvalidArgumentException(sprintf('Parameter "token" must be string, but type "%s" given.', get_debug_type($token)));
}
$requireToken = false;
try {
$ref = new \ReflectionClass($endpoint);
$docComment = trim((string) $ref->getDocComment());
if (preg_match('/@public(?:$|\s|\n)/', $docComment) === 1) {
return null;
}
foreach ($ref->getAttributes(PublicEndpoint::class) as $publicEndpointAttribute) {
if (($publicEndpointAttribute->getArguments()['requireToken'] ?? false) === true) {
$requireToken = true;
}
}
} catch (\ReflectionException $e) {
throw new \InvalidArgumentException(
sprintf('Endpoint "%s" can not be reflected: %s', $endpoint::class, $e->getMessage()),
500,
$e,
);
}
if ($requireToken === false || $this->strategy->verify($token)) {
if (
$this->strategy->isActive() === false
|| $this->isPublicAccess($endpoint)
|| $this->isTokenOk($params['token'] ?? null)
) {
return null;
}

throw new \InvalidArgumentException('Token is invalid or expired, please contact your administrator.');
}

Expand All @@ -78,4 +54,34 @@ public function afterProcess(Endpoint $endpoint, array $params, ?Response $respo
{
return null;
}


private function isPublicAccess(Endpoint $endpoint): bool
{
$requireToken = false;
$ref = new \ReflectionClass($endpoint);
if (str_contains((string) $ref->getDocComment(), '@public')) {
return true;
}
foreach ($ref->getAttributes(PublicEndpoint::class) as $publicEndpointAttribute) {
if (($publicEndpointAttribute->getArguments()['requireToken'] ?? false) === true) {
$requireToken = true;
}
}

return $requireToken === false;
}


private function isTokenOk(mixed $token): bool
{
if ($token === null) {
throw new \InvalidArgumentException('Parameter "token" is required.');
}
if (is_string($token) === false) {
throw new \InvalidArgumentException(sprintf('Parameter "token" must be string, but type "%s" given.', get_debug_type($token)));
}

return $this->strategy->verify($token);
}
}