Skip to content

Adds a before_login_completed hook to auth_oidc - #3321

Open
weilai-irl wants to merge 2 commits into
MOODLE_502_STABLEfrom
wip-135097-m502
Open

Adds a before_login_completed hook to auth_oidc#3321
weilai-irl wants to merge 2 commits into
MOODLE_502_STABLEfrom
wip-135097-m502

Conversation

@weilai-irl

Copy link
Copy Markdown
Collaborator

Adds a before_login_completed hook to auth_oidc so plugins can reject a login before it completes.

Copilot AI review requested due to automatic review settings July 31, 2026 15:20
@weilai-irl weilai-irl self-assigned this Jul 31, 2026
@weilai-irl weilai-irl added this to the 2026-03 milestone Jul 31, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new Moodle hook (auth_oidc\hook\before_login_completed) and dispatches it from the OIDC auth code login flow so other plugins can perform additional validation immediately before a login is finalized.

Changes:

  • Introduces a new before_login_completed hook class carrying the idtoken.
  • Dispatches the hook at three points in the OIDC auth-code login flow, before calling complete_user_login().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
auth/oidc/classes/loginflow/authcode.php Dispatches the new hook during login handling before finalizing the Moodle session.
auth/oidc/classes/hook/before_login_completed.php Defines the new hook class and metadata (label/tags) for subscribers.
Suppressed comments (2)

auth/oidc/classes/loginflow/authcode.php:820

  • The hook is dispatched before checking that authenticate_user_login() succeeded. To ensure handlers only run when a login is actually about to be completed, move the hook dispatch inside if (!empty($user)) (right before complete_user_login($user)).
            // Look for plugins that want to add extra checks before user login is completed.
            $hook = new \auth_oidc\hook\before_login_completed($idtoken);
            di::get(\core\hook\manager::class)->dispatch($hook);

            if (!empty($user)) {

auth/oidc/classes/loginflow/authcode.php:902

  • The hook dispatch currently occurs even when authenticate_user_login() returns empty. This can cause hook handlers to run during failed logins. Wrap the dispatch in the existing if (!empty($user)) block so it only runs when a user is about to be logged in.
            // Look for plugins that want to add extra checks before user login is completed.
            $hook = new \auth_oidc\hook\before_login_completed($idtoken);
            di::get(\core\hook\manager::class)->dispatch($hook);

            if (!empty($user)) {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread auth/oidc/classes/loginflow/authcode.php
Comment thread auth/oidc/classes/hook/before_login_completed.php
- Dispatch the hook in all three handlelogin() branches (it previously
  only fired for already-connected users, missing the username-changed
  and new/unconnected-user paths - the latter being the main case for
  gating first-time logins).
- Dispatch only after authenticate_user_login() succeeds, immediately
  before complete_user_login(), instead of unconditionally.
- Fix the hook's PHPDoc/attribute text and $idtoken property doc, which
  were copy-pasted from unrelated core hooks.
- Add the missing @param tag for the constructor.
- Document the login-rejection contract: callbacks reject a login by
  throwing an exception (e.g. \moodle_exception), since the hook manager
  does not catch callback exceptions.
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.

3 participants