Skip to main content

grad_pylib

Graduate College Python common library for web application APIs and other related projects.

Additional docs in the repository root:

Authentication

Applications authenticate with Azure AD through grad_pylib.core.auth. A few things are worth knowing before deploying a service that uses it.

Set ENVIRONMENT

ENVIRONMENT must be set explicitly for every deployment (production for production). Only development, local and test are treated as development environments. Outside those, the .env file is not loaded at all, so a stray dotenv file in the working directory cannot override production configuration.

The development API key is a full bypass

When ENABLE_DEV_API_KEY is turned on, a request carrying the Api-Key header authenticates as any role it asks for through the Api-Role header, with no token involved. This is intentional: arbitrary impersonation is needed for local development and automated end-to-end tests. It is guarded as follows:

  • ENABLE_DEV_API_KEY must be explicitly enabled, and settings validation refuses it outside a development environment.
  • The request must arrive from a loopback address. Forwarding headers such as X-Forwarded-For are ignored, so a leaked key is not remotely exploitable.
  • A request that presents Api-Key when the bypass is unavailable, or presents the wrong key, is rejected with a 401 and audited. It never falls through to Azure AD authentication.

Validating the requested role against the application's roles is the consuming project's responsibility, inside its api_key_user_builder.

Audit logging

Every authentication decision is emitted to the grad_pylib.audit.auth logger as a structured event: auth.access.granted, auth.access.denied, auth.failed, auth.token.rejected, auth.api_key.bypass and auth.roles.overridden. Records include the subject, policy, mechanism, effective roles, client address and request path. Note that override_loader is a privilege-granting hook, and any override that changes the effective roles is logged.

Identities

Only illinois.edu and uillinois.edu UPNs are accepted; other domains and tokens without a UPN claim are rejected with a 401. Only the application's own user object is stored on request.state.user, so the raw access token is not left where an error handler or APM integration could serialize it.

The library's contract for a user is the AuthUser protocol — a read-only effective_roles sequence. An application can satisfy it with any immutable type it likes; BaseUser is a ready-made implementation, not a required base class. Its fields are stored exactly as declared (tuples, and a read-only attribute mapping), so untrusted values are normalized where they enter the application, with parse_roles() and parse_distinct_strings(). Users are immutable — use with_roles_override() or dataclasses.replace() to derive a modified user.

Authorization configuration

AuthConfiguration is a frozen pydantic model that is validated once, at startup:

  • policy_roles entries must name roles from valid_roles. They are matched case-insensitively and stored in the canonical casing, so a typo or a casing mismatch is a startup error rather than an endpoint that silently never grants access.
  • A policy with no roles is rejected at construction rather than when its dependency is built.
  • The policy mapping and its role sets are deep-frozen, so a caller holding a reference to what it passed in cannot change authorization decisions at runtime.

A policy grants access when the user holds any of its roles.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grad_pylib-5.0.1.tar.gz (142.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

grad_pylib-5.0.1-py3-none-any.whl (97.8 kB view details)

Uploaded Python 3

File details

Details for the file grad_pylib-5.0.1.tar.gz.

File metadata

  • Download URL: grad_pylib-5.0.1.tar.gz
  • Upload date:
  • Size: 142.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for grad_pylib-5.0.1.tar.gz
Algorithm Hash digest
SHA256 f5d98d8d69bd251f0eea62ab1cf8b5de063f6deb3c3de5af46eee8df15a6f558
MD5 0faf2c0004e74f1ad32be4b2ce90ea0f
BLAKE2b-256 cb6b3667e9a2fec82960fba574b3f87b2eafbdb9f5af362d1e6b5e00369fb7ac

See more details on using hashes here.

File details

Details for the file grad_pylib-5.0.1-py3-none-any.whl.

File metadata

  • Download URL: grad_pylib-5.0.1-py3-none-any.whl
  • Upload date:
  • Size: 97.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.13 {"installer":{"name":"uv","version":"0.12.13","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for grad_pylib-5.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f2c98a2f7806a6e703c102c6aa04673f5e3e2d2a0407c652544254a48cdbc8f
MD5 ed3c732b6f5dafd992cc6d5670002ef3
BLAKE2b-256 4fe60e8134f60b0036d284acae51ea31e47763df4837040f271fcc541cb7867c

See more details on using hashes here.

Release history Release notifications | RSS feed

5.0.2

2 files

This release

5.0.1 This release

2 files

5.0.0

2 files

4.0.0

2 files

3.0.4

2 files

3.0.3

2 files

3.0.2

2 files

3.0.1

2 files

3.0.0

2 files

2.0.0

2 files

1.0.20

2 files

1.0.19

2 files

1.0.17

2 files

1.0.16

2 files

1.0.15

2 files

1.0.14

2 files

1.0.13

2 files

1.0.12

2 files

1.0.11

2 files

1.0.10

2 files

1.0.9

2 files

1.0.8

2 files

1.0.7

2 files

1.0.6

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page