Skip to content

Feat/scoped installation token - #991

Merged
dmgorsky merged 3 commits into
XAMPPRocky:mainfrom
zaimy:feat/scoped-installation-token
Oct 27, 2026
Merged

dmgorsky merged 3 commits into
XAMPPRocky:mainfrom
zaimy:feat/scoped-installation-token

Conversation

@dmgorsky

Copy link
Copy Markdown
Collaborator

No description provided.

zaimy added 2 commits July 13, 2026 10:32
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.

@dmgorsky dmgorsky left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Arigato gozaimasu for adding repository-scoped installation tokens! Left inline suggestions to gate the new installation token types/methods behind #[cfg(feature = "jwt")] so building without the jwt feature continues to compile cleanly.

Comment thread src/lib.rs
},
}

#[derive(serde::Serialize)]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

CreateInstallationAccessToken is only used inside #[cfg(feature = "jwt")] fn request_installation_auth_token, so gating it avoids dead-code warnings when jwt is disabled:

Suggested change
#[derive(serde::Serialize)]
#[cfg(feature = "jwt")]
#[derive(serde::Serialize)]

Comment thread src/lib.rs
Comment on lines +1428 to +1436
/// 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> {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:

Suggested change
/// 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> {

Comment thread src/lib.rs
/// 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<'_> {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Same #[cfg(feature = "jwt")] gate here to match Octocrab::installation:

Suggested change
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>
@zaimy

zaimy commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Thanks for the review! I applied your suggestions in 65d567d. The installation token builder is now gated behind jwt, and cargo check --no-default-features builds again.

@dmgorsky
dmgorsky merged commit 217fcbe into XAMPPRocky:main Oct 27, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 27, 2026
@zaimy
zaimy deleted the feat/scoped-installation-token branch November 2, 2026 02:10
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.

2 participants