Skip to content

Commit

Permalink
Fixes from PR tests
Browse files Browse the repository at this point in the history
- Dynamically decide which parameters must be passed on, as OAuth requires completely different authentication parameters compared to jwt
- Checking if the required parameters are set is already done by the authentication service depending on the flavor of authentication that is chosen.
  • Loading branch information
rjzondervan committed Nov 20, 2024
1 parent 7b7f4c4 commit 0aea3ff
Showing 1 changed file with 7 additions and 48 deletions.
55 changes: 7 additions & 48 deletions lib/Twig/AuthenticationRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\OpenConnector\Twig;

use Adbar\Dot;
use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenConnector\Db\Source;
use OCA\OpenConnector\Service\AuthenticationService;
Expand All @@ -17,36 +18,6 @@ public function __construct(

}


/**
* Checks if the given array has at least the required keys to continue checkin oauth or a jwt token.
*
* The Required keys:
* 'authentication.algorithm',
* 'authentication.secret',
* 'authentication.payload'
*
* @param array $arrayCheck The PHP array to check if all required keys are present.
* @return bool True if all required keys are present, false if not.
*/
private function checkRequiredKeys(array $arrayCheck): bool
{
$requiredKeys = [
'authentication.algorithm',
'authentication.secret',
'authentication.payload'
];

foreach ($requiredKeys as $key) {
if (array_key_exists($key, $arrayCheck) === false) {
echo "Key '$key' is missing.\n";
return false; // Stop further execution if any key is missing
}
}

return true;
}

/**
* Add an oauth token to the configuration.
*
Expand All @@ -57,18 +28,12 @@ private function checkRequiredKeys(array $arrayCheck): bool
*/
public function oauthToken(Source $source): string
{
$configuration = $source->getConfiguration();

if ($this->checkRequiredKeys(arrayCheck: $configuration) === false) {
throw new ServiceUnavailableHttpException(message: 'The Source configuration is missing one or more required keys');
}
$configuration = new Dot($source->getConfiguration(), true);

$configuration['algorithm'] = $configuration['authentication.algorithm'];
$configuration['secret'] = $configuration['authentication.secret'];
$configuration['payload'] = json_decode($configuration['authentication.payload'], true);
$authConfig = $configuration->get('authentication');

return $this->authService->fetchOAuthTokens(
configuration: $configuration
configuration: $authConfig
);
}

Expand All @@ -81,18 +46,12 @@ public function oauthToken(Source $source): string
*/
public function jwtToken(Source $source): string
{
$configuration = $source->getConfiguration();

if ($this->checkRequiredKeys(arrayCheck: $configuration) === false) {
throw new ServiceUnavailableHttpException(message: 'The Source configuration is missing one or more required keys');
}
$configuration = new Dot($source->getConfiguration(), true);

$configuration['algorithm'] = $configuration['authentication.algorithm'];
$configuration['secret'] = $configuration['authentication.secret'];
$configuration['payload'] = json_decode($configuration['authentication.payload'], true);
$authConfig = $configuration->get('authentication');

return $this->authService->fetchJWTToken(
configuration: $configuration
configuration: $authConfig
);
}
}

0 comments on commit 0aea3ff

Please sign in to comment.