Repository navigation
Feat/scoped installation token - #991
Conversation
Installation access tokens were always minted with installation-wide
scope: `request_installation_auth_token` posted an empty body and
`AuthState::Installation` carried no scope, so even automatic refresh
could only re-mint an installation-wide token. GitHub's
`/app/installations/{id}/access_tokens` endpoint accepts `repositories`
and `repository_ids` to scope a token down to specific repositories.
Scoping a token to only the repositories a job touches follows the
principle of least privilege: it limits the blast radius if the token
leaks, instead of granting access to every repository the installation
can reach. But octocrab could not do this together with its automatic
refresh, because the scope was not retained anywhere.
Store the requested scope in `AuthState::Installation` and send it in the
mint request, so a caller can scope the token via the new
`installation_builder(id)` API and every automatic refresh re-mints with
the same scope. `installation(id)` is unchanged behaviorally: it delegates
to the builder with no scope, which serializes to an empty body.
Note: `AuthState::Installation` gains `repositories` and `repository_ids`
fields, so code that constructs this variant directly must set them.
| }, | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] |
There was a problem hiding this comment.
CreateInstallationAccessToken is only used inside #[cfg(feature = "jwt")] fn request_installation_auth_token, so gating it avoids dead-code warnings when jwt is disabled:
| #[derive(serde::Serialize)] | |
| #[cfg(feature = "jwt")] | |
| #[derive(serde::Serialize)] |
| /// A builder for an installation-authenticated `Octocrab` with an optional repository scope. | ||
| pub struct InstallationRequestBuilder<'octo> { | ||
| crab: &'octo Octocrab, | ||
| installation: InstallationId, | ||
| repositories: Option<Vec<String>>, | ||
| repository_ids: Option<Vec<RepositoryId>>, | ||
| } | ||
|
|
||
| impl<'octo> InstallationRequestBuilder<'octo> { |
There was a problem hiding this comment.
InstallationRequestBuilder::build references AuthState::App and AuthState::Installation, which are both gated behind #[cfg(feature = "jwt")]. Gating InstallationRequestBuilder and its impl block ensures --no-default-features (without jwt) compiles:
| /// A builder for an installation-authenticated `Octocrab` with an optional repository scope. | |
| pub struct InstallationRequestBuilder<'octo> { | |
| crab: &'octo Octocrab, | |
| installation: InstallationId, | |
| repositories: Option<Vec<String>>, | |
| repository_ids: Option<Vec<RepositoryId>>, | |
| } | |
| impl<'octo> InstallationRequestBuilder<'octo> { | |
| /// A builder for an installation-authenticated `Octocrab` with an optional repository scope. | |
| #[cfg(feature = "jwt")] | |
| #[cfg_attr(docsrs, doc(cfg(feature = "jwt")))] | |
| pub struct InstallationRequestBuilder<'octo> { | |
| crab: &'octo Octocrab, | |
| installation: InstallationId, | |
| repositories: Option<Vec<String>>, | |
| repository_ids: Option<Vec<RepositoryId>>, | |
| } | |
| #[cfg(feature = "jwt")] | |
| impl<'octo> InstallationRequestBuilder<'octo> { |
| /// Returns a builder that authorizes via a specific installation and lets you scope the minted | ||
| /// access token to specific repositories. The scope is retained, so automatic token refresh | ||
| /// re-mints a token with the same scope. | ||
| pub fn installation_builder(&self, id: InstallationId) -> InstallationRequestBuilder<'_> { |
There was a problem hiding this comment.
Same #[cfg(feature = "jwt")] gate here to match Octocrab::installation:
| pub fn installation_builder(&self, id: InstallationId) -> InstallationRequestBuilder<'_> { | |
| #[cfg(feature = "jwt")] | |
| #[cfg_attr(docsrs, doc(cfg(feature = "jwt")))] | |
| pub fn installation_builder(&self, id: InstallationId) -> InstallationRequestBuilder<'_> { |
`InstallationRequestBuilder::build` refers to `AuthState::App` and `AuthState::Installation`, and both of those exist only when the `jwt` feature is enabled. Building with `--no-default-features` therefore failed. Gate the builder, `Octocrab::installation_builder` and the token request body behind `jwt`, matching `Octocrab::installation`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review! I applied your suggestions in 65d567d. The installation token builder is now gated behind |
No description provided.