Skip to content

feat: supporting s2s user context#1592

Merged
davidgamez merged 5 commits intomainfrom
feat/server_to_server_user_context
Feb 13, 2026
Merged

feat: supporting s2s user context#1592
davidgamez merged 5 commits intomainfrom
feat/server_to_server_user_context

Conversation

@davidgamez
Copy link
Member

@davidgamez davidgamez commented Feb 4, 2026

Summary:

Closes #1578
Related to MobilityData/mobilitydatabase-web#7
This PR adds support for server-to-server authentication.

Expected behavior:

User context is extracted from the request when the HTTP headers the contains the mdb-user-context JWT signed header.

From our AI friend

This pull request introduces support for a custom user-context JWT for server-to-server authentication, allowing the API to securely identify users based on a shared secret. The changes include decoding and verifying the new JWT, updating request context extraction logic, and ensuring the required secret is available in the deployment environment.

Authentication and user context enhancements:

  • Added a new method decode_user_context_jwt in request_context.py to decode and verify a custom JWT (x-mdb-user-context) using HS256 and a shared secret (S2S_JWT_SECRET). This method safely falls back to existing identity handling if verification fails.
  • Updated _extract_from_headers in request_context.py to extract user information from the custom JWT header, prioritizing verified values and supporting guest users.
  • Introduced the is_guest attribute to the request context to track guest status, and updated related tests to expect this field.

Infrastructure and configuration updates:

  • Modified main.tf to inject the S2S_JWT_SECRET as an environment variable from a secret manager, ensuring secure access to the shared secret for JWT verification.

Dependency management:

  • Added imports for base64, hashlib, hmac, and json in request_context.py to support JWT decoding and verification.

Testing tips:

[internal team]

Please make sure these boxes are checked before submitting your pull request - thanks!

  • Run the unit tests with ./scripts/api-tests.sh to make sure you didn't break anything
  • Add or update any needed documentation to the repo
  • Format the title like "feat: [new feature short description]". Title must follow the Conventional Commit Specification(https://www.conventionalcommits.org/en/v1.0.0/).
  • Linked all relevant issues
  • Include screenshot(s) showing how this pull request works and fixes the issue(s)

@davidgamez davidgamez marked this pull request as ready for review February 9, 2026 17:39

# Optional user-context header set by the web app for server-to-server calls.
# Name is aligned with the frontend's USER_CONTEXT_HEADER.
user_context_header = headers.get("x-mdb-user-context") or headers.get("md-user-context")
Copy link
Contributor

Choose a reason for hiding this comment

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

[question] where is md-user-context used?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not currently in use. It will be used for server-to-server calls that are not from the UI.

Copy link
Contributor

@Alessandro100 Alessandro100 left a comment

Choose a reason for hiding this comment

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

LGTM ✅

value = data.google_project.project.project_id
}
env {
name = "S2S_JWT_SECRET"
Copy link
Contributor

Choose a reason for hiding this comment

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

[question] is the secret manually created?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this secret is set directly in the environment.

Copy link
Contributor

@cka-y cka-y left a comment

Choose a reason for hiding this comment

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

LGTM

@davidgamez davidgamez merged commit 0ac9c92 into main Feb 13, 2026
2 of 5 checks passed
@davidgamez davidgamez deleted the feat/server_to_server_user_context branch February 13, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSR: Update Authentication to Firebase Session Management

3 participants