Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/lore-server-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ This field is optional. When it's absent, the server starts with the presigned U
| `jwt_issuer` | none | Expected JWT `iss` claim. When set, tokens with a different issuer are rejected; when unset, issuer validation is skipped. |
| `jwt_audience` | none | Array of accepted JWT `aud` values. A token's audience must match one entry; when unset, audience validation is skipped. |
| `jwk` | none | The `[server.auth.jwk]` sub-table below. Its presence enables JWT verification. |
| `managed_repositories_file` | none | Path of a JSON policy naming managed repositories and their publishers (format in `lore-server/src/auth/managed.rs`). On a managed repository only an explicit `urc-<id>` grant carrying `publish`, held by a listed publisher (`sub`), writes or performs elevated operations; every other grant reads only. Unset: nothing is managed. Set but missing or invalid: the server refuses to start; if it breaks later, every repository is treated as managed with no publishers until it is valid again. Changes are picked up within 5 s. |

`[server.auth.jwk]`:

Expand Down
53 changes: 36 additions & 17 deletions lore-server/src/auth/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use tracing::debug;
use tracing::warn;

use super::jwk::JWKServiceError;
use super::managed;
use super::managed::EffectiveGrants;
use super::managed::Snapshot;
use crate::auth::jwk::JWKService;

#[serde_as]
Expand Down Expand Up @@ -53,25 +56,26 @@ impl ResourcePermission {
self.resource_id == "urc-*"
}

pub fn matches_repository(&self, repository_id: &String) -> bool {
self.resource_id == *repository_id || self.is_wildcard_resource()
pub fn matches_repository(&self, repository_id: &str) -> bool {
self.resource_id == repository_id || self.is_wildcard_resource()
}

/// Whether this grant allows reading repository content. Fail-closed: a grant must
/// explicitly carry a permission that implies read; an empty permission list grants
/// nothing. `write`/`owner`/`admin` imply read.
/// nothing. `write`/`owner`/`admin`/`publish` imply read.
pub fn allows_read(&self) -> bool {
self.permission.iter().any(|permission| {
matches!(
permission.as_str(),
"read" | "write" | "owner" | "admin" | "obliterate" | "migrate"
"read" | "write" | "owner" | "admin" | "obliterate" | "migrate" | "publish"
)
})
}

/// Whether this grant allows mutating the repository (push, remote commit, locks,
/// mutable-store writes). Fail-closed: requires `write`, `owner`, or `admin`; a grant
/// listing only `read` (or nothing) does not.
/// listing only `read` (or nothing) does not. On a managed repository only a publisher's
/// grant counts at all: see [`super::managed`].
pub fn allows_write(&self) -> bool {
self.permission
.iter()
Expand Down Expand Up @@ -196,6 +200,19 @@ pub fn remaining_lifetime(authorization: &AuthorizationToken) -> Duration {
Duration::from_secs(authorization.expires).saturating_sub(now)
}

/// The caller's grants that count on `repository` under `policy`.
pub fn effective_grants<'a>(
authorization: &'a AuthorizationToken,
repository: lore_revision::lore::RepositoryId,
policy: &Snapshot,
) -> EffectiveGrants<'a> {
policy.effective_grants(
&authorization.user_id,
&format!("urc-{repository}"),
authorization.resources.as_deref().unwrap_or_default(),
)
}

pub fn verify_authorization(
authorization: &AuthorizationToken,
repository: lore_revision::lore::RepositoryId,
Expand All @@ -216,23 +233,25 @@ pub fn verify_authorization(

/// Like [`verify_authorization`], but for mutating operations: the matching resource grant
/// must include write (or an elevated grant implying it). A token scoped `read` on the
/// repository can therefore clone and sync but never push, lock, or store.
/// repository can therefore clone and sync but never push, lock, or store. On a managed
/// repository only a listed publisher's explicit `publish` grant writes.
pub fn verify_write_authorization(
authorization: &AuthorizationToken,
repository: lore_revision::lore::RepositoryId,
) -> Result<(), JwtVerifierError> {
if let Some(resources) = authorization.resources.as_ref() {
let checked_repository = format!("urc-{repository}");
for authorized_resource in resources.iter() {
if authorized_resource.matches_repository(&checked_repository)
&& authorized_resource.allows_write()
{
return Ok(());
}
}
}
verify_write_authorization_under(authorization, repository, &managed::current())
}

Err(JwtVerifierError::NotAuthorized)
pub fn verify_write_authorization_under(
authorization: &AuthorizationToken,
repository: lore_revision::lore::RepositoryId,
policy: &Snapshot,
) -> Result<(), JwtVerifierError> {
if effective_grants(authorization, repository, policy).allows_write() {
Ok(())
} else {
Err(JwtVerifierError::NotAuthorized)
}
}

#[cfg(test)]
Expand Down
Loading
Loading