From e5a43956b6cd19d00f4ebc142f826586f7034207 Mon Sep 17 00:00:00 2001 From: Patrick Gunsolley Date: Tue, 16 Sep 2025 21:44:26 -0700 Subject: [PATCH] Refactored api auth config handling --- config/authentication_local.example.php | 2 +- .../ApiAuthenticationServiceProvider.php | 10 ++-------- src/Controller/Api/V1/UsersController.php | 6 +++--- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/config/authentication_local.example.php b/config/authentication_local.example.php index 00d11dd..54f4f4d 100644 --- a/config/authentication_local.example.php +++ b/config/authentication_local.example.php @@ -3,7 +3,7 @@ return [ 'Authentication' => [ 'Api' => [ - 'algorithm' => 'ES256', + 'algorithm' => 'RS256', 'expiration' => time() + 60, 'privateKey' => null, 'publicKey' => null, diff --git a/src/Authentication/ApiAuthenticationServiceProvider.php b/src/Authentication/ApiAuthenticationServiceProvider.php index 80f74cf..69ce3b9 100644 --- a/src/Authentication/ApiAuthenticationServiceProvider.php +++ b/src/Authentication/ApiAuthenticationServiceProvider.php @@ -21,18 +21,12 @@ public function getAuthenticationService(ServerRequestInterface $request): Authe AbstractIdentifier::CREDENTIAL_USERNAME => 'email', AbstractIdentifier::CREDENTIAL_PASSWORD => 'password', ]; - $config = Configure::read('Authentication.Api'); - foreach (['publicKey', 'algorithm'] as $key) { - if (!array_key_exists($key, $config)) { - throw new MissingConfigurationException($key); - } - } return new AuthenticationService([ 'authenticators' => [ 'Authentication.Jwt' => [ 'identifier' => 'Authentication.JwtSubject', - 'secretKey' => $config['publicKey'], - 'algorithm' => $config['algorithm'], + 'secretKey' => Configure::readOrFail('Authentication.Api.publicKey'), + 'algorithm' => Configure::readOrFail('Authentication.Api.algorithm'), ], 'Authentication.Form' => [ 'identifier' => [ diff --git a/src/Controller/Api/V1/UsersController.php b/src/Controller/Api/V1/UsersController.php index 92095d0..15e02ab 100644 --- a/src/Controller/Api/V1/UsersController.php +++ b/src/Controller/Api/V1/UsersController.php @@ -25,9 +25,9 @@ public function authenticate() $result = $this->Authentication->getResult(); dd($result); if ($result->isValid()) { - $algorithm = Configure::read(''); - $privateKey = Configure::read(''); - $expiration = Configure::read(''); + $algorithm = Configure::readOrFail('Authentication.Api.algorithm'); + $privateKey = Configure::readOrFail('Authentication.Api.privateKey'); + $expiration = Configure::readOrFail('Authentication.Api.expiration'); $user = $result->getData(); $payload = [ 'iss' => $this->request->domain(),