chadtmiller.com/group-host
atmosphere group host
0
0
An app signed in as a group calls the host's methods and reads its spaces
.changeset/group-session-delegation.mdnew+7−0
| @@ -0,0 +1,7 @@ | ||
--- | ||
default: minor | ||
--- | ||
| ||
# An app signed in as a group reads its spaces | ||
| ||
`com.atproto.space.getDelegationToken` answers an app signed in as a group, as a PDS answers one signed in as its account: a delegation token the group signs for one of its spaces, which buys a credential from `com.atproto.space.authorizeSpaceSync` to read the space, with `notify` if the app wants to hear of its changes. The session's scope must read the space (`space:<type>?action=read`, `ScopeMissing` otherwise), and the member who approved it must read it at that moment (`NotAuthorized`). So an app an admin approves once reads a group's `meta`, `members` and its own spaces, the members' records in them at their PDSes included, the Atmosphere Groups proposal's shared application session. Before, a group session could write in its spaces and read none of them. | ||
.changeset/group-session-methods.mdnew+9−0
| @@ -0,0 +1,9 @@ | ||
--- | ||
default: minor | ||
--- | ||
| ||
# An app signed in as a group calls its methods with rpc: scopes, and what its grant bought ends with it | ||
| ||
Approval keeps `rpc:org.atgrp.listJoinRequests`, `denyJoinRequest`, `listModSubjects`, `getModSubjectHistory` and `resolveModSubject` (for this host or `aud=*`) when the approving member's roles hold the action each asks (`admit`, `mod.read`, `mod.resolve`). Those methods then answer the group session's DPoP token as well as a member's service auth, asking the member's roles again at each call (`Forbidden`), and the scope (`ScopeMissing`). | ||
| ||
The delegation tokens a group session is given, and the space credentials and `notify` registrations they buy, are kept with its grant. When the grant ends (`revokeGroupToken`, `/oauth/revoke`, `deleteSession`, its member put out), or its member reads a space no more after a members-space write or a policy change, its credentials there are revoked, the writers' PDSes told, its registrations withdrawn, and its unspent delegations buy nothing. | ||
.changeset/last-configurer-message.mdnew+7−0
| @@ -0,0 +1,7 @@ | ||
--- | ||
default: patch | ||
--- | ||
| ||
# LastConfigurer says what to do, in words a person reads | ||
| ||
`leaveGroup`, refused because nobody else could run the group, says "You're the group's only admin. Make someone else an admin first." A write to the members space that would leave nobody running it says "That would leave the group with no admin. Make someone else an admin first." `leaveGroup`'s lexicon describes `LastConfigurer` in the same words, so an app can show it to the person. | ||
.changeset/token-member.mdnew+7−0
| @@ -0,0 +1,7 @@ | ||
--- | ||
default: minor | ||
--- | ||
| ||
# /oauth/token names the approving member | ||
| ||
A token response, to a code or a refresh, carries `member`, the DID of the member who approved the grant: this host's own field, beside `sub`, the group. | ||
crates/group-host/src/app_passwords.rs+2−0
| @@ -325,6 +325,8 @@ pub async fn delete_session(State(host): State<Arc<Host>>, headers: HeaderMap) - | ||
host.store | ||
.end_grant(&grant.id) | ||
.map_err(XrpcError::internal)?; | ||
let bought = host.end_grant_reads(&grant.group).await; | ||
bought.map_err(XrpcError::internal)?; | ||
Ok(Json(json!({}))) | ||
} | ||
| ||
crates/group-host/src/authority.rs+62−1
| @@ -555,9 +555,70 @@ impl Host { | ||
/// once for the repos here, and told to the host of each writer | ||
/// elsewhere in those spaces, best effort, as the authority. | ||
pub async fn revoke_credentials(&self, authority: &str, did: &str) -> Result<()> { | ||
let issued = self.store.issued_to(authority, did, now())?; | ||
self.revoke_issued(authority, issued).await | ||
} | ||
| ||
/// Whether the grant `id` still lasts, and its member reads `space`, | ||
/// at `uri`, now: what a delegation it was given buys with. | ||
pub fn grant_reads(&self, id: &str, uri: &str, space: &Space) -> Result<bool> { | ||
let Some(grant) = self.store.grant_by_id(id)? else { | ||
return Ok(false); | ||
}; | ||
self.may(uri, space, Some(&grant.member), Access::Read) | ||
} | ||
crates/group-host/src/authority.rsline 569
| ||
| ||
/// Ends what the grants of apps signed in as `authority` bought of a | ||
/// space of its that they read no more, the grant gone or its member | ||
/// no longer reading there: their credentials revoked, as | ||
/// `revoke_credentials` does, and their syncers' registrations | ||
/// withdrawn, told nothing more. | ||
pub async fn end_grant_reads(&self, authority: &str) -> Result<()> { | ||
let mut asked = std::collections::BTreeMap::new(); | ||
let mut gone = Vec::new(); | ||
for (space, jti, expires_at, grant) in self.store.bought_of(authority, now())? { | ||
if !self.still_reads(&mut asked, &grant, &space)? { | ||
self.store.forget_bought(&space, &jti)?; | ||
gone.push((space, jti, expires_at)); | ||
} | ||
} | ||
for (space, to, grant) in self.store.bound_registrations(authority)? { | ||
if !self.still_reads(&mut asked, &grant, &space)? { | ||
self.store.unregister(&space, &to)?; | ||
} | ||
} | ||
self.revoke_issued(authority, gone).await | ||
} | ||
| ||
/// `grant_reads` for the grant `grant` and the space at `uri`, asked | ||
/// once for each pair kept in `asked`. | ||
fn still_reads( | ||
&self, | ||
asked: &mut std::collections::BTreeMap<(String, String), bool>, | ||
grant: &str, | ||
uri: &str, | ||
) -> Result<bool> { | ||
let key = (grant.to_string(), uri.to_string()); | ||
if let Some(reads) = asked.get(&key) { | ||
return Ok(*reads); | ||
} | ||
// `bought_of` and `bound_registrations` name only spaces here. | ||
let space = self.store.space(uri)?.context("the space is here")?; | ||
let reads = self.grant_reads(grant, uri, &space)?; | ||
asked.insert(key, reads); | ||
Ok(reads) | ||
} | ||
| ||
/// Revokes the credentials `issued` to `authority`'s spaces, each its | ||
/// space, jti and lapse, as `revoke_credentials` says. | ||
async fn revoke_issued( | ||
&self, | ||
authority: &str, | ||
issued: Vec<(String, String, i64)>, | ||
) -> Result<()> { | ||
let mut by_space: std::collections::BTreeMap<String, (Vec<String>, i64)> = | ||
Default::default(); | ||
for (space, jti, expires_at) in self.store.issued_to(authority, did, now())? { | ||
for (space, jti, expires_at) in issued { | ||
let (jtis, until) = by_space.entry(space).or_default(); | ||
jtis.push(jti); | ||
*until = (*until).max(expires_at + 2 * space_auth::CLOCK_SKEW as i64); | ||
crates/group-host/src/authority_api.rs+19−1
| @@ -115,8 +115,20 @@ pub async fn authorize_space_sync( | ||
let user = delegation.as_ref().map(|d| d.iss.as_str()); | ||
let hosted = host.store.space(uri).map_err(XrpcError::internal)?; | ||
let hosted = hosted.ok_or_else(not_found)?; | ||
// A delegation a group's grant was given here buys only while the | ||
// grant lasts and its member reads the space. | ||
let grant = match &delegation { | ||
Some(d) => host.store.delegation_grant(&d.jti), | ||
None => Ok(None), | ||
}; | ||
let grant = grant.map_err(XrpcError::internal)?; | ||
let may = host.may(uri, &hosted, user, Access::Read); | ||
if !may.map_err(XrpcError::internal)? { | ||
let mut may = may.map_err(XrpcError::internal)?; | ||
if let Some(id) = grant.as_deref().filter(|_| may) { | ||
let reads = host.grant_reads(id, uri, &hosted); | ||
may = reads.map_err(XrpcError::internal)?; | ||
} | ||
if !may { | ||
// A syncer that lost the space is told of it no more. | ||
if let Some(to) = ¬ify { | ||
let gone = host.store.unregister(uri, to); | ||
| @@ -156,11 +168,17 @@ pub async fn authorize_space_sync( | ||
.issued(uri, user, &issued.jti, issued.exp as i64, now() as i64) | ||
.map_err(XrpcError::internal)?; | ||
} | ||
if let Some(id) = &grant { | ||
let kept = host.store.bought(uri, &issued.jti, id); | ||
kept.map_err(XrpcError::internal)?; | ||
} | ||
// Renewed with each credential, so a syncer that keeps authorizing | ||
// stays registered. | ||
if let Some(to) = ¬ify { | ||
let kept = host.store.register(uri, to, issued.exp as i64); | ||
kept.map_err(XrpcError::internal)?; | ||
let bound = host.store.bind_registration(uri, to, grant.as_deref()); | ||
bound.map_err(XrpcError::internal)?; | ||
} | ||
// Read once registered, so that any change after it is told. | ||
let space_rev = host.store.space_rev(uri).map_err(XrpcError::internal)?; | ||
crates/group-host/src/config.rs+13−0
| @@ -3,6 +3,7 @@ | ||
//! `docs/reference/config.md`. | ||
| ||
use anyhow::{Context, Result, bail}; | ||
use std::collections::BTreeMap; | ||
use std::path::PathBuf; | ||
| ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
| @@ -24,6 +25,10 @@ pub struct Config { | ||
/// How a `did:web`'s document is read, `DID_WEB_SCHEME`: `https`, or | ||
/// `http` for a network of test servers. | ||
pub did_web_scheme: String, | ||
/// Where a `did:web`'s document is read instead of at its own host, | ||
/// `DID_WEB_HOSTS`, comma-separated `host=address` pairs, for a | ||
/// network of test servers: `grain.social=127.0.0.1:2584`. | ||
pub did_web_hosts: BTreeMap<String, String>, | ||
/// How a handle is resolved, `HANDLE_RESOLVERS`, comma-separated, each | ||
/// tried in turn: `well-known:<scheme>` for the handle's own | ||
/// `/.well-known/atproto-did`, or a service answering | ||
| @@ -122,6 +127,13 @@ impl Config { | ||
.filter(|u| !u.is_empty()) | ||
.collect() | ||
}; | ||
let mut did_web_hosts = BTreeMap::new(); | ||
for pair in list("DID_WEB_HOSTS") { | ||
let Some((host, address)) = pair.split_once('=') else { | ||
bail!("DID_WEB_HOSTS is host=address pairs, not {pair}"); | ||
}; | ||
did_web_hosts.insert(host.to_string(), address.to_string()); | ||
} | ||
let group_founders = list("GROUP_FOUNDERS"); | ||
let mut handle_resolvers = urls("HANDLE_RESOLVERS"); | ||
if handle_resolvers.is_empty() { | ||
| @@ -175,6 +187,7 @@ impl Config { | ||
data_dir, | ||
plc_url, | ||
did_web_scheme, | ||
did_web_hosts, | ||
handle_resolvers, | ||
group_founders, | ||
crawlers, | ||
crates/group-host/src/group_api.rs+42−0
| @@ -8,12 +8,14 @@ use crate::Host; | ||
use crate::api::{Params, page, param}; | ||
use crate::group::{Made, NewGroup, members_space, meta_space, rkey_did}; | ||
use crate::member_writes::{expanded, held}; | ||
use crate::oauth_resource::Refusal; | ||
use crate::rbac::{ | ||
ACTIONS, Grant, INTERNAL, NS, actions_for, actions_of, label_writes_in, listed, | ||
repo_collections_for, roles_of, scope_of, | ||
}; | ||
use crate::scopes::covers; | ||
use crate::space_api::{service_caller, unauthorized}; | ||
use crate::token_writes::missing; | ||
use crate::xrpc::XrpcError; | ||
use axum::Json; | ||
use axum::extract::{Query, State}; | ||
| @@ -64,6 +66,46 @@ pub(crate) async fn signed_in( | ||
who.ok_or_else(|| unauthorized("AuthMissing", "service auth is required")) | ||
} | ||
| ||
/// Who calls `lxm`, one of the host's own methods, sent with HTTP `verb`, | ||
/// to do `action` in `group`: a member holding it, by their service auth | ||
/// or the console's cookie; or an app signed in as the group (a DPoP | ||
/// token), whose scope holds `rpc:lxm` and whose member holds `action` at | ||
/// this call, as a session's writes are checked. The answer is the member. | ||
pub(crate) async fn doing( | ||
host: &Host, | ||
headers: &HeaderMap, | ||
verb: &str, | ||
lxm: &str, | ||
group: &str, | ||
action: &str, | ||
) -> Result<String, Refusal> { | ||
let scheme = headers | ||
.get(header::AUTHORIZATION) | ||
.and_then(|v| v.to_str().ok()) | ||
.and_then(|a| a.split_once(' ')); | ||
let me = if scheme.is_some_and(|(s, _)| s.eq_ignore_ascii_case("DPoP")) { | ||
let acting = host.acting(headers, verb, &format!("/xrpc/{lxm}"))?; | ||
if acting.grant.group != group { | ||
return Err(forbidden("a session for another group").into()); | ||
} | ||
if !acting.permissions.calls(lxm, |aud| for_host(host, aud)) { | ||
return Err(missing(format!("rpc:{lxm}")).into()); | ||
} | ||
acting.grant.member | ||
} else { | ||
signed_in(host, headers, lxm).await? | ||
}; | ||
group_of(host, group)?; | ||
if !host.may_do(group, &me, action)? { | ||
return Err(forbidden(format!("missing {action}")).into()); | ||
} | ||
Ok(me) | ||
} | ||
| ||
fn forbidden(message: impl Into<String>) -> XrpcError { | ||
XrpcError::new(StatusCode::FORBIDDEN, "Forbidden", message) | ||
} | ||
| ||
fn strings(body: &Value, key: &str, field: &str) -> Option<Vec<String>> { | ||
body.get(key).map(|_| listed(body, key, field)) | ||
} | ||
crates/group-host/src/group_join.rs+17−26
| @@ -12,9 +12,10 @@ | ||
use crate::Host; | ||
use crate::api::{Params, invalid_cursor, page, param}; | ||
use crate::group::meta_space; | ||
use crate::group_api::{group_of, signed_in}; | ||
use crate::group_api::{doing, group_of, signed_in}; | ||
use crate::group_invites::invite_uri; | ||
use crate::group_members::{Members, acting}; | ||
use crate::group_members::Members; | ||
use crate::oauth_resource::Refusal; | ||
use crate::rbac::{INTERNAL, NS}; | ||
use crate::store::JoinRequest; | ||
use crate::time::{iso, now_millis}; | ||
| @@ -125,22 +126,16 @@ pub async fn get_join_request( | ||
})) | ||
} | ||
| ||
/// The requests waiting to join, newest first, for one holding `admit`. | ||
/// The requests waiting to join, newest first, for one holding `admit`, | ||
/// or an app signed in as the group with its `rpc:` scope. | ||
pub async fn list_join_requests( | ||
State(host): State<Arc<Host>>, | ||
Query(p): Query<Params>, | ||
headers: HeaderMap, | ||
) -> Answer { | ||
) -> Result<Json<Value>, Refusal> { | ||
let group = param(&p, "group")?; | ||
let body = json!({ "group": group }); | ||
let (members, _, _) = acting( | ||
&host, | ||
&headers, | ||
&body, | ||
&format!("{NS}.listJoinRequests"), | ||
Some("admit"), | ||
) | ||
.await?; | ||
let lxm = format!("{NS}.listJoinRequests"); | ||
doing(&host, &headers, "GET", &lxm, group, "admit").await?; | ||
let limit = page(&p, 50, 100)?; | ||
let before = match p.get("cursor") { | ||
None => None, | ||
| @@ -148,7 +143,7 @@ pub async fn list_join_requests( | ||
}; | ||
let waiting = host | ||
.store | ||
.asking(&members.group, before, limit) | ||
.asking(group, before, limit) | ||
.map_err(XrpcError::internal)?; | ||
let cursor = waiting | ||
.last() | ||
| @@ -158,24 +153,20 @@ pub async fn list_join_requests( | ||
Ok(Json(json!({"requests": requests, "cursor": cursor}))) | ||
} | ||
| ||
/// Turns down one who asked, for one holding `admit`. They may ask again. | ||
/// Turns down one who asked, for one holding `admit`, or an app signed in | ||
/// as the group with its `rpc:` scope. They may ask again. | ||
pub async fn deny_join_request( | ||
State(host): State<Arc<Host>>, | ||
headers: HeaderMap, | ||
Json(body): Json<Value>, | ||
) -> Answer { | ||
let (members, _, _) = acting( | ||
&host, | ||
&headers, | ||
&body, | ||
&format!("{NS}.denyJoinRequest"), | ||
Some("admit"), | ||
) | ||
.await?; | ||
) -> Result<Json<Value>, Refusal> { | ||
let group = body["group"].as_str().expect("its lexicon requires it"); | ||
let lxm = format!("{NS}.denyJoinRequest"); | ||
doing(&host, &headers, "POST", &lxm, group, "admit").await?; | ||
let did = body["member"].as_str().expect("its lexicon requires it"); | ||
let decided = host.store.decide(&members.group, did); | ||
let decided = host.store.decide(group, did); | ||
if !decided.map_err(XrpcError::internal)? { | ||
return Err(no_request()); | ||
return Err(no_request().into()); | ||
} | ||
Ok(Json(json!({}))) | ||
} | ||
crates/group-host/src/group_members.rs+5−3
crates/group-host/src/group_spaces.rs+57−1
crates/group-host/src/group_tokens.rs+3−0
crates/group-host/src/identity.rs+11−1
crates/group-host/src/lib.rs+6−1
crates/group-host/src/member_writes.rs+1−1
crates/group-host/src/moderation.rs+18−30
crates/group-host/src/oauth.rs+9−0
crates/group-host/src/oauth_scope.rs+28−3
crates/group-host/src/oauth_store.rs+6−4
crates/group-host/src/scopes.rs+11−0
crates/group-host/src/store.rs+102−0
crates/group-host/src/token_writes.rs+4−1
crates/group-host/tests/config.rs+9−0
crates/group-host/tests/firehose.rs+104−0
crates/group-host/tests/identity.rs+16−0
crates/group-host/tests/members.rs+10−5
crates/group-host/tests/oauth.rs+2−0
crates/group-host/tests/oauth_scope.rs+2−0
crates/group-host/tests/provision.rs+4−2
crates/group-host/tests/tokens.rs+300−1
crates/group-host-mcp/src/lib.rs+11−7
crates/group-host-mcp/tests/protocol.rs+7−3
docs/design/signing-in.md+35−9
docs/design/spaces.md+11−2
docs/guides/shots/apps-activity-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-activity.png+0−0
A binary file, not shown.
docs/guides/shots/apps-made-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-made.png+0−0
A binary file, not shown.
docs/guides/shots/apps-revoke-access-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-revoke-access.png+0−0
A binary file, not shown.
docs/guides/shots/apps-revoke-access.txt+1−1
docs/guides/shots/apps-signed-in-dark.png+0−0
A binary file, not shown.
docs/guides/shots/apps-signed-in.png+0−0
A binary file, not shown.