alis-build-iam
Authentication and authorization for services on the Alis Build platform.
One identity model that survives every hop: decoded from a token at the edge, carried through the request, forwarded to the services you call, and used to answer authorization questions when it gets there.
pip install alis-build-iam
The core has no dependencies. Everything that needs a third-party library lives behind an extra:
| Install | Adds | Pulls in |
|---|---|---|
alis-build-iam |
Identity, wire format, context, HTTP headers, ASGI | nothing |
alis-build-iam[grpc] |
gRPC metadata and interceptors | grpcio |
alis-build-iam[authz] |
Role and permission checks, policy fetching | grpc-google-iam-v1 |
alis-build-iam[authn] |
OAuth2/OIDC client, token verification | httpx, cryptography |
alis-build-iam[all] |
everything | all of the above |
Importing a subpackage without its extra tells you exactly what to install.
Status: early development. The public API is not yet stable.
The identity
An Identity is a frozen value describing a user, a service account, or a
system actor.
from alis.build import iam
identity = iam.Identity(
type=iam.IdentityType.USER,
id="1934872948",
email="john@example.com",
group_ids=["df913r888"],
)
identity.policy_member() # 'user:1934872948'
identity.user() # 'users/1934872948'
identity.is_privileged() # False
System identities bypass authorization entirely, which is what makes trusted internal calls work. Register the emails that should count as system, and the humans who get the same bypass, once at startup:
iam.add_system_email("alis-build@my-project.iam.gserviceaccount.com")
iam.add_admin_email("admin@example.com")
An admin keeps their identity type, so resource names and audit trails still
say "user". The environment's own service account is registered automatically
when ALIS_OS_PROJECT is set.
Scoped credentials
A credential can be issued below the authority of the principal it names.
restricted switches off both bypasses — the system and admin one, and the
open-role one in authz — so the credential passes only for roles it
explicitly carries.
scoped = iam.Identity(
type=iam.IdentityType.USER,
id="1934872948",
email="admin@example.com",
restricted=True,
authz_roles=["roles/app.developer"],
)
scoped.is_admin() # True — the principal is still an admin
scoped.is_privileged() # False — but this credential does not bypass
authz_roles carries the issuing service's role allowlist. This package never
enforces it: role ids are unnamespaced and registered per process, so the same
id routinely means different permissions in different services. Only the
service that minted an allowlist can read it.
The difference between an absent allowlist and an empty one is load-bearing:
| Claim in the token | authz_roles |
Meaning |
|---|---|---|
absent, or null |
None |
unrestricted |
"authz_roles": [] |
[] |
restricted to no roles at all |
"authz_roles": ["roles/x"] |
["roles/x"] |
restricted to those roles |
Never normalise the empty case to None: collapsing the two turns the most
restricted credential there is into the least restricted one.
restricted is fail-open across versions. A consumer that predates it ignores
the claim and grants the credential full authority, so an issuer keeps its own
guards until every consumer has upgraded.
Carrying it through a request
The identity is ambient, held in a context variable rather than passed down through every call:
with iam.identity_context(identity):
...
iam.from_context() # raises if there is none
iam.from_context_or_none() # None if there is none
Child asyncio tasks inherit it and cannot leak changes back to the parent.
Threads start empty, so use contextvars.copy_context().run(fn) if you need it
to cross one.
Over HTTP
from alis.build.iam.asgi import IdentityMiddleware
app = IdentityMiddleware(app) # 401s anonymous requests
app = IdentityMiddleware(app, required=False, exclude_paths={"/healthz"})
Pure ASGI3, so it works under Starlette, FastAPI, Litestar, Quart and async Django without importing any of them. For anything else, the primitives take a plain header mapping:
from alis.build.iam.headers import identity_from_headers, identity_headers
identity = identity_from_headers(request.headers)
response = httpx.get(url, headers=identity_headers(identity))
Over gRPC
from alis.build.iam.grpc import AsyncIdentityInterceptor, IdentityClientInterceptor
server = grpc.aio.server(interceptors=[AsyncIdentityInterceptor()])
channel = grpc.intercept_channel(channel, IdentityClientInterceptor())
The server interceptor binds the identity for the handler and refuses calls
without one as UNAUTHENTICATED. The client interceptor forwards whatever
identity is bound, so a service handling one request and calling another passes
the caller through without threading it by hand. Calls with no identity bound
are left alone, which keeps health checks working.
Authorization
Register what each role grants once at startup, then close the registry:
from alis.build.iam.authz import add_role_permissions, default_registry
def configure_authorization() -> None:
add_role_permissions("roles/viewer", ["/example.v1.Books/Get"])
add_role_permissions("roles/admin", ["/example.v1.Books/Delete"], inherits=["roles/viewer"])
default_registry().freeze()
A restricted identity never passes via an open role or the privileged bypass; it passes only for roles it explicitly carries.
freeze() closes the registry to further changes and logs anything suspicious
about it, such as a permission an open role has made reachable by everyone.
Then, per request:
from alis.build.iam.authz import Authorizer
authorizer = Authorizer(iam.from_context())
if not authorizer.has_permission("/example.v1.Books/Get", policy):
raise PermissionDenied
Roles are seeded from the policy the identity carries, and any policies you
pass to a check apply to that check only. To reuse them across several checks,
with_roles_from_policies(...) returns a new authorizer rather than mutating
the one you have.
Two behaviours worth knowing before you rely on them:
- An open role is an allow-list of role names, not a grant to anyone in particular. A check for an open role succeeds for every caller, and every permission that role grants becomes reachable along with it.
- A conditional policy binding cannot be evaluated here, because the
condition needs the resource being accessed and a check is never given it. By
default such a binding grants and logs a warning naming the condition. Set
ConditionMode.DENYonce those warnings have stopped appearing.
Fetching policies
An authorization decision often needs several resources' policies. Fetching them one after another adds up their round trips:
from alis.build.iam.policypool import PolicyPool
pool = PolicyPool()
pool.add("resources/a", client.get_iam_policy)
pool.add("resources/b", client.get_iam_policy)
collected = await pool.gather()
authorizer.has_role(["roles/admin"], *collected.policies)
A failed fetch does not discard the successful ones — collapsing a partial
failure to "no policies" turns a transient outage into a blanket refusal. Call
collected.raise_for_errors() when a partial answer will not do.
SyncPolicyPool does the same on threads.
Authentication
from alis.build.iam.authn import AuthnClient
client = AuthnClient.from_base_url("https://auth.example.com", client_id=..., client_secret=...)
Signing a user in — send them to start.url and set start.cookie on that
same response:
start = client.start_login("https://app.example.com/callback", return_to="/dashboard")
On the callback route:
result = await client.complete_login(request.query, request.cookies, redirect_uri)
client.validate_id_token_nonce(result.tokens.id_token, result.nonce)
Checking a token on each request:
identity = await client.verified_identity(bearer_token)
authenticate(tokens) checks a stored token set and renews it only if the
access token has actually expired, returning whether it did so you know when to
persist. The renewed token is validated too, against the same audience — a
refresh is not a way around a check the original token failed.
SyncAuthnClient offers the same surface for a blocking server.
The aud claim is accepted in both forms RFC 7519 §4.1.3 allows, a single
string or an array of strings. A token passes when any audience it names is one
you asked for, and a token naming no audience at all is accepted, since not
every issuer sets one.
Only RS256 signatures are verified. Published keys are cached with a TTL and a minimum refresh interval, and concurrent misses trigger one fetch rather than one per request.
Testing
from alis.build.iam.testing import fresh_authz_registry, fresh_system_config
@pytest.fixture(autouse=True)
def _iam():
with fresh_system_config(), fresh_authz_registry():
yield
The registries are process-wide, so without this a test that registers an admin email privileges that address for every test after it. The authorization registry defaults to stricter settings in tests than in production: an unregistered permission raises rather than denying, and a conditional binding raises rather than granting, so either shows up as a failing test.
alis.build.iam.authz.testing has policy(), binding() and
encode_policy() for writing readable authorization tests.
License
Apache-2.0
Metadata
Release files for alis-build-iam 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| alis_build_iam-0.1.0.tar.gz | 81.9 kB | With provenance | Details for alis_build_iam-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| alis_build_iam-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for alis_build_iam-0.1.0-py3-none-any.whl |
Total release size: 151.5 kB
Release files / alis_build_iam-0.1.0.tar.gz
| Download URL | alis_build_iam-0.1.0.tar.gz |
|---|---|
| Size | 81.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fa2045a88906d13f0b93ac40a0da89bec8e5590e97c1501c0cda196332dae5e5
|
|
BLAKE2b-256 checksum How to use checksums |
34cd1c7e1e3af1a56f766cddc341556829bbb42d20c32bb74994a91cddb4cc0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.
Transparency logRelease files / alis_build_iam-0.1.0-py3-none-any.whl
| Download URL | alis_build_iam-0.1.0-py3-none-any.whl |
|---|---|
| Size | 69.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0100fbd8f9f9a1aae01f4bcd36958caf34c449cda9da8ca35bf9360177d73496
|
|
BLAKE2b-256 checksum How to use checksums |
226b532c9c9b925eb49332f287f3dfd7f1a6af26b4397abdd82f97c5f4e9f4ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.
Transparency logSecurity for alis-build-iam 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for alis_build_iam-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.