Skip to content

Commit

Permalink
Identify api-local route as an API request (#2134)
Browse files Browse the repository at this point in the history
Introduces new "keyauth" flag for the "regular" API that uses the key-based auth.

(close #2109)
  • Loading branch information
jimsafley authored Dec 13, 2023
1 parent 4940578 commit 68b637c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions application/config/routes.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
'route' => '/api',
'defaults' => [
'__API__' => true,
'__KEYAUTH__' => true,
'controller' => 'Omeka\Controller\Api',
],
],
Expand All @@ -247,6 +248,7 @@
'options' => [
'route' => '/api-local',
'defaults' => [
'__API__' => true,
'controller' => 'Omeka\Controller\ApiLocal',
],
],
Expand Down
2 changes: 1 addition & 1 deletion application/src/Controller/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function editAction()

if ($keyPersisted) {
$message = new Message(
'API key successfully created.<br><br>Here is your key ID and credential for access to the API. WARNING: "key_credential" will be unretrievable after you navigate away from this page.<br><br>key_identity: <code>%1$s</code><br>key_credential: <code>%2$s</code>', // @translate
'API key successfully created.<br><br>Here is your key ID and credential for access to the API. WARNING: "key_credential" will be unretrievable after you navigate away from this page.<br><br><code>key_identity=%1$s</code><br><code>key_credential=%2$s</code>', // @translate
$keyId, $keyCredential
);
$message->setEscapeHtml(false);
Expand Down
2 changes: 1 addition & 1 deletion application/src/Mvc/MvcListeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function redirectToLogin(MvcEvent $event)
public function authenticateApiKey(MvcEvent $event)
{
$routeMatch = $event->getRouteMatch();
if (!$routeMatch->getParam('__API__')) {
if (!$routeMatch->getParam('__KEYAUTH__')) {
// This is not an API request.
return;
}
Expand Down
19 changes: 19 additions & 0 deletions application/src/Mvc/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class Status
*/
protected $isApiRequest;

/**
* @var bool
*/
protected $isKeyauthRequest;

/**
* @var bool
*/
Expand Down Expand Up @@ -105,6 +110,20 @@ public function isApiRequest()
return $this->isApiRequest;
}

/**
* Check whether the current HTTP request requires key authentication (api).
*
* @return bool
*/
public function isKeyauthRequest()
{
if (isset($this->isKeyauthRequest)) {
return $this->isKeyauthRequest;
}
$this->isKeyauthRequest = (bool) $this->getRouteParam('__KEYAUTH__');
return $this->isKeyauthRequest;
}

/**
* Check whether the current HTTP request is an admin request.
*
Expand Down
4 changes: 2 additions & 2 deletions application/src/Service/AuthenticationServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function __invoke(ContainerInterface $serviceLocator, $requestedName, arr
});
} else {
$userRepository = $entityManager->getRepository('Omeka\Entity\User');
if ($status->isApiRequest()) {
// Authenticate using key for API requests.
if ($status->isKeyauthRequest()) {
// Authenticate using key for requests that require key authentication.
$keyRepository = $entityManager->getRepository('Omeka\Entity\ApiKey');
$storage = new DoctrineWrapper(new NonPersistent, $userRepository);
$adapter = new KeyAdapter($keyRepository, $entityManager);
Expand Down

0 comments on commit 68b637c

Please sign in to comment.