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

Some small changes in the Source Auth configuration #71

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
170 changes: 94 additions & 76 deletions lib/Controller/MappingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use OCP\IAppConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class MappingsController extends Controller
{
Expand Down Expand Up @@ -126,17 +128,17 @@ public function create(): JSONResponse
return new JSONResponse($this->mappingMapper->createFromArray(object: $data));
}

/**
* Updates an existing mapping
*
* This method updates an existing mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the mapping to update
* @return JSONResponse A JSON response containing the updated mapping details
*/
/**
* Updates an existing mapping
*
* This method updates an existing mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the mapping to update
* @return JSONResponse A JSON response containing the updated mapping details
*/
public function update(int $id): JSONResponse
{
$data = $this->request->getParams();
Expand All @@ -152,60 +154,66 @@ public function update(int $id): JSONResponse
return new JSONResponse($this->mappingMapper->updateFromArray(id: (int) $id, object: $data));
}

/**
* Deletes a mapping
*
* This method deletes a mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id The ID of the mapping to delete
* @return JSONResponse An empty JSON response
*/
/**
* Deletes a mapping
*
* This method deletes a mapping based on its ID.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the mapping to delete
* @return JSONResponse An empty JSON response
* @throws \OCP\DB\Exception
*/
public function destroy(int $id): JSONResponse
{
$this->mappingMapper->delete($this->mappingMapper->find((int) $id));

return new JSONResponse([]);
}

/**
* Tests a mapping
*
* This method tests a mapping with provided input data and optional schema validation.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse A JSON response containing the test results
*
* @example
* Request:
* {
* "inputObject": "{\"name\":\"John Doe\",\"age\":30,\"email\":\"[email protected]\"}",
* "mapping": {
* "mapping": {
* "fullName":"{{name}}",
* "userAge":"{{age}}",
* "contactEmail":"{{email}}"
* }
* },
* "schema": "user_schema_id",
* "validation": true
* }
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
/**
* Tests a mapping
*
* This method tests a mapping with provided input data and optional schema validation.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param ObjectService $objectService
* @param IURLGenerator $urlGenerator
*
* @return JSONResponse A JSON response containing the test results
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*
* @example
* Request:
* {
* "inputObject": "{\"name\":\"John Doe\",\"age\":30,\"email\":\"[email protected]\"}",
* "mapping": {
* "mapping": {
* "fullName":"{{name}}",
* "userAge":"{{age}}",
* "contactEmail":"{{email}}"
* }
* },
* "schema": "user_schema_id",
* "validation": true
* }
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
public function test(ObjectService $objectService, IURLGenerator $urlGenerator): JSONResponse
{
$openRegisters = $objectService->getOpenRegisters();
Expand Down Expand Up @@ -293,32 +301,42 @@ public function test(ObjectService $objectService, IURLGenerator $urlGenerator):
]);
}

/**
* Saves a mapping object
*
* This method saves a mapping object based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function saveObject(): JSONResponse
/**
* Saves a mapping object
*
* This method saves a mapping object based on POST data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse|null
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function saveObject(): ?JSONResponse
{
// Check if the OpenRegister service is available
$openRegisters = $this->objectService->getOpenRegisters();
if ($openRegisters !== null) {
$data = $this->request->getParams();
return new JSONResponse($openRegisters->saveObject($data['register'], $data['schema'], $data['object']));
}

return null;
}

/**
* Retrieves a list of objects to map to
*
* This method retrieves a list of objects to map to based on GET data.
*
* @NoAdminRequired
* @NoCSRFRequired
*/
/**
* Retrieves a list of objects to map to
*
* This method retrieves a list of objects to map to based on GET data.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @return JSONResponse
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getObjects(): JSONResponse
{
// Check if the OpenRegister service is available
Expand All @@ -333,6 +351,6 @@ public function getObjects(): JSONResponse
}

return new JSONResponse($data);

}
}
1 change: 1 addition & 0 deletions lib/Service/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public function fetchJWTToken (array $configuration): string
}

$payload = $this->getJWTPayload($configuration);

$jwk = $this->getJWK($configuration);

if ($jwk === null) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/CallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ public function call(
// Set authentication if needed. @todo: create the authentication service
//$createCertificates && $this->getCertificate($config);

// Make sure to filter out all the authentication variables / secrets.
$config = array_filter($config, function ($key) {
return str_contains(strtolower($key), 'authentication') === false;
}, ARRAY_FILTER_USE_KEY);

// Let's log the call.
$this->source->setLastCall(new \DateTime());
// @todo: save the source
Expand Down
13 changes: 7 additions & 6 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException;
use OCP\App\IAppManager;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function getClient(array $config): Client
* @param array $config The configuration that should be used by the call.
*
* @return array The resulting object.
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function saveObject(array $data, array $config): array
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public function saveObject(array $data, array $config): array
*
* @return array The objects found for given filters.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function findObjects(array $filters, array $config): array
{
Expand Down Expand Up @@ -115,7 +116,7 @@ public function findObjects(array $filters, array $config): array
*
* @return array The resulting object.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function findObject(array $filters, array $config): array
{
Expand Down Expand Up @@ -149,7 +150,7 @@ public function findObject(array $filters, array $config): array
*
* @return array The updated object.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function updateObject(array $filters, array $update, array $config): array
{
Expand Down Expand Up @@ -181,7 +182,7 @@ public function updateObject(array $filters, array $update, array $config): arra
*
* @return array An empty array.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function deleteObject(array $filters, array $config): array
{
Expand All @@ -206,7 +207,7 @@ public function deleteObject(array $filters, array $config): array
* @param array $pipeline The pipeline to use.
* @param array $config The configuration to use in the call.
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws GuzzleException
*/
public function aggregateObjects(array $filters, array $pipeline, array $config):array
{
Expand Down
56 changes: 54 additions & 2 deletions lib/Twig/AuthenticationRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GuzzleHttp\Exception\GuzzleException;
use OCA\OpenConnector\Db\Source;
use OCA\OpenConnector\Service\AuthenticationService;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Twig\Extension\RuntimeExtensionInterface;

class AuthenticationRuntime implements RuntimeExtensionInterface
Expand All @@ -15,6 +17,36 @@ 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 @@ -25,8 +57,18 @@ public function __construct(
*/
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['algorithm'] = $configuration['authentication.algorithm'];
$configuration['secret'] = $configuration['authentication.secret'];
$configuration['payload'] = json_decode($configuration['authentication.payload'], true);

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

Expand All @@ -39,8 +81,18 @@ 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['algorithm'] = $configuration['authentication.algorithm'];
$configuration['secret'] = $configuration['authentication.secret'];
$configuration['payload'] = json_decode($configuration['authentication.payload'], true);

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