~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
1271 lines · 48.2 KB Raw History
//! Deactivating, taking down and deleting an account, and what each state
//! does to every method, as `spec/LIFECYCLE.md` says: `deactivateAccount`,
//! `activateAccount`, `checkAccountStatus`, `requestAccountDelete`,
//! `deleteAccount`, and the admin's `updateSubjectStatus` and
//! `getSubjectStatus`.
//!
//! An account's state is its `State`, behind a lock of its own, changed only
//! with its store held for writing, so that the events a change puts on the
//! firehose are numbered in order with its commits'. A change is entries of
//! the service log (`ENTRIES`) in one frame: the state, the sessions a
//! takedown ends, and the change's events, so that it is whole or not at
//! all. A deletion is one entry, `deleted`, holding the deletion's event and
//! the seqs of every event of the account's, which are dropped: once it is
//! on disk the account is gone, and its files are removed after; a server
//! started again removes any left, once it serves
//! (`directory::Accounts::sweep`), and the dropped events' seqs stay holes
//! in the firehose's numbers, as no seq is given out twice.
//!
//! A record taken down is an entry of its account's own log, `takedownRecord`,
//! made under its lock, and stays on its key until a commit deletes it; a
//! blob taken down is the blobs' (`blobs::ENTRIES`), and goes when the blob
//! is no longer held.
//!
//! `gate` and `gate_query` are the one check of an account's state the
//! dispatch consults: `row` says what each method answers of an account
//! deactivated, taken down or deleted, once its token and input are checked.

use crate::email::{self, Purpose};
use crate::events::{self, Firehose, Landing, Reserved};
use crate::oauth::Ends;
use crate::server::{password, password_length};
use crate::sessions::{self, Bearer, Token};
use crate::{
    Account, Auth, Call, Handler, Resp, Service, Shared, Store, XrpcError, durable, err, internal,
    invalid, invalid_token, opt, read, required, write,
};
use base64::Engine;
use pds_store::Wal;
use serde_json::{Map, Value as Json, json};
use std::sync::Arc;

/// The kinds of `accounts.log` entry this module keeps, each naming its account by `did`.
pub(crate) const ENTRIES: &[&str] = &["deactivated", "takedown", "event", "deleted"];

/// Those of them that change an account's `State`, which its row of the
/// service checkpoint keeps (`compact`).
pub(crate) const STATES: &[&str] = &["deactivated", "takedown"];

/// The entry of an account's own log that takes a record down, or lifts that.
pub(crate) const RECORD_TAKEDOWN: &str = "takedownRecord";

const REPO_REF: &str = "com.atproto.admin.defs#repoRef";
const STRONG_REF: &str = "com.atproto.repo.strongRef";
const BLOB_REF: &str = "com.atproto.admin.defs#repoBlobRef";

/// An account's state: active unless it is deactivated or taken down, each
/// set and cleared on its own.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct State {
    pub deactivated: bool,
    /// The takedown's reference, while it is down.
    pub takedown: Option<String>,
    /// Gone: set as it is deleted, for whoever held the account then.
    pub deleted: bool,
}

impl State {
    /// What the methods answer for it: none while it is active.
    pub fn status(&self) -> Option<&'static str> {
        if self.deleted {
            Some("deleted")
        } else if self.takedown.is_some() {
            Some("takendown")
        } else if self.deactivated {
            Some("deactivated")
        } else {
            None
        }
    }

    /// Make the change an entry says, as it is logged and as it is replayed.
    pub fn apply(&mut self, e: &Json) -> Result<(), String> {
        match e["t"].as_str() {
            Some("deactivated") => {
                self.deactivated = e["applied"]
                    .as_bool()
                    .ok_or_else(|| format!("a deactivation with no applied: {e}"))?
            }
            Some("takedown") => {
                self.takedown = match &e["ref"] {
                    Json::Null => None,
                    Json::String(r) => Some(r.clone()),
                    _ => return Err(format!("a takedown whose ref is not a string: {e}")),
                }
            }
            t => return Err(format!("a log entry of no known type: {t:?}")),
        }
        Ok(())
    }

    /// The entries that make this state again, for a compacted log.
    pub fn entries(&self, did: &str) -> Vec<Json> {
        let deactivated = self.deactivated.then(|| deactivated_entry(did, true));
        let takedown = self
            .takedown
            .as_deref()
            .map(|r| takedown_entry(did, Some(r)));
        deactivated.into_iter().chain(takedown).collect()
    }
}

pub(crate) fn deactivated_entry(did: &str, applied: bool) -> Json {
    json!({ "t": "deactivated", "did": did, "applied": applied })
}

fn takedown_entry(did: &str, r: Option<&str>) -> Json {
    json!({ "t": "takedown", "did": did, "ref": r })
}

/// An event of the service log, of the account `did`.
fn event_entry(did: &str, seq: u64, frame: &[u8]) -> Json {
    json!({ "t": "event", "did": did, "seq": seq, "frame": hex::encode(frame) })
}

/// `seqs`, in order, as runs `[first, last]`.
fn runs(ords: &[u64]) -> Vec<[u64; 2]> {
    let mut out: Vec<[u64; 2]> = Vec::new();
    for &o in ords {
        match out.last_mut() {
            Some(r) if r[1] + 1 == o => r[1] = o,
            _ => out.push([o, o]),
        }
    }
    out
}

/// An event `accounts.log` holds, as replayed.
pub(crate) struct LoggedEvent {
    pub did: String,
    pub seq: u64,
    pub frame: Vec<u8>,
    /// Whether it is an account's deletion's, which its `deleted` entry holds.
    deletion: bool,
}

/// What the service log's entries of the kinds in `ENTRIES` that are not
/// one account's state leave: the events the log holds, and the seqs of the
/// events of the accounts deleted, dropped with them.
#[derive(Default)]
pub(crate) struct Replayed {
    pub events: Vec<LoggedEvent>,
    pub gaps: Vec<u64>,
    /// The `deleted` entries, as logged, for a compacted log.
    pub deletions: Vec<Json>,
}

impl Replayed {
    /// Replay an `event` or a `deleted` entry; answers the DID of an
    /// account the entry deletes, which is then no account.
    pub fn replay(&mut self, e: &Json) -> Result<Option<String>, String> {
        let Some(did) = e["did"].as_str() else {
            return Err(format!("an entry with no did: {e}"));
        };
        let event = |e: &Json| -> Result<LoggedEvent, String> {
            let Some(seq) = e["seq"].as_u64().filter(|s| *s > 0) else {
                return Err(format!("an event with no seq: {e}"));
            };
            let frame = e["frame"]
                .as_str()
                .and_then(|f| hex::decode(f).ok())
                .ok_or_else(|| format!("an event with no frame: {e}"))?;
            Ok(LoggedEvent {
                did: did.to_string(),
                seq,
                frame,
                deletion: false,
            })
        };
        match e["t"].as_str() {
            Some("event") => self.events.push(event(e)?),
            Some("deleted") => {
                let gaps = e["gaps"]
                    .as_array()
                    .ok_or_else(|| format!("a deletion with no gaps: {e}"))?;
                for g in gaps {
                    let (Some(first), Some(last)) = (g[0].as_u64(), g[1].as_u64()) else {
                        return Err(format!("a gap that is not two numbers: {e}"));
                    };
                    if first == 0 || last < first {
                        return Err(format!("a gap out of order: {e}"));
                    }
                    self.gaps.extend(first..=last);
                }
                self.events.retain(|x| x.did != did);
                self.events.push(LoggedEvent {
                    deletion: true,
                    ..event(e)?
                });
                self.deletions.push(e.clone());
                return Ok(Some(did.to_string()));
            }
            t => return Err(format!("a log entry of no known type: {t:?}")),
        }
        Ok(None)
    }

    /// The entries that make what was replayed again, for a checkpoint:
    /// the deletions whose events `keep` keeps, then the events it keeps
    /// but theirs, so that no event logged after a deletion is dropped by
    /// it.
    pub fn entries(&self, keep: impl Fn(u64, &[u8]) -> bool) -> Vec<Json> {
        let mut out: Vec<Json> = Vec::new();
        let kept = |e: &LoggedEvent| keep(e.seq, &e.frame);
        let deleted: std::collections::HashSet<&str> = self
            .events
            .iter()
            .filter(|e| e.deletion && kept(e))
            .map(|e| e.did.as_str())
            .collect();
        out.extend(
            self.deletions
                .iter()
                .filter(|d| d["did"].as_str().is_some_and(|did| deleted.contains(did)))
                .map(|d| {
                    let mut d = d.clone();
                    if let Some(m) = d.as_object_mut() {
                        m.remove("ord");
                    }
                    d
                }),
        );
        out.extend(
            self.events
                .iter()
                .filter(|e| !e.deletion && kept(e))
                .map(|e| event_entry(&e.did, e.seq, &e.frame)),
        );
        out
    }

    /// The greatest seq of the events replayed.
    pub fn last_seq(&self) -> u64 {
        self.events.iter().map(|e| e.seq).max().unwrap_or(0)
    }
}

pub(crate) fn handler(nsid: &str) -> Option<Handler> {
    Some(match nsid {
        "com.atproto.server.deactivateAccount" => {
            Handler::Authed(sessions::deactivating_token, |s, t, _| {
                Box::pin(deactivate_account(s, t))
            })
        }
        "com.atproto.server.activateAccount" => {
            Handler::Authed(sessions::full_access_token, |s, t, _| {
                Box::pin(activate_account(s, t))
            })
        }
        "com.atproto.server.checkAccountStatus" => {
            Handler::Authed(sessions::access_token, |s, t, _| {
                Box::pin(check_account_status(s, t))
            })
        }
        "com.atproto.server.requestAccountDelete" => {
            Handler::Authed(sessions::full_access_token, |s, t, _| {
                Box::pin(request_account_delete(s, t))
            })
        }
        "com.atproto.server.deleteAccount" => {
            Handler::Procedure(|s, b| Box::pin(delete_account(s, b)))
        }
        "com.atproto.admin.updateSubjectStatus" => {
            Handler::Authed(admin, |s, _, b| Box::pin(update_subject_status(s, b)))
        }
        "com.atproto.admin.getSubjectStatus" => {
            Handler::Authed(admin, |s, _, p| Box::pin(get_subject_status(s, p)))
        }
        _ => return None,
    })
}

// ---- The gate -----------------------------------------------------------------

/// What a method answers of an account in a state.
#[derive(Clone, Copy)]
enum Answer {
    /// As for an active account: the method itself answers.
    Pass,
    Refuse(u16, &'static str),
}

/// Which account a method's state check is of.
#[derive(Clone, Copy)]
enum Of {
    /// The token's.
    Token,
    /// The one a parameter names; with `readers`, the account itself and the
    /// admin read it as if it were active, a `com.atproto.takendown` token
    /// too if `readers` is `Some(true)`.
    Param(&'static str, Option<bool>),
}

/// What a method answers of an account deactivated, taken down, and deleted.
struct Row {
    of: Of,
    deactivated: Answer,
    takendown: Answer,
    deleted: Answer,
}

const PASS: Answer = Answer::Pass;
const TAKEDOWN: Answer = Answer::Refuse(401, "AccountTakedown");
const NOT_FOUND: Answer = Answer::Refuse(403, "AccountNotFound");
const INVALID: Answer = Answer::Refuse(400, "InvalidRequest");

/// The table of `spec/LIFECYCLE.md`'s "What each state does to every
/// method", for the methods a state refuses. A method not here answers as
/// for an active account, or says its state itself (`getRepoStatus`,
/// `listRepos`, `resolveHandle`, `createSession`, `deleteAccount`, and the
/// answers of `getSession`, `refreshSession` and `checkAccountStatus`), and
/// a deleted account is no account at all to a method that looks it up.
/// `com.atproto.repo.getRecord` of an account that is not active is
/// answered as of a repository the server does not hold (`proxy::get_record`).
fn row(nsid: &str) -> Option<Row> {
    let own = |takendown, deleted| Row {
        of: Of::Token,
        deactivated: PASS,
        takendown,
        deleted,
    };
    let read = |name, readers, deactivated, takendown| Row {
        of: Of::Param(name, readers),
        deactivated,
        takendown,
        deleted: PASS,
    };
    let repo_deactivated = Answer::Refuse(400, "RepoDeactivated");
    let repo_takendown = Answer::Refuse(400, "RepoTakendown");
    Some(match nsid {
        "com.atproto.server.getSession" => own(INVALID, INVALID),
        "com.atproto.server.refreshSession" => own(TAKEDOWN, INVALID),
        "com.atproto.server.createAppPassword"
        | "com.atproto.repo.uploadBlob"
        | "com.atproto.identity.updateHandle"
        | "com.atproto.server.requestAccountDelete"
        | "com.atproto.server.requestEmailConfirmation"
        | "com.atproto.server.confirmEmail"
        | "com.atproto.server.requestEmailUpdate"
        | "com.atproto.server.updateEmail"
        | "app.bsky.actor.putPreferences"
        // The account's own route of no lexicon (`spec/PASSKEYS.md`).
        | "_passkeyAdd" => own(TAKEDOWN, NOT_FOUND),
        "com.atproto.server.getAccountInviteCodes" => Row {
            of: Of::Token,
            deactivated: Answer::Refuse(400, "NotFound"),
            takendown: TAKEDOWN,
            deleted: NOT_FOUND,
        },
        "app.bsky.actor.getPreferences" => own(PASS, Answer::Refuse(400, "NotFound")),
        "com.atproto.identity.getRecommendedDidCredentials" => {
            own(PASS, Answer::Refuse(500, "InternalServerError"))
        }
        "com.atproto.server.checkAccountStatus" => own(PASS, Answer::Refuse(400, "NotFound")),
        "com.atproto.server.deactivateAccount" => own(PASS, INVALID),
        // spec/MIGRATION.md. `importRepo` checks its account's state with
        // its token (`migration`); `listMissingBlobs`, `submitPlcOperation`
        // and `signPlcOperation` each answer a deleted account where they
        // find it gone.
        "com.atproto.repo.importRepo" => own(TAKEDOWN, NOT_FOUND),
        "com.atproto.identity.requestPlcOperationSignature" => own(PASS, INVALID),
        // `activateAccount` checks its account's state itself, after its DID
        // document (`activate`, `activate_gone`).
        // A write's is the account of its `repo` (`write_repo`).
        "com.atproto.repo.createRecord"
        | "com.atproto.repo.putRecord"
        | "com.atproto.repo.deleteRecord"
        | "com.atproto.repo.applyWrites" => Row {
            of: Of::Token,
            deactivated: Answer::Refuse(401, "AccountDeactivated"),
            takendown: TAKEDOWN,
            deleted: NOT_FOUND,
        },
        "com.atproto.repo.describeRepo" => read("repo", None, repo_deactivated, repo_takendown),
        "com.atproto.repo.listRecords" => read("repo", None, INVALID, INVALID),
        "com.atproto.sync.getRepo" | "com.atproto.sync.getBlob" | "com.atproto.sync.listBlobs" => {
            read("did", Some(true), repo_deactivated, repo_takendown)
        }
        "com.atproto.sync.getLatestCommit"
        | "com.atproto.sync.getBlocks"
        | "com.atproto.sync.getRecord" => {
            read("did", Some(false), repo_deactivated, repo_takendown)
        }
        _ => return None,
    })
}

fn refuse(a: Answer, did: &str) -> Result<(), XrpcError> {
    match a {
        Answer::Pass => Ok(()),
        Answer::Refuse(status, name) => {
            let message = match name {
                "RepoDeactivated" => format!("Repo has been deactivated: {did}"),
                "RepoTakendown" => format!("Repo has been takendown: {did}"),
                "AccountDeactivated" => "Account is deactivated".into(),
                "AccountTakedown" => "Account has been taken down".into(),
                "AccountNotFound" => "Account not found".into(),
                "NotFound" => "Account not found".into(),
                "InternalServerError" => "Internal Server Error".into(),
                _ => "Account is not active".into(),
            };
            err(status, name, message)
        }
    }
}

/// What a row answers of the account `did`, as its state is now: of none,
/// it is deleted.
fn answer_of(s: &Service, r: &Row, did: &str) -> Result<(), XrpcError> {
    let Some(a) = s.account(did) else {
        return refuse(r.deleted, did);
    };
    let state = read(&a.state);
    let which = if state.deleted {
        r.deleted
    } else if state.takedown.is_some() {
        r.takendown
    } else if state.deactivated {
        r.deactivated
    } else {
        Answer::Pass
    };
    refuse(which, &a.did)
}

/// The state check of a call of `nsid` whose token, input and rate limits
/// are checked: of the account `did`, the token's, or a write's `repo`'s.
pub(crate) fn gate(s: &Service, nsid: &str, did: &str) -> Result<(), XrpcError> {
    match row(nsid) {
        Some(r) => answer_of(s, &r, did),
        None => Ok(()),
    }
}

/// Whether a call of `nsid` has its state checked with its token, before its
/// input and rate limits: the account's own calls a taken-down account is
/// refused with `401 AccountTakedown` and a deleted one with `403
/// AccountNotFound`, as the reference refuses them with the token, and
/// `getPreferences`, which has no input. A query among them checks its
/// token, and then its state, itself (`getAccountInviteCodes`).
pub(crate) fn with_token(nsid: &str) -> bool {
    matches!(
        nsid,
        "com.atproto.server.createAppPassword"
            | "com.atproto.repo.uploadBlob"
            | "com.atproto.identity.updateHandle"
            | "com.atproto.server.requestAccountDelete"
            | "com.atproto.server.requestEmailConfirmation"
            | "com.atproto.server.confirmEmail"
            | "com.atproto.server.requestEmailUpdate"
            | "com.atproto.server.updateEmail"
            | "com.atproto.server.getAccountInviteCodes"
            | "app.bsky.actor.getPreferences"
            | "app.bsky.actor.putPreferences"
    )
}

/// Whether an error is the refusal of a call's account's state made with
/// its token, which costs the call nothing of the rate limits.
pub(crate) fn refused_with_token(nsid: &str, e: &XrpcError) -> bool {
    with_token(nsid) && matches!(e.name, "AccountTakedown" | "AccountNotFound")
}

/// The state check of a query once its parameters are checked: of its
/// token's account, if its token is one, which the query checks itself if
/// not; or of the account a parameter names, if any has it, which the query
/// answers itself if none does. A read the account itself and the admin
/// make as of an active account checks its `Authorization` first, as
/// `viewer` says.
pub(crate) fn gate_query(s: &Service, call: &Call, p: &Map<String, Json>) -> Result<(), XrpcError> {
    let Some(r) = row(call.nsid) else {
        return Ok(());
    };
    match r.of {
        Of::Token if with_token(call.nsid) => Ok(()),
        Of::Token => match s.auth(call.bearer) {
            Auth::Account(did) => answer_of(s, &r, &did),
            _ => Ok(()),
        },
        Of::Param(name, readers) => {
            let viewer = match readers {
                Some(takendown) => viewer(s, call.bearer, takendown)?,
                None => Viewer::Anyone,
            };
            let Some(a) = opt(p, name).and_then(|id| s.account(id)) else {
                return Ok(());
            };
            match viewer {
                Viewer::Admin => Ok(()),
                Viewer::Account(did) if did == a.did => Ok(()),
                _ => answer_of(s, &r, &a.did),
            }
        }
    }
}

/// Who reads a repository, as its `Authorization` says.
enum Viewer {
    Anyone,
    Account(String),
    Admin,
}

/// The reader of the sync reads: none; an account, by an access token, or a
/// `com.atproto.takendown` one if `takendown`; or the admin, by its `Basic`.
/// A token the server would refuse is `400 InvalidToken`, one past its
/// `exp` `400 ExpiredToken`, and a `Basic` that is not the admin's `401`.
fn viewer(s: &Service, b: Bearer, takendown: bool) -> Result<Viewer, XrpcError> {
    match b {
        Bearer::None => Ok(Viewer::Anyone),
        Bearer::Token(_) if takendown => {
            sessions::takendown_ok_token(s, b).map(|t| Viewer::Account(t.did))
        }
        Bearer::Token(_) => sessions::access_token(s, b).map(|t| Viewer::Account(t.did)),
        Bearer::Basic(_) => crate::invites::admin(s, b).map(|_| Viewer::Admin),
        Bearer::OAuth(g) => Ok(Viewer::Account(g.did.clone())),
        Bearer::DPoP(_) | Bearer::Malformed => invalid_token(),
    }
}

/// `403 AccountNotFound` if the account was deleted: checked with its store
/// held, by what writes to it, as it may have been deleted while they waited.
pub(crate) fn not_deleted(a: &Account) -> Result<(), XrpcError> {
    if read(&a.state).deleted {
        account_not_found()
    } else {
        Ok(())
    }
}

/// What a method of a deleted account's answers when it gets that far.
pub(crate) fn account_not_found<T>() -> Result<T, XrpcError> {
    err(403, "AccountNotFound", "Account not found")
}

/// Whether an account is active, as `resolveHandle`, `listRepos` and
/// `getRepoStatus` answer it.
pub(crate) fn status(a: &Account) -> Option<&'static str> {
    read(&a.state).status()
}

// ---- Changing an account's state ------------------------------------------------

/// A change of an account's state, and the events it puts on the firehose.
enum Change {
    /// Deactivate it: `#account` with its status. With an OAuth token, every
    /// OAuth session of the account ends too, and its app passwords are
    /// deleted, as the reference does.
    Deactivate { by_oauth: bool },
    /// Activate it: `#account`, `#identity` and `#sync`.
    Activate,
    /// Take it down with this reference, or lift that, either way ending its
    /// sessions: `#account` with its status.
    Takedown(Option<String>),
}

/// Make a change to an account's state, with its store held, logged in one
/// frame of `accounts.log` with its events and what it ends of OAuth's (a
/// takedown, applied or lifted, its sessions and the requests approved for
/// it; a deactivation those requests, and with an OAuth token its sessions
/// and the clients it approved too): answers the frame, and the events to
/// land once it is on disk. OAuth's lock is taken before the state's
/// (`oauth::OAuth::end_for`).
fn change(s: &Service, a: &Account, c: Change) -> Result<(u64, Landing), XrpcError> {
    let st = write(&a.store);
    not_deleted(a)?;
    let ends = match c {
        Change::Takedown(_) => Ends {
            sessions: true,
            requests: true,
            ..Ends::default()
        },
        Change::Deactivate { by_oauth } => Ends {
            requests: true,
            sessions: by_oauth,
            clients: by_oauth,
            ..Ends::default()
        },
        Change::Activate => Ends::default(),
    };
    s.oauth.end_for(&a.did, ends, |ended| {
        let mut state = write(&a.state);
        let mut next = state.clone();
        let mut entries = match &c {
            Change::Deactivate { .. } => vec![deactivated_entry(&a.did, true)],
            Change::Activate => vec![deactivated_entry(&a.did, false)],
            Change::Takedown(r) => vec![takedown_entry(&a.did, r.as_deref())],
        };
        for e in &entries {
            next.apply(e)
                .unwrap_or_else(|why| panic!("a change made under its account's lock: {why}"));
        }
        let n = if matches!(c, Change::Activate) { 3 } else { 1 };
        let r = s
            .events
            .reserve_kept(&a.did, n)
            .or_else(email::not_logged)?;
        let frames = match c {
            Change::Activate => activation(a, &r, next.status(), &st),
            _ => vec![events::account_event(
                &a.did,
                r.seq,
                &r.time(),
                next.status(),
            )],
        };
        let seq = r.seq;
        entries.extend(
            frames
                .iter()
                .enumerate()
                .map(|(i, f)| event_entry(&a.did, seq + i as u64, f)),
        );
        entries.extend(ended);
        let logged = match c {
            Change::Takedown(_) => s.sessions.end_all(&s.log, &a.did, entries),
            Change::Deactivate { by_oauth: true } => {
                s.sessions.drop_app_passwords(&s.log, &a.did, entries)
            }
            _ => s.log.append(&entries),
        };
        let n = match logged {
            Ok(n) => n,
            Err(e) => {
                s.events.fail(seq, frames.len());
                return email::not_logged(e);
            }
        };
        *state = next;
        Ok((n, r.landing(frames.into_iter().map(Into::into).collect())))
    })
}

/// The events an activation puts on the firehose, numbered as `r` says:
/// `#account` with the status it has, `#identity` with its handle, and
/// `#sync` of its latest commit, as its store has it.
fn activation(a: &Account, r: &Reserved, status: Option<&str>, st: &Store) -> Vec<Vec<u8>> {
    let time = r.time();
    vec![
        events::account_event(&a.did, r.seq, &time, status),
        events::identity_event(&a.did, &a.handle(), r.seq + 1, &time),
        events::sync_event(&a.did, r.seq + 2, &time, st.repo.head_bytes(), &st.repo.rev),
    ]
}

/// Put on the firehose, for an active account a write of which was found
/// whole after a power cut with nothing kept of its event, the events an
/// activation puts there, so that those who read the firehose learn what
/// its repository holds (`spec/STORE.md`, "The firehose after a start"):
/// logged in one frame of the service log, `log`, as `change` logs an
/// activation's, its store held so that they are in order with its other
/// events, and nothing changed. Answers the frame and the events to land
/// once it and their entry are on disk; none for an account not active,
/// which its activation announces when it comes.
pub(crate) fn announce_found(
    events: &Firehose,
    log: &Wal,
    a: &Account,
) -> std::io::Result<Option<(u64, Landing)>> {
    let st = write(&a.store);
    if read(&a.state).status().is_some() {
        return Ok(None);
    }
    let r = events.reserve_kept(&a.did, 3)?;
    let frames = activation(a, &r, None, &st);
    let entries: Vec<Json> = frames
        .iter()
        .enumerate()
        .map(|(i, f)| event_entry(&a.did, r.seq + i as u64, f))
        .collect();
    match log.append(&entries) {
        Ok(n) => Ok(Some((
            n,
            r.landing(frames.into_iter().map(Into::into).collect()),
        ))),
        Err(e) => {
            events.fail(r.seq, frames.len());
            Err(e)
        }
    }
}

/// Wait for the events' entry in the index and a frame of `accounts.log`
/// to be on disk, in that order, then land the events.
async fn settle(s: &Shared, n: u64, landing: Landing) -> Result<(), XrpcError> {
    let entry = match landing.entry() {
        Some((index, k)) => durable(index, k).await,
        None => Ok(()),
    };
    let synced = durable(s.log.clone(), n).await;
    s.events.land(landing);
    entry.and(synced)
}

/// Make a change and wait for it.
async fn make(s: &Shared, a: &Account, c: Change) -> Result<(), XrpcError> {
    let (n, landing) = change(s, a, c)?;
    settle(s, n, landing).await
}

/// Put `#account` on the firehose for the account `did` with no change: its
/// status now, or `deleted` if no account here has it.
async fn announce(s: &Shared, did: &str) -> Result<(), XrpcError> {
    let account = s.account(did);
    let (n, landing) = {
        // Held, so that the event is in order with its account's others.
        let st = account.as_ref().map(|a| write(&a.store));
        let status = match &account {
            Some(a) => read(&a.state).status(),
            None => Some("deleted"),
        };
        let r = s.events.reserve_kept(did, 1).or_else(email::not_logged)?;
        let frame = events::account_event(did, r.seq, &r.time(), status);
        let n = match s.log.append(&[event_entry(did, r.seq, &frame)]) {
            Ok(n) => n,
            Err(e) => {
                s.events.fail(r.seq, 1);
                return email::not_logged(e);
            }
        };
        drop(st);
        (n, r.landing(vec![frame.into()]))
    };
    settle(s, n, landing).await
}

/// Put one event of the account `a`'s on the firehose, made by `frame` of
/// its seq and time, logged in the service log as `announce`'s is: the
/// `#identity` of a PLC operation submitted for it (`spec/MIGRATION.md`).
pub(crate) async fn tell(
    s: &Shared,
    a: &Account,
    frame: impl FnOnce(u64, &str) -> Vec<u8>,
) -> Result<(), XrpcError> {
    let (n, landing) = {
        // Held, so that the event is in order with its account's others.
        let st = write(&a.store);
        let r = s
            .events
            .reserve_kept(&a.did, 1)
            .or_else(email::not_logged)?;
        let frame = frame(r.seq, &r.time());
        let n = match s.log.append(&[event_entry(&a.did, r.seq, &frame)]) {
            Ok(n) => n,
            Err(e) => {
                s.events.fail(r.seq, 1);
                return email::not_logged(e);
            }
        };
        drop(st);
        (n, r.landing(vec![frame.into()]))
    };
    settle(s, n, landing).await
}

/// The PLC directory's current data for `did`: none if it does not know the
/// DID, `502 UpstreamFailure` if it cannot be read.
async fn did_data(s: &Service, did: &str) -> Result<Option<Json>, XrpcError> {
    let url = format!("{}/{did}/data", s.config.plc_url.trim_end_matches('/'));
    crate::plc::document(&s.http, &url).await.or_else(|why| {
        crate::ops::failed_for(
            crate::ops::Level::Error,
            "reading a DID's data failed",
            did,
            why,
        );
        err(
            502,
            "UpstreamFailure",
            "The PLC directory could not be reached",
        )
    })
}

/// Whether a DID's data names this server: its rotation keys include the
/// server's and its PDS is this one, looked at first, then its signing key
/// is `signing`, if the account has one still.
fn names_server(s: &Service, data: &Json, signing: Option<&str>) -> bool {
    let rotation = crate::plc::did_key(s.config.rotation_key.verifying_key());
    data["rotationKeys"]
        .as_array()
        .is_some_and(|keys| keys.iter().any(|k| *k == json!(rotation)))
        && data["services"]["atproto_pds"]["endpoint"] == json!(s.config.public_url)
        && signing.is_none_or(|k| data["verificationMethods"]["atproto"] == json!(k))
}

/// Whether the PLC directory's current data for the account's DID names
/// this server: its PDS, its signing key, and the server's rotation key. A
/// `did:web`'s document is read from its host instead, its errors as
/// `identity::web_doc_now` gives them, and names this server by its
/// endpoint and signing key alone (`spec/MIGRATION.md`, "A `did:web` account").
async fn valid_did(s: &Service, a: &Account) -> Result<bool, XrpcError> {
    let signing = *read(&a.store).repo.verifying_key();
    if crate::identity::is_web(&a.did) {
        let doc = crate::identity::web_doc_now(s, a).await?;
        return Ok(crate::identity::web_names_server(s, &doc, Some(&signing)));
    }
    let Some(data) = did_data(s, &a.did).await? else {
        return Ok(false);
    };
    let signing = crate::plc::did_key(&signing);
    Ok(names_server(s, &data, Some(&signing)))
}

fn not_this_server<T>() -> Result<T, XrpcError> {
    invalid("Requested DID does not match current DID document")
}

/// Activate an account, by its owner or the admin: refused if its DID
/// document does not name this server, then if it is taken down.
async fn activate(s: &Shared, a: &Account) -> Result<(), XrpcError> {
    if !valid_did(s, a).await? {
        return not_this_server();
    }
    if read(&a.state).takedown.is_some() {
        return err(400, "AccountNotFound", "Account has been taken down");
    }
    make(s, a, Change::Activate).await
}

/// Activate a DID no account here has, deleted or never made, by its
/// deleted account's token or the admin: its DID document is read as an
/// account's, but there is no signing key to find in it, and one that
/// otherwise names this server is `500`, as the reference answers it.
async fn activate_gone(s: &Service, did: &str) -> Result<(), XrpcError> {
    let names = if crate::identity::is_web(did) {
        let doc = crate::proxy::read_did(s, did).await?;
        crate::identity::web_names_server(s, &doc, None)
    } else {
        did_data(s, did)
            .await?
            .is_some_and(|data| names_server(s, &data, None))
    };
    if names {
        return internal();
    }
    not_this_server()
}

/// The token's account, which the gate found.
fn own(s: &Service, t: &Token) -> Result<Arc<Account>, XrpcError> {
    s.account(&t.did).map_or_else(internal, Ok)
}

async fn deactivate_account(s: Shared, t: Token) -> Result<Resp, XrpcError> {
    let a = own(&s, &t)?;
    make(&s, &a, Change::Deactivate { by_oauth: t.oauth }).await?;
    Ok(Resp::Nothing)
}

async fn activate_account(s: Shared, t: Token) -> Result<Resp, XrpcError> {
    match s.account(&t.did) {
        Some(a) => activate(&s, &a).await?,
        None => activate_gone(&s, &t.did).await?,
    }
    Ok(Resp::Nothing)
}

async fn check_account_status(s: Shared, t: Token) -> Result<Resp, XrpcError> {
    let a = own(&s, &t)?;
    // A directory that cannot be read is answered as one that does not name this server.
    let valid = valid_did(&s, &a).await.unwrap_or(false);
    let (mut out, wal, n) = {
        let st = read(&a.store);
        let (expected, imported) = st.blobs.counts(s.limits.clock.now(), s.config.blob_ttl);
        let out = json!({
            "activated": status(&a).is_none(),
            "repoCommit": st.repo.head_string(),
            "repoRev": st.repo.rev,
            "repoBlocks": st.repo.block_count(),
            "indexedRecords": st.repo.record_count(),
            "privateStateValues": 0,
            "expectedBlobs": expected,
            "importedBlobs": imported,
        });
        (out, st.wal.clone(), st.wal.written())
    };
    out["validDid"] = json!(valid);
    // What it read of the account's state is on disk once `accounts.log` is.
    durable(wal, n).await?;
    durable(s.log.clone(), s.log.written()).await?;
    Ok(Resp::Json(out))
}

async fn request_account_delete(s: Shared, t: Token) -> Result<Resp, XrpcError> {
    let a = own(&s, &t)?;
    let out = {
        let mut m = email::lock(&a.mail);
        email::make(&s, &a.did, &mut m, Purpose::DeleteAccount)?
    };
    email::deliver(&s, out).await?;
    Ok(Resp::Nothing)
}

/// The longest password `deleteAccount` takes, in UTF-16 code units.
const MAX_PASSWORD: usize = 512;

async fn delete_account(s: Shared, b: Map<String, Json>) -> Result<Resp, XrpcError> {
    let refuse = || err(401, "AuthenticationRequired", "Invalid did or password");
    let (did, pw, token) = (
        required(&b, "did")?,
        required(&b, "password")?.to_string(),
        required(&b, "token")?,
    );
    if password_length(&pw) > MAX_PASSWORD {
        return refuse();
    }
    let Some(a) = s.account(did) else {
        return invalid("Could not find account");
    };
    // Hashed as for any account, one without a password too, with no lock held.
    let stored = email::lock(&a.mail).password.clone();
    let hash = stored.clone();
    let right = s
        .limits
        .hashes
        .run(move || password::verify_or_none(&pw, hash.as_deref()))
        .await
        .unwrap_or(false);
    if !right {
        return refuse();
    }
    let now = s.sessions.now();
    let (n, landing, objects) = {
        let m = email::lock(&a.mail);
        // A password reset meanwhile makes it wrong.
        if m.password != stored {
            return refuse();
        }
        m.check(Purpose::DeleteAccount, token, now)?;
        remove(&s, &a, &m)?
    };
    settle(&s, n, landing).await?;
    // Its objects in a bucket go once its deletion is on disk.
    if let Some(bucket) = &s.ctx.bucket {
        bucket.doom(&a.did, objects);
    }
    // Its files go once its deletion is on disk; a server started again
    // removes any left. A DID may be made again: the journal forgets its
    // directory, durably, before it goes, so that a start never writes what
    // it held of these files into another account's.
    let dir = s.dir.join("repos").join(&a.did);
    let journal = s.ctx.files.journal().cloned();
    let removed = tokio::task::spawn_blocking(move || {
        if let Some(j) = &journal {
            j.forget(&dir)
                .map_err(|e| format!("{}: {e}", dir.display()))?;
        }
        match std::fs::remove_dir_all(&dir) {
            Err(e) if e.kind() != std::io::ErrorKind::NotFound => {
                Err(format!("{}: {e}", dir.display()))
            }
            _ => Ok(()),
        }
    });
    if let Ok(Err(why)) = removed.await {
        crate::ops::failed("removing a deleted account failed", why);
    }
    Ok(Resp::Nothing)
}

/// Delete an account, with its mail held: logged, in one entry with its
/// deletion's event and the places of its events, dropped from the
/// firehose; then it is no account, its handle and email free, its sessions,
/// app passwords and tokens gone. With blobs in a bucket, the objects it may
/// keep are named in the same frame, to delete (`blobstore`). Answers the
/// frame, the event to land, and those objects.
fn remove(
    s: &Service,
    a: &Account,
    m: &email::Mail,
) -> Result<(u64, Landing, Vec<String>), XrpcError> {
    // Its events the index holds are known before any lock is taken.
    s.events.find_old();
    let st = write(&a.store);
    not_deleted(a)?;
    // OAuth's lock, taken before the state's, is held throughout: no OAuth
    // change is logged meanwhile, and once logged, OAuth forgets the account.
    let out = s.oauth.end_for(&a.did, Ends { account: true, ..Ends::default() }, |_| {
        let mut state = write(&a.state);
        let mut d = write(&s.directory);
        // Its events: those its store holds, and the service log's.
        let mut seqs: Vec<u64> = st
            .event_seqs()
            .into_iter()
            .map(|(seq, _)| seq)
            .chain(s.events.seqs_of(&a.did))
            .collect();
        seqs.sort_unstable();
        seqs.dedup();
        let r = s
            .events
            .reserve_kept(&a.did, 1)
            .or_else(email::not_logged)?;
        let seq = r.seq;
        let frame = events::account_event(&a.did, seq, &r.time(), Some("deleted"));
        let entry = json!({ "t": "deleted", "did": a.did, "gaps": runs(&seqs), "seq": seq, "frame": hex::encode(&frame) });
        let objects = match &s.ctx.bucket {
            Some(_) => st.blobs.kept_list(),
            None => Vec::new(),
        };
        let mut entries = vec![entry];
        if !objects.is_empty() {
            entries.push(crate::blobstore::doomed_entry(&a.did, &objects));
        }
        let (n, at) = match s.log.append_at(&entries) {
            Ok(logged) => logged,
            Err(e) => {
                s.events.fail(seq, 1);
                return email::not_logged(e);
            }
        };
        s.events.drop_events(&seqs, |seq| st.event_frame(seq));
        s.ctx.eager.forget(&a.did);
        state.deleted = true;
        d.remove(a, m.email.as_deref(), at.generation);
        let landing = r.landing(vec![frame.into()]);
        Ok((n, landing, objects))
    })?;
    s.sessions.forget(&a.did);
    s.resets.forget(&a.did);
    s.prefs.forget(&a.did);
    Ok(out)
}

// ---- The admin ------------------------------------------------------------------

/// The admin's `Basic` auth, as the admin's methods of this page check it: a
/// `Bearer` token is taken for a moderation service's JWT, which the server
/// does not take, and a header that is not two words is `InvalidToken`.
fn admin(s: &Service, b: Bearer) -> Result<Token, XrpcError> {
    match b {
        Bearer::Token(jwt) => {
            let typ = jwt
                .split('.')
                .next()
                .and_then(|h| {
                    base64::engine::general_purpose::URL_SAFE_NO_PAD
                        .decode(h)
                        .ok()
                })
                .and_then(|h| serde_json::from_slice::<Json>(&h).ok())
                .and_then(|h| h["typ"].as_str().map(str::to_string));
            match typ.as_deref() {
                Some("at+jwt" | "refresh+jwt") => err(401, "BadJwtType", "Invalid jwt type"),
                _ => err(401, "BadJwt", "Jwt could not be verified"),
            }
        }
        Bearer::Malformed => invalid_token(),
        _ => crate::invites::admin(s, b),
    }
}

/// A takedown's or a deactivation's `applied`, if it is given.
fn applied(b: &Map<String, Json>, name: &str) -> Option<bool> {
    b.get(name).and_then(|a| a["applied"].as_bool())
}

async fn update_subject_status(s: Shared, b: Map<String, Json>) -> Result<Resp, XrpcError> {
    let subject = b.get("subject").cloned().unwrap_or_default();
    let ty = subject["$type"].as_str().unwrap_or_default();
    let (takedown, deactivated) = (applied(&b, "takedown"), applied(&b, "deactivated"));
    if takedown == Some(true) && deactivated == Some(false) {
        return invalid("Cannot both take down and activate an account");
    }
    if takedown.is_some() && ![REPO_REF, STRONG_REF, BLOB_REF].contains(&ty) {
        return invalid("Invalid subject");
    }
    let text = |k: &str| subject[k].as_str().unwrap_or_default().to_string();
    if let Some(on) = takedown {
        let r = on.then(|| {
            b["takedown"]["ref"]
                .as_str()
                .map_or_else(events::timestamp, str::to_string)
        });
        match ty {
            REPO_REF => match s.account(&text("did")) {
                Some(a) => make(&s, &a, Change::Takedown(r)).await?,
                None => announce(&s, &text("did")).await?,
            },
            STRONG_REF => take_record_down(&s, &text("uri"), r).await?,
            _ => take_blob_down(&s, &text("did"), &text("cid"), r).await?,
        }
    }
    if let (Some(on), REPO_REF) = (deactivated, ty) {
        let did = text("did");
        match s.account(&did) {
            Some(a) if on => make(&s, &a, Change::Deactivate { by_oauth: false }).await?,
            Some(a) => activate(&s, &a).await?,
            None if on => return invalid("Account not found"),
            None => activate_gone(&s, &did).await?,
        }
    }
    if takedown.is_none() && b.get("deactivated").is_none() && ty == REPO_REF {
        announce(&s, &text("did")).await?;
    }
    let mut out = json!({ "subject": subject });
    if let Some(t) = b.get("takedown") {
        out["takedown"] = t.clone();
    }
    Ok(Resp::Json(out))
}

/// An `at://` URI's authority, and the path `collection/rkey` of the record
/// it names, if it names one: none of its authority alone, or of a
/// collection and no key. Any other URI is none.
fn record_uri(uri: &str) -> Option<(&str, Option<String>)> {
    let mut parts = uri.strip_prefix("at://")?.splitn(3, '/');
    let authority = parts.next().filter(|a| !a.is_empty())?;
    let path = match (
        parts.next().filter(|c| !c.is_empty()),
        parts.next().filter(|k| !k.is_empty()),
    ) {
        (Some(collection), Some(rkey)) if !rkey.contains('/') => {
            Some(format!("{collection}/{rkey}"))
        }
        (_, Some(_)) => return None,
        _ => None,
    };
    Some((authority, path))
}

/// Take down the record the repository now has at the URI's key, or lift
/// that: a key it does not hold changes nothing, nor a URI of an account
/// here that names no record. One whose authority is a handle, or a DID no
/// account here has, is `500`, as the reference answers it.
async fn take_record_down(s: &Shared, uri: &str, r: Option<String>) -> Result<(), XrpcError> {
    let Some((did, path)) = record_uri(uri).filter(|(a, _)| a.starts_with("did:")) else {
        return internal();
    };
    let Some(a) = s.account(did) else {
        return internal();
    };
    let Some(path) = path else { return Ok(()) };
    let (wal, n) = {
        let mut st = write(&a.store);
        not_deleted(&a)?;
        let changes = match &r {
            Some(_) => st.holds(&path),
            None => st.takedowns.contains_key(&path),
        };
        if changes {
            if let Err(e) = st
                .wal
                .append(&[json!({ "t": RECORD_TAKEDOWN, "path": path, "ref": r })])
            {
                crate::ops::failed_for(
                    crate::ops::Level::Error,
                    "logging a takedown failed",
                    did,
                    e,
                );
                return internal();
            }
            match r {
                Some(r) => st.takedowns.insert(path, r),
                None => st.takedowns.remove(&path),
            };
        }
        (st.wal.clone(), st.wal.written())
    };
    durable(wal, n).await
}

/// Take down a blob the account holds, or lift that: one it does not hold
/// changes nothing.
async fn take_blob_down(
    s: &Shared,
    did: &str,
    cid: &str,
    r: Option<String>,
) -> Result<(), XrpcError> {
    let Some(a) = s.account(did) else {
        return internal();
    };
    let (wal, n) = {
        let mut st = write(&a.store);
        not_deleted(&a)?;
        st.blobs.time(s.limits.clock.now(), s.config.blob_ttl);
        if let Some(e) = st.blobs.takedown_entry(cid, r.as_deref()) {
            if let Err(e) = st.wal.append(std::slice::from_ref(&e)) {
                crate::ops::failed_for(
                    crate::ops::Level::Error,
                    "logging a takedown failed",
                    did,
                    e,
                );
                return internal();
            }
            st.blobs
                .replay(&e)
                .unwrap_or_else(|why| panic!("a takedown made under its account's lock: {why}"));
        }
        (st.wal.clone(), st.wal.written())
    };
    durable(wal, n).await
}

/// A subject's takedown, as `getSubjectStatus` answers it.
fn takedown_json(r: Option<&str>) -> Json {
    match r {
        Some(r) => json!({ "applied": true, "ref": r }),
        None => json!({ "applied": false }),
    }
}

async fn get_subject_status(s: Shared, p: Map<String, Json>) -> Result<Resp, XrpcError> {
    let not_found = || err(400, "NotFound", "Subject not found");
    let (did, uri, blob) = (opt(&p, "did"), opt(&p, "uri"), opt(&p, "blob"));
    let (out, wal, n) = if let Some(cid) = blob {
        let Some(did) = did else {
            return invalid("Must provide a did to request blob state");
        };
        let Some(a) = s.account(did) else {
            return not_found();
        };
        let mut st = write(&a.store);
        st.blobs.time(s.limits.clock.now(), s.config.blob_ttl);
        let Some(r) = st.blobs.held_takedown(cid) else {
            return not_found();
        };
        let out = json!({ "subject": { "$type": BLOB_REF, "did": a.did, "cid": cid }, "takedown": takedown_json(r) });
        (out, st.wal.clone(), st.wal.written())
    } else if let Some(uri) = uri {
        let Some((did, path)) = record_uri(uri) else {
            return not_found();
        };
        // As the reference answers a handle, whether or not an account has it.
        if !did.starts_with("did:") {
            return internal();
        }
        let Some(a) = s.account(did) else {
            return not_found();
        };
        let Some(path) = path else { return not_found() };
        let st = read(&a.store);
        let Some(rec) = st.record(&path) else {
            return not_found();
        };
        let cid = pds_repo::data::cid_string(&rec.cid);
        let out = json!({ "subject": { "$type": STRONG_REF, "uri": uri, "cid": cid }, "takedown": takedown_json(st.takedowns.get(&path).map(String::as_str)) });
        (out, st.wal.clone(), st.wal.written())
    } else if let Some(did) = did {
        let Some(a) = s.account(did) else {
            return not_found();
        };
        let state = read(&a.state).clone();
        let out = json!({
            "subject": { "$type": REPO_REF, "did": a.did },
            "takedown": takedown_json(state.takedown.as_deref()),
            "deactivated": { "applied": state.deactivated },
        });
        (out, s.log.clone(), s.log.written())
    } else {
        return invalid("No provided subject");
    };
    durable(wal, n).await?;
    Ok(Resp::Json(out))
}

#[cfg(test)]
mod tests;