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

DDFBRA-134 - Setup simple_oauth module #1737

Merged
merged 16 commits into from
Nov 22, 2024

Conversation

Dresse
Copy link
Contributor

@Dresse Dresse commented Nov 7, 2024

Link to issue

https://reload.atlassian.net/browse/DDFBRA-134

Description

This PR adds the simple_oauth module, which we want to use for authenticating when requesting the GraphQL endpoints.

This PR also fixes 2 issue we found during testing of the module:

  1. The openapi module was generating security definitions for oauth2 using v3 specification instead of v2. This resulted in errors when trying to generate API SDK files as v2 does not support multiple oauth2 flows. I created an issue on Drupal.org and created a patch to fix this. The patch is added in this PR.
  2. The simple_oauth modules AuthenticationProvider had an issue where in its applies function was using a function (isOauth2Request) for checking wether Requests was OAuth2 requests. The problem however, was that it was just checking to see if a Bearer token was present. This causes problems with the current REST API for fetching opening hours, as those requests would sometime contain the Library token as a Bearer token, and thus the simple_oauth module would try to authenticate the request.
    To solve this issue, this PR extends the simple_oauth modules functionality by decorating the service that provides the logic for determining if a request is a Oauth2 request. The added logic simple check to see if the request path stats with /graphql. If this is not the case, we return FALSE.

The PR also updates the openapi.json file with the new specification.

Lastly this PR makes sure to generate the public and private keys that the simple_oauth modules needs to use when creating tokens. This is done in an update hook, but we also make sure to call the same update hook in the install hook function, as we want to make sure that the keys are generated on both old and new sites. I am unsure if this is the best approach, but we do something similar in dpl_update module, and I did not think it was worth using more time on figuring a better way of doing this. We also update the configuration for where the module should look for the generated keys.

Other comments

We had a talk about where the public and private keys should be generated. We ended up deciding on placing them in folder called simple_oauth_keys placed in the /web/sites/default/files directory.

…t service.

In our decorator, we added functionality that checks wether the request path
starts with '/graphql' or not.

The reason for this is, that right now the isOauth2Request function only checks
if the request contains a bearer token. This is also the case when the requests
come from the opening hours REST API endpoints, as it submits a library token.

By checking for the /graphql in the path, we make sure to only apply the oauth2
validation on the graphql requests.
@Dresse Dresse changed the title DDFBRA-138 - Setup simple_oauth module DDFBRA-134 - Setup simple_oauth module Nov 11, 2024
Copy link
Collaborator

@spaceo spaceo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!
I have a couple of answers I would like to have clarified

config/sync/simple_oauth.settings.yml Outdated Show resolved Hide resolved
openapi.json Show resolved Hide resolved
…in the

simple_oauth module. The update hook is also called from the install hook as
we want to generate the keys on both new and old sites.

Updated the simple_oauth config to look for the keys in the correct directory.
Comment on lines 36 to 37
$path = '../web/sites/default/files/simple_oauth_keys';
$file_system->prepareDirectory($path, FileSystemInterface::CREATE_DIRECTORY);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files need to be written to a private directory so the are not accessible publicly.
The best think would be that we could write in a mount point that is not accesible at all by the web.
We could look into that later.
But for now we need to make sure that the directory that we create in sites/default/files is protected.
You need to investigate if the path to the private dir is set in settings, otherwise you shoul be able to set it via:

$settings['file_private_path'] = SOME_ABSOLUTE_PATH_TO_THE_DIRECTORY

When you need the path to the dir you should be able to do so by calling:

$private_files_path = \Drupal::service('file_system')->realpath('private://');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now added the $settings['file_private_path'] in our settings.php.
This points to sites/default/files/private.

When generating the keys they are now being placed within that folder.

Is it enough to do this for now?

@spaceo spaceo self-requested a review November 20, 2024 11:25
Copy link
Collaborator

@spaceo spaceo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@spaceo spaceo self-requested a review November 20, 2024 11:32
Copy link
Collaborator

@spaceo spaceo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vi need to have the keys properly secured in a private files dir

The private folder is currently placed within /sites/default/files folder.
This is not idea, as the private files folder should be placed outside of
the web root folder. We will look into changing this in the future.

Changed the install hook to generate the keys in the private
directory.

Also changed the simple_oauth configuration to look in the new directory
for the private and public keys.
@Dresse Dresse force-pushed the DDFBRA-138-setup-simple-oauth-module branch from 15ade81 to de7b67f Compare November 21, 2024 12:23
@Dresse Dresse requested a review from spaceo November 21, 2024 12:37
Copy link
Collaborator

@spaceo spaceo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@spaceo spaceo removed their assignment Nov 22, 2024
@Dresse Dresse force-pushed the DDFBRA-138-setup-simple-oauth-module branch from c65ded9 to 2ef6894 Compare November 22, 2024 13:42
@Dresse Dresse force-pushed the DDFBRA-138-setup-simple-oauth-module branch from 2ef6894 to 57154ac Compare November 22, 2024 13:50
@Dresse Dresse merged commit 477ff61 into develop Nov 22, 2024
19 checks passed
@Dresse Dresse deleted the DDFBRA-138-setup-simple-oauth-module branch November 22, 2024 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants