diff --git a/clients/python/src/objectstore_client/auth.py b/clients/python/src/objectstore_client/auth.py index 0343985e..4afc2315 100644 --- a/clients/python/src/objectstore_client/auth.py +++ b/clients/python/src/objectstore_client/auth.py @@ -42,23 +42,49 @@ def __init__( self.expiry_seconds = expiry_seconds self.permissions = permissions if permissions is not None else Permission.max() - def sign_for_scope(self, usecase: str, scope: Scope) -> str: + def sign_for_scope( + self, + usecase: str, + scope: Scope, + permissions: list[Permission] | None = None, + expiry_seconds: int | None = None, + ) -> str: """ Sign a JWT for the passed-in usecase and scope using the configured key information, expiry, and permissions. - The JWT is signed using EdDSA, so `self.secret_key` must be an EdDSA private - key. `self.kid` is used by the Objectstore server to load the corresponding - public key from its configuration. + When ``permissions`` is ``None``, the generator's default permissions are + used. When provided, they override the defaults for this token only. + + When ``expiry_seconds`` is ``None``, the generator's default expiry is + used. + + Raises ``ValueError`` if any requested permission is not granted to + this generator. + + The JWT is signed using EdDSA, so ``self.secret_key`` must be an EdDSA + private key. ``self.kid`` is used by the Objectstore server to load the + corresponding public key from its configuration. """ + if permissions is not None: + escalated = set(permissions) - set(self.permissions) + if escalated: + raise ValueError( + "requested permissions not granted to this " + f"token generator: {sorted(escalated)}" + ) + headers = {"kid": self.kid} + expiry = expiry_seconds if expiry_seconds is not None else self.expiry_seconds claims = { "res": { "os:usecase": usecase, **{k: str(v) for k, v in scope.dict().items()}, }, - "permissions": self.permissions, - "exp": datetime.now(tz=UTC) + timedelta(seconds=self.expiry_seconds), + "permissions": ( + permissions if permissions is not None else self.permissions + ), + "exp": datetime.now(tz=UTC) + timedelta(seconds=expiry), } return jwt.encode(claims, self.secret_key, algorithm="EdDSA", headers=headers) diff --git a/clients/python/src/objectstore_client/client.py b/clients/python/src/objectstore_client/client.py index 02b735cc..74f18243 100644 --- a/clients/python/src/objectstore_client/client.py +++ b/clients/python/src/objectstore_client/client.py @@ -12,7 +12,7 @@ from urllib3.connectionpool import HTTPConnectionPool from objectstore_client import utils -from objectstore_client.auth import TokenGenerator, TokenProvider +from objectstore_client.auth import Permission, TokenGenerator, TokenProvider from objectstore_client.metadata import ( HEADER_EXPIRATION, HEADER_META_PREFIX, @@ -228,10 +228,36 @@ def __init__( self._scope = scope self._token = token - def mint_token(self) -> str | None: + def mint_token( + self, + permissions: list[Permission] | None = None, + expiry_seconds: int | None = None, + ) -> str: """ - Returns a signed token if a token or generator was provided or `None` otherwise. + Returns a signed token. + + When ``permissions`` is ``None``, the generator's default + permissions are used. When provided, they must be a subset of + the generator's permissions. + + When ``expiry_seconds`` is ``None``, the generator's default + expiry is used. + + Raises ``ValueError`` if no ``TokenGenerator`` is configured + or if any requested permission is not granted to the + generator. """ + if not isinstance(self._token, TokenGenerator): + raise ValueError("no token generator configured on this session") + return self._token.sign_for_scope( + self._usecase.name, + self._scope, + permissions, + expiry_seconds, + ) + + def _auth_token(self) -> str | None: + """Returns a token for internal auth headers.""" if isinstance(self._token, TokenGenerator): return self._token.sign_for_scope(self._usecase.name, self._scope) elif isinstance(self._token, str): @@ -244,7 +270,7 @@ def _make_headers(self) -> dict[str, str]: headers.update( dict(sentry_sdk.get_current_scope().iter_trace_propagation_headers()) ) - if token := self.mint_token(): + if token := self._auth_token(): headers["x-os-auth"] = f"Bearer {token}" return headers