Skip to content

feat: expose keycloak global_id on the users/me serializer - #3666

Open
ChristopherChudzicki wants to merge 3 commits into
mainfrom
cc/users-me-global-id
Open

feat: expose keycloak global_id on the users/me serializer#3666
ChristopherChudzicki wants to merge 3 commits into
mainfrom
cc/users-me-global-id

Conversation

@ChristopherChudzicki

@ChristopherChudzicki ChristopherChudzicki commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

What are the relevant tickets?

Toward https://github.com/mitodl/hq/issues/11158 (frontend needs a stable Keycloak-supplied user id; this exposes it via the API).

Description (What does it do?)

Adds the Keycloak-supplied user UUID to the UserSerializer response used by GET /api/v0/users/me/, so the frontend can identify the authenticated user by their SSO id.

  • Uses the field name global_id to match how mitxonline already exposes the same value (users.User.global_id, documented there as "the SSO ID for the user, usually the Keycloak ID"). Same field already exists on mit-learn's User model.
  • Read-only and nullable: authenticated users get their global_id; anonymous users get null (a stable key rather than an omitted one).
  • Regenerated the OpenAPI spec (v0.yaml) and the TypeScript client — User now has global_id: string | null.
  • Updated the user test factory for the new required field.
  • Bumped two line numbers in drf_lint_baseline.json (pre-existing ORM violations shifted down by the added serializer line — not new findings).

How can this be tested?

  1. While authenticated, GET /api/v0/users/me/ should return a global_id matching the user's Keycloak id. Make the same request while logged out and confirm the response contains "global_id": null.
  2. With MITxOnline and MIT Learn connected to same keycloak, MITxOnline's users/me api should return the same global_id.

Additional Context

The global_id key is present as null for anonymous users, whereas the existing authed-only fields (first_name, profile, …) are omitted entirely. This falls out of DRF's handling of a nullable read-only field, and an always-present key is arguably friendlier for the frontend — flagging in case a reviewer prefers it omitted for anonymous.

Copilot AI review requested due to automatic review settings July 23, 2026 17:02
@github-actions

github-actions Bot commented Jul 23, 2026

Copy link
Copy Markdown

OpenAPI Changes

6 changes: 0 error, 0 warning, 6 info

View full changelog

Unexpected changes? Ensure your branch is up-to-date with main (consider rebasing).

Comment thread profiles/serializers.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Exposes the Keycloak-supplied stable user identifier (global_id) on the UserSerializer used by GET /api/v0/users/me/, and propagates the change through the OpenAPI spec and generated TypeScript client.

Changes:

  • Added global_id to profiles.serializers.UserSerializer (read-only) and updated profiles/views_test.py expectations.
  • Regenerated openapi/specs/v0.yaml and the generated TS client so User.global_id is available as string | null.
  • Updated the frontend User test factory and adjusted drf_lint_baseline.json line references.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
profiles/views_test.py Updates API response assertions to include global_id for staff detail and /users/me (authed + anonymous).
profiles/serializers.py Adds global_id to UserSerializer fields and marks it read-only.
openapi/specs/v0.yaml Updates OpenAPI schema for User to include nullable, read-only global_id and regenerates required list.
frontends/api/src/test-utils/factories/user.ts Updates TS test factory to populate global_id for authed users and include null for anonymous user objects.
frontends/api/src/generated/v0/api.ts Regenerates TS types so User includes `global_id: string
drf_lint_baseline.json Shifts baseline line numbers for pre-existing findings due to serializer changes.

Comment thread profiles/serializers.py
@ChristopherChudzicki ChristopherChudzicki added the Needs Review An open Pull Request that is ready for review label Jul 23, 2026
Comment thread profiles/views_test.py
ChristopherChudzicki and others added 3 commits July 24, 2026 15:36
Add the Keycloak-supplied user UUID to the UserSerializer response so the
frontend can identify the authenticated user by their SSO id. Uses the
`global_id` field name to match how mitxonline already exposes the same
value.

Read-only and nullable: authenticated users get their global_id, anonymous
users get null. Regenerated the OpenAPI spec and TypeScript client, and
updated the user test factory for the new required field.

Toward mitodl/hq#11158

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The exact-dict assertions in profiles/serializers_test.py omitted the new
global_id key and were failing. Caught in PR review.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
test_list_users and test_patch_user also assert the exact serialized user
shape; add the new global_id key. Caught by CI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@zamanafzal zamanafzal self-assigned this Jul 27, 2026

@zamanafzal zamanafzal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Image

@zamanafzal zamanafzal added Waiting on author and removed Needs Review An open Pull Request that is ready for review labels Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants