Authentication flow¶
Summary¶
login.phpredirects to SUAP’sauthorize_urlwithclient_idandredirect_uripointing toauthenticate.php.The user authenticates in SUAP and is redirected back with a
codeparameter.authenticate.phpexchanges thecodefor anaccess_tokenattoken_url.With the
access_token, the plugin fetches data from four SUAP endpoints and merges the results.create_or_update_user()creates or updates the user in Moodle and synchronizes the profile fields.complete_user_login()authenticates the session and the user is redirected to the original destination (next/wantsurl).
Plugin endpoints¶
Endpoint |
Purpose |
|---|---|
|
Starts the SUAP login ( |
|
OAuth2 callback ( |
|
Full logout confirmation page (SUAP + Moodle). |
|
Diagnostics: shows active settings (requires login). |
|
Redirects to |
Note
Earlier versions of this README documented an additional endpoint, dispatch.php, which
would generate a Moodle webservice token from an Authentication: Token header (for use
by applications). This file no longer exists in the current source code — it was
removed in a previous refactor. This documentation describes only what is implemented
today.
Step 1 — login()¶
auth_plugin_suap::login() (in auth.php):
Resolves the post-login destination (
next): thenextparameter, or$SESSION->wantsurl, or the site root.If the user is already logged in, it simply redirects to
next.Otherwise, it validates that
authorize_urlandclient_idare configured (otherwise it throwsconfigincomplete), storesnextin$SESSION->next_after_nextand redirects to:{authorize_url}?response_type=code&client_id={client_id}&redirect_uri={wwwroot}/auth/suap/authenticate.php
Step 2 — exchanging the code for a token¶
authenticate_token() makes a POST request to token_url with
grant_type=authorization_code, code, redirect_uri, client_id and
client_secret. On error (a response without access_token, a cURL error, or HTTP ≥
400), the exception is caught and the plugin renders the auth_suap/auth_error template
with a button to restart the login, instead of exposing the raw error to the user.
On success, the method returns the headers used in the following calls:
Authorization: Bearer {access_token}
x-api-key: {client_secret}
Accept: application/json
Step 3 — collecting user data¶
authenticate() calls, in sequence, four methods that query the SUAP API and whose results
are combined with array_merge (in this order: rheu, meusdados,
ensinomeusdadosaluno, meusvinculos — later keys overwrite earlier ones):
Method |
Endpoint (configurable) |
Notes |
|---|---|---|
|
|
Called with |
|
|
Removes the |
|
|
Returns |
|
|
Fault-tolerant: any exception (e.g., the user is not a student) is caught and the
method returns an empty object, without interrupting the login. Removes
|
Step 4 — user creation/update¶
See User synchronization for the complete field table. In summary,
create_or_update_user():
Derives
usernamefromidentificacao(ormatriculaas a fallback), lowercased; throwsidentificacao_ausenteif neither is present.If the user does not exist, it creates the account with a random local password (ignored, since authentication is always done via
suap) and, iflocal_suapis installed, appliesdefault_user_preferences.On every authentication (creation or subsequent login), it updates the name, e-mail and dozens of custom profile fields from the SUAP data.
Persists the changes via
update_user_record()(inherited fromauth_oauth2\auth).If a photo URL is available, downloads and applies it via
update_picture().
Step 5 — completing the login¶
complete_user_login($usuario) authenticates the Moodle session. Then,
resolve_next_after_login() reads and clears $SESSION->next_after_next (set in step 1)
to determine the redirect target, falling back to the site root if nothing was saved.
Logout¶
postlogout_hook() intercepts the logout of users with auth == 'suap' and redirects to
/auth/suap/logout.php, which shows a confirmation page: the user chooses between also
ending the SUAP session (logout_url) or staying signed in to Moodle.
\core\session\manager::init_empty_session() is called before showing the page, to
invalidate the local Moodle session right away.