A small Moodle local plugin that provides a Moodle-side redirect endpoint to start the OAuth2/OIDC login flow while preserving sesskey and wantsurl parameters.
Many administrators want Moodle's login to automatically redirect to an external OAuth/OIDC provider (Okta, Microsoft, Google). Direct server-level redirects to /auth/oauth2/login.php?id=... often fail because require_sesskey() expects a session token or the sesskey GET parameter. This plugin provides a Moodle-executed endpoint that can safely build the proper redirect URL including sesskey() and wantsurl.
- Copy the
oauthredirectfolder tomoodle/local/oauthredirect. - Log into Moodle as an admin and visit
Site administration -> Notificationsto install the plugin. - Configure settings at
Site administration -> Plugins -> Local plugins -> OAuth Redirect settings.- Choose the default issuer from the dropdown.
- Toggle
Include sesskeyandInclude wantsurlas desired.
-
To use the plugin as a redirect target in your web server (recommended):
- Point your HTTP server redirect for the login page (
alternateloginurl) to:https://yourmoodle.example.com/local/oauthredirect/redirect.php
- That endpoint will read the admin-configured
issuerid, includesesskey()(if enabled) and optionalwantsurl, then redirect the client to the real OAuth entrypoint/auth/oauth2/login.php.
- Point your HTTP server redirect for the login page (
-
If you want a public redirect (so external clients can hit it without being logged in), edit
redirect.phpand remove therequire_capabilitycall. Be cautious: includingsesskey()for anonymous clients will fail (there won't be a session). Use this endpoint primarily when your server (Nginx/Apache) proxies users to it and sessions exist. -
For testing: you can call
?issuerid=<id>&sesskey=<value>&wantsurl=<path>to override behavior (for diagnostics only).
This plugin includes PHPUnit tests in tests/. Run Moodle PHPUnit according to Moodle dev documentation. The test asserts URL construction behaviors and sesskey inclusion.
sesskey()is a CSRF protection token. Do not removerequire_sesskey()in core or bypass it in production.- Keep one administrator login method available to avoid lockout.
- If you make the redirect endpoint public, understand that sesskey-based flows require a valid session; for true SSO-only workflows consider using the redirect in combination with a server redirect to a Moodle-generated URL for each user/session.
GPL v3 (same as Moodle).