Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 50 additions & 0 deletions auth/oidc/classes/hook/before_login_completed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace auth_oidc\hook;

use auth_oidc\jwt;

/**
* Allow plugins to perform additional checks before a user login is completed.
*
* This hook is dispatched by auth_oidc after authenticate_user_login() has
* succeeded, but before complete_user_login() is called. The hook manager
* does not catch exceptions raised by callbacks, so a callback can reject
* the login by throwing an exception (e.g. \moodle_exception) - doing so
* will propagate out of the hook dispatch and prevent complete_user_login()
* from being called. There is no other signal (e.g. a flag on this hook) to
* reject the login; a plain return from a callback allows the login to
* proceed.
*
* @package auth_oidc
* @copyright 2026 Ariadne
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
#[\core\attribute\label('Allow plugins to perform additional checks before a user login is completed.')]
#[\core\attribute\tags('user', 'login')]
class before_login_completed {
/**
* Constructor for the hook.
*
* @param jwt $idtoken The id_token of the user attempting to log in.
*/
public function __construct(
/** @var jwt The id_token of the user attempting to log in */
public readonly jwt $idtoken
) {
}
}
14 changes: 14 additions & 0 deletions auth/oidc/classes/loginflow/authcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use core\url;
use pix_icon;
use stdClass;
use core\di;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -739,6 +740,10 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
$user = authenticate_user_login($username, '', true);

if (!empty($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);

complete_user_login($user);
} else {
Comment thread
weilai-irl marked this conversation as resolved.
// There was a problem in authenticate_user_login.
Expand Down Expand Up @@ -809,6 +814,10 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
$user = authenticate_user_login($username, '', true);

if (!empty($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);

complete_user_login($user);
} else {
// There was a problem in authenticate_user_login.
Expand Down Expand Up @@ -895,6 +904,11 @@ protected function handlelogin(string $oidcuniqid, array $authparams, array $tok
$updatedtokenrec->userid = $user->id;
$DB->update_record('auth_oidc_token', $updatedtokenrec);
}

// 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);

complete_user_login($user);
} else {
// There was a problem in authenticate_user_login. Clean up incomplete token record.
Expand Down