Okay, this is a common issue when integrating Moodle with Keycloak via OAuth2, especially when a proxy is involved. Here's a breakdown of debugging steps, covering potential causes and how to investigate them, categorized for clarity. I'll also include things to check related to the proxy.
1. Moodle Configuration Verification (Double-Check)
- OAuth2 Settings: Go to Administration > Plugins > Web services > OAuth2 in Moodle. Carefully review these settings:
- Authorization Endpoint: Correctly points to your Keycloak authorization endpoint.
- Token Endpoint: Correctly points to your Keycloak token endpoint.
- User Info Endpoint: This is critical. It must point to the Keycloak User Info endpoint (usually something like
https://your.keycloak.domain/auth/realms/{your_realm}/protocol/openid-connect/userinfo). Ensure it's accurate.
- Client ID & Client Secret: Verify these match exactly what's configured in your Keycloak client. Case sensitivity matters.
- Scope: The scope must include
openid, profile, and email. (e.g., openid profile email). Without email, Moodle won't get the email address. Without profile, it might not get the username.
- Mapping: This is where you tell Moodle which Keycloak claims map to Moodle's username and email fields. This is the most likely culprit.
- Username attribute: Should be set to the Keycloak claim that contains the username (often
preferred_username or sub).
- Email attribute: Should be set to the Keycloak claim that contains the email address (usually
email).
- Moodle Logs: Enable debugging for OAuth2 in Moodle. Go to Administration > Site Administration > Development > Debugging. Set "Debugging mode" to "Developer" and check "Display debugging messages". Then, try the login again. The debug output will show the raw data Moodle receives from Keycloak. This is invaluable. Look for the JSON response from the User Info endpoint. Does it contain the
preferred_username and email claims? If not, the problem is not in Moodle's mapping, but in Keycloak or the proxy.
2. Keycloak Client Configuration Verification
- Client Settings: In the Keycloak admin console, go to your client configuration.
- Access Type: Should be set to
confidential if you're using a client secret. public is for JavaScript-based clients.
- Standard Flow Enabled: Make sure this is enabled.
- Direct Access Grants Enabled: Generally, you don't need this for a standard web application integration.
- Valid Redirect URIs: This must include the Moodle OAuth2 callback URL (usually something like
https://your.moodle.domain/auth/oauth2/callback). Wildcards can be used, but be careful.
- Web Origins: If you're using Keycloak 22+, you'll need to configure Web Origins as well. Add your Moodle domain.
- Client Scopes: Ensure the client has the
openid, profile, and email scopes enabled. You might need to explicitly map these scopes to the client.
- User Attributes: Keycloak allows you to map user attributes to claims. Verify that the
email and preferred_username (or whatever you're using for the username) attributes are being mapped to claims in the ID token and User Info endpoint response. (Go to Realm Settings > Identity Providers > OpenID Connect v1.0 and check the "Attributes" section).
- Token Inspection: Use a tool like https://jwt.io/ to decode the ID token and Access token that Keycloak is issuing. Paste the tokens (you can get them from the Moodle debug output or from your browser's developer tools during the authentication flow) into the site. Verify that the
email and preferred_username claims are present in the decoded tokens. This confirms Keycloak is actually including the data.
3. Proxy Server Investigation (Crucial)
This is where things get tricky. The proxy could be modifying the headers or body of the requests/responses, causing the issue.
- Proxy Logs: Examine the proxy server's logs. Look for:
- Request to Keycloak: Verify the request from the proxy to Keycloak is being sent correctly, with all necessary headers (especially
Authorization).
- Response from Keycloak: This is the most important. The proxy logs should show the exact response it received from Keycloak. Compare this to the response you see in Moodle's debug output (if you can see it). If the response from the proxy is missing the
email or preferred_username claims, the proxy is the problem.
- Header Manipulation: Check if the proxy is stripping or modifying any headers. Specifically, look for headers related to authorization, content type, and any custom headers you might be using.
- Body Modification: The proxy might be modifying the body of the response from Keycloak. This is less common, but possible. Look for any rules that might be altering the JSON payload.
- Caching: Is the proxy caching the Keycloak responses? If so, it might be serving an old, incorrect response. Disable caching for the Keycloak endpoints.
- SSL/TLS Inspection: If the proxy is performing SSL/TLS inspection (decrypting and re-encrypting the traffic), it could be interfering with the data. Try temporarily disabling SSL/TLS inspection for the Keycloak endpoints to see if that resolves the issue. (Be aware of the security implications of disabling SSL/TLS inspection.)
- Proxy Configuration: Review the proxy's configuration files for any rules that might be affecting the traffic to Keycloak.
4. Network Tracing (Advanced)
- tcpdump/Wireshark: Use a network packet analyzer like
tcpdump or Wireshark to capture the traffic between Moodle, the proxy, and Keycloak. This will give you the most detailed view of what's happening, but it requires some expertise to interpret the results. Look for missing headers, modified data, or any other anomalies.
5. Moodle Version and Plugins
- Moodle Version: What version of Moodle are you using? Older versions might have compatibility issues with Keycloak.
- OAuth2 Plugin Version: Make sure you're using the latest version of the OAuth2 plugin.
- Other Plugins: Are there any other plugins that might be interfering with the OAuth2 authentication process? Try temporarily disabling other plugins to see if that resolves the issue.
Troubleshooting Steps - In Order
- Moodle Debugging: Enable Moodle debugging and examine the raw response from the User Info endpoint. This is your first step.
- Keycloak Token Inspection: Decode the ID and Access tokens using https://jwt.io/ to confirm Keycloak is including the necessary claims.
- Proxy Logs: Examine the proxy logs to see the exact response from Keycloak. This is the most likely source of the problem.
- Proxy Configuration: Review the proxy's configuration for any rules that might be affecting the traffic.
- Network Tracing: If you're still stuck, use a network packet analyzer to capture the traffic.
To help me narrow down the problem, please provide:
- Moodle Version:
- OAuth2 Plugin Version:
- Keycloak Version:
- Proxy Server Type: (e.g., Apache, Nginx, HAProxy, Squid)
- Relevant snippets from your Moodle OAuth2 configuration. (Redact any sensitive information like client secrets.)
- The raw JSON response from the Keycloak User Info endpoint (from Moodle debugging). (Redact any sensitive information.)
- Relevant snippets from the proxy logs. (Redact any sensitive information.)
Good luck! This can be a frustrating process, but with careful investigation, you should be able to identify the root cause and resolve the issue.