~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Service auth is checked with atproto-client 0.7.1's verify_with

Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:auth/verify-with
.changeset/service-auth-verify-with.mdnew+7−0
@@ -0,0 +1,7 @@
---
default: patch
---
# Service auth is checked with atproto-client 0.7.1's `verify_with`
The host's service auth and an account's token for moving here are checked with atproto-client 0.7.1's `verify_with`, which judges a token's audience itself, and tells an issuer that can't be resolved from one naming no key. An account's token for moving here whose DID can't be resolved is refused `InvalidRequest` again, as before 0.6; one whose DID names no key, or whose signature doesn't hold, is still `InvalidToken`.
crates/group-host/src/authority_api.rs+3−6
@@ -10,7 +10,6 @@ use crate::space::NOTIFY_REPO_UPDATE;
use crate::space_api::{credential, forbidden, now, refused, service_caller};
use crate::store::{Registration, Space};
use crate::xrpc::XrpcError;
use atproto::service_auth::Claims;
use atproto::space::SpaceRef;
use atproto::space_auth::{self, Kind, Make, space_host_aud};
use atproto::{PublicKey, Value as Drisl, syntax, tid};
@@ -217,15 +216,13 @@ pub async fn notify_repo_update(
) -> Answer {
let uri = text(&body, "space")?;
let space = space_ref(uri)?;
let for_authority = |claims: &Claims| {
let aud = &claims.aud;
match *aud == space.authority || *aud == space_host_aud(&space.authority) {
let for_authority =
|aud: &str| match aud == space.authority || aud == space_host_aud(&space.authority) {
true => Ok(()),
false => Err(forbidden(
"notifyRepoUpdate is not for this space's authority",
)),
}
};
};
let caller = service_caller(&host, &headers, NOTIFY_REPO_UPDATE, for_authority).await?;
let (repo, rev) = (text(&body, "repo")?, text(&body, "repoRev")?);
if !syntax::is_tid(rev) {
crates/group-host/src/group_api.rs+2−3
@@ -17,7 +17,6 @@ use crate::scopes::covers;
use crate::space_api::{service_caller, unauthorized};
use crate::token_writes::missing;
use crate::xrpc::XrpcError;
use atproto::service_auth::Claims;
use atproto::space::SpaceRef;
use axum::Json;
use axum::extract::{Query, State};
@@ -48,11 +47,11 @@ pub(crate) async fn caller(
if !headers.contains_key(header::AUTHORIZATION) {
return host.console_caller(headers).map_err(XrpcError::internal);
}
let ours = |claims: &Claims| match for_host(host, &claims.aud) {
let ours = |aud: &str| match for_host(host, aud) {
true => Ok(()),
false => Err(unauthorized(
"BadJwtAudience",
format!("a token for {}, not this host", claims.aud),
format!("a token for {aud}, not this host"),
)),
};
let claims = service_caller(host, headers, lxm, ours).await?;
crates/group-host/src/import.rs+9−7
@@ -39,7 +39,7 @@ use anyhow::Result;
use atproto::plc::Signed;
use atproto::service_auth;
use atproto::{Cid, PublicKey, did};
use atproto_client::service_auth::verify_any;
use atproto_client::service_auth::{Error as Unverified, verify_with};
use axum::Json;
use axum::extract::{Query, State};
use axum::http::{HeaderMap, StatusCode, header};
@@ -360,13 +360,15 @@ async fn proof(host: &Host, jwt: &str) -> Result<String, XrpcError> {
if !did.starts_with("did:plc:") {
return Err(not_plc());
}
if !for_host(host, &claims.aud) || claims.lxm.as_deref() != Some(CREATE_ACCOUNT) {
return Err(bad("not for this host's createAccount"));
}
let now = (now_millis() / 1000) as u64;
let auds = [claims.aud.as_str()];
let checked = verify_any(&host.identity, jwt, &auds, CREATE_ACCOUNT, now).await;
checked.map_err(|e| bad(&e.to_string()))?;
let ours = |aud: &str| for_host(host, aud);
let checked = verify_with(&host.identity, jwt, ours, CREATE_ACCOUNT, now).await;
checked.map_err(|e| match e {
Unverified::Unresolved { did, error } => {
invalid(format!("could not resolve {did}: {error}"))
}
e => bad(&e.to_string()),
})?;
Ok(did)
}
crates/group-host/src/space_api.rs+26−15
@@ -16,7 +16,7 @@ use atproto::space::{
};
use atproto::space_auth::{self, CLOCK_SKEW, CREDENTIAL_MAX_AGE, Expect, Kind, TokenError};
use atproto::{Cid, drisl, syntax};
use atproto_client::service_auth::{Error as Unverified, verify_any};
use atproto_client::service_auth::{Error as Unverified, verify_with};
use axum::Json;
use axum::extract::{Query, Request, State};
use axum::http::{HeaderMap, HeaderName, HeaderValue, Method, StatusCode, header};
@@ -82,9 +82,13 @@ pub(crate) async fn credential(
let check =
|key: &atproto::PublicKey| space_auth::verify(Kind::Credential, jwt, key, &expect, now());
let checked = host.identity.with_key(&token.iss, check).await;
let checked = checked
.ok_or_else(|| unauthorized("BadJwtIss", format!("no signing key for {}", token.iss)))?;
checked.map_err(refused)?;
let no_key = |e| {
unauthorized(
"BadJwtIss",
format!("no signing key for {}: {e}", token.iss),
)
};
checked.map_err(no_key)?.map_err(refused)?;
let audience = text("atproto-space-audience").filter(|a| syntax::is_did(a));
let audience = audience.ok_or_else(|| {
unauthorized("BadSpaceSignature", "missing or invalid space audience DID")
@@ -489,7 +493,7 @@ pub(crate) async fn service_caller(
host: &Host,
headers: &HeaderMap,
lxm: &str,
ours: impl FnOnce(&Claims) -> Result<(), XrpcError>,
ours: impl Fn(&str) -> Result<(), XrpcError>,
) -> Result<Claims, XrpcError> {
let bearer = headers
.get(header::AUTHORIZATION)
@@ -503,13 +507,20 @@ pub(crate) async fn service_caller(
format!("a token not for {lxm}"),
));
}
ours(&claims)?;
let auds = [claims.aud.as_str()];
let checked = verify_any(&host.identity, token, &auds, lxm, now()).await;
checked.map_err(|e| match e {
Unverified::NoKey(_) => unauthorized("BadJwtIss", e.to_string()),
e => unauthorized("BadJwt", e.to_string()),
})?;
// The audience's refusal, kept for the answer in place of the crate's.
let refusal = std::sync::Mutex::new(None);
let keep = |e| *refusal.lock().expect("a refusal kept") = Some(e);
let takes = |aud: &str| ours(aud).map_err(keep).is_ok();
let checked = verify_with(&host.identity, token, takes, lxm, now()).await;
checked.map_err(
|e| match (refusal.lock().expect("a refusal kept").take(), e) {
(Some(refused), _) => refused,
(None, e @ (Unverified::Unresolved { .. } | Unverified::NoKey(_))) => {
unauthorized("BadJwtIss", e.to_string())
}
(None, e) => unauthorized("BadJwt", e.to_string()),
},
)?;
Ok(claims)
}
@@ -524,10 +535,10 @@ pub async fn notify_credentials_revoked(
headers: HeaderMap,
Json(body): Json<Value>,
) -> Result<Json<Value>, XrpcError> {
let hosted_here = |claims: &Claims| {
let hosted = host.store.account(&claims.aud);
let hosted_here = |aud: &str| {
let hosted = host.store.account(aud);
let hosted = hosted.map_err(XrpcError::internal)?;
match hosted.is_some_and(|a| a.did == claims.aud) {
match hosted.is_some_and(|a| a.did == aud) {
true => Ok(()),
false => Err(forbidden("revocation audience is not a repo hosted here")),
}
crates/group-host/tests/import.rs+1−2
@@ -551,8 +551,7 @@ async fn refuses_the_account_signed_in_a_did_web_and_one_whose_did_names_no_key(
json!({"serviceJwt": token("did:plc:nobody", &w.key, &w.h.host.config.service_did, CREATE_ACCOUNT)}),
)
.await;
// A DID that resolves to nothing has no key to check its token by.
assert_eq!(error(&said), (400, "InvalidToken"));
assert_eq!(error(&said), (400, "InvalidRequest"));
// A DID whose document names no key.
let rotation = Keypair::generate(Curve::K256);
let mut op = Op::atproto(
Cargo.lock+4−4
@@ -83,9 +83,9 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
[[package]]
name = "atproto"
version = "0.4.3"
version = "0.4.4"
source = "sparse+https://knotless.dev/cargo/did:plc:bcgltzqazw5tb6k2g3ttenbj/3mzr5vldibtui/"
checksum = "0eefab92b923b5ca527f1e1f251ddcc8001d2d568a435bc9d8220bd2c1ceae8e"
checksum = "2c5e4e543f0b770a3dc16c30bb5874837aef4979fe714bc7219d0adb4eec176a"
dependencies = [
"base64 0.22.1",
"blake3",
@@ -101,9 +101,9 @@ dependencies = [
[[package]]
name = "atproto-client"
version = "0.6.0"
version = "0.7.1"
source = "sparse+https://knotless.dev/cargo/did:plc:bcgltzqazw5tb6k2g3ttenbj/3mzr5vldibtui/"
checksum = "224e5ba76374157d99c1f1f91816fef07f54e3b1a1ccd4a1025e51f5bfabc75b"
checksum = "6e744df30aeca5f942e7cbb0fafddc7d6bd4abf922737f0ba594844180e32297"
dependencies = [
"atproto",
"bytes",
Cargo.toml+1−1
@@ -17,7 +17,7 @@ unsafe_code = "forbid"
[workspace.dependencies]
atproto = { version = "0.4.3", registry = "atproto" }
atproto-client = { version = "0.6", registry = "atproto" }
atproto-client = { version = "0.7.1", registry = "atproto" }
group-host = { path = "crates/group-host" }
group-host-mcp = { path = "crates/group-host-mcp" }
anyhow = "1"