Adds a before_login_completed hook to auth_oidc - #3321
Open
weilai-irl wants to merge 2 commits into
Open
Conversation
… user login is completed
There was a problem hiding this comment.
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_completedhook class carrying theidtoken. - 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 insideif (!empty($user))(right beforecomplete_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 existingif (!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.
- 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.
weilai-irl
force-pushed
the
wip-135097-m502
branch
from
July 31, 2026 15:36
4429b2c to
5c4319d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a before_login_completed hook to auth_oidc so plugins can reject a login before it completes.