Skip to content

Refactor OAuth2Controller to actions. - #5605

Open
EreMaijala wants to merge 7 commits into
vufind-org:devfrom
EreMaijala:dev-refactor-oauth2controller
Open

Refactor OAuth2Controller to actions.#5605
EreMaijala wants to merge 7 commits into
vufind-org:devfrom
EreMaijala:dev-refactor-oauth2controller

Conversation

@EreMaijala

Copy link
Copy Markdown
Contributor

Moves a significant part of the logic to a new OAuth2ServerService.

*
* @return ResponseInterface
*/
public function action(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to move more of the logic in this method to OAuth2ServerService, but it's not easy because it's a multi-step operation with different outcomes depending e.g. on the user login status.

@demiankatz demiankatz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I ran tests on this locally and they all passed, but I've only just started looking at the code. See below for one small thing I noticed while waiting to get set up for the Summit. I'll provide a more thorough follow-up after the Summit is over -- just submitting this item now so it doesn't get lost/forgotten.

Comment thread module/VuFind/config/module.config.php Outdated
Moves a significant part of the logic to a new OAuth2ServerService.
@EreMaijala
EreMaijala force-pushed the dev-refactor-oauth2controller branch from f19d0f9 to d650826 Compare September 9, 2026 10:58
@demiankatz demiankatz added this to the 12.0 milestone Sep 9, 2026
@demiankatz demiankatz added the architecture pull requests that involve significant refactoring / architectural changes label Sep 9, 2026
@EreMaijala

EreMaijala commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Note: the functionality in the actions and OAuth2ServerService is almost completely covered by OAuth2Test Mink tests.

@demiankatz demiankatz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, @EreMaijala. All tests are passing for me, but see below for some minor questions, suggestions and observations.

Comment on lines -131 to -138
if ($request->getMethod() == 'OPTIONS') {
// Disable session writes
$this->disableSessionWrites();
$response = $this->getResponse();
$response->setStatusCode(204);
$this->addCorsHeaders($response);
return $response;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's not immediately obvious to me where this logic ended up in the new version. Is this important, or am I just overlooking something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oops! Thanks for catching that. I completely forgot to bring it over. Now done. In future we may want to consider handling OPTIONS requests in middleware, but that's a future thing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An OPTIONS request also added to the Mink test.

protected function handleOAuth2Exception(
ResponseInterface $response,
string $function,
\Exception $e

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

convertOAuthServerExceptionToResponse assumes that $e will have a generateHttpResponse method, which I assume is not part of the base exception class. Do we need a more specific type here, or some kind of type checking later?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The exception handling methods were kind of confusing already in the controller. I renamed the methods and fixed the parameter typing.

Comment thread module/VuFind/src/VuFind/Action/OAuth2/AuthorizeAction.php Outdated
$response,
$this->oauth2Service->getUserInfo($request)
);
return $responseHelper->addCorsHeaders($response);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As far as I can tell, the original version did not add CORS headers. Was that an oversight that is corrected here, or am I just missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I believe it was an oversight. It'd only affect browser-based authorization (public client), so probably that's why nobody has noticed.


$responseHelper = $this->getHelper(ResponseHelper::class);
$response = $responseHelper->getJsonResponse($response, $this->oauth2Service->getJwks());
return $responseHelper->addCorsHeaders($response);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another where the original code does not seem to include CORS. Probably not a problem, just an observation.

Comment thread module/VuFind/src/VuFind/ActionHelper/ResponseHelper.php Outdated

$responseHelper = $this->getHelper(ResponseHelper::class);
$response = $responseHelper->getJsonResponse($response, $this->oauth2Service->getWellKnownConfiguration());
return $responseHelper->addCorsHeaders($response);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

More added CORS.

{
$baseUrl = rtrim($this->baseUrl, '/');
$configuration = [
'issuer' => 'https://' . $_SERVER['HTTP_HOST'], // Same as OpenIDConnectServer\IdTokenResponse

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there a way to do this without using the $_SERVER superglobal? (I realize that the existing code already did this, but it seems like using an abstraction of the server variables would be more elegant).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I changed it now. I believe the original one was done using $_SERVER because that's what OpenIDConnectServer\IdTokenResponse uses internally as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

architecture pull requests that involve significant refactoring / architectural changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants