Something went wrong. Try again.
PDS software with bells & whistles you didn’t even know you needed. will move this to its own account when ready.
Something went wrong. Try again.
4.4 kB · 141 lines
Rust
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142use crate::api::error::ApiError;use crate::auth::{BearerAuth, extract_auth_token_from_header, validate_token_with_dpop};use crate::state::AppState;use axum::{ Json, extract::State, http::HeaderMap, response::{IntoResponse, Response},};use cid::Cid;use jacquard_repo::storage::BlockStore;use serde::{Deserialize, Serialize};use std::str::FromStr;
#[derive(Serialize)]#[serde(rename_all = "camelCase")]pub struct CheckSignupQueueOutput { pub activated: bool, #[serde(skip_serializing_if = "Option::is_none")] pub place_in_queue: Option<i64>, #[serde(skip_serializing_if = "Option::is_none")] pub estimated_time_ms: Option<i64>,}
pub async fn check_signup_queue(State(state): State<AppState>, headers: HeaderMap) -> Response { if let Some(extracted) = extract_auth_token_from_header(headers.get("Authorization").and_then(|h| h.to_str().ok())) { let dpop_proof = headers.get("DPoP").and_then(|h| h.to_str().ok()); if let Ok(user) = validate_token_with_dpop( &state.db, &extracted.token, extracted.is_dpop, dpop_proof, "GET", "/", false, false, ) .await && user.is_oauth { return ApiError::Forbidden.into_response(); } } Json(CheckSignupQueueOutput { activated: true, place_in_queue: None, estimated_time_ms: None, }) .into_response()}
#[derive(Deserialize)]#[serde(rename_all = "camelCase")]pub struct DereferenceScopeInput { pub scope: String,}
#[derive(Serialize)]#[serde(rename_all = "camelCase")]pub struct DereferenceScopeOutput { pub scope: String,}
pub async fn dereference_scope( State(state): State<AppState>, auth: BearerAuth, Json(input): Json<DereferenceScopeInput>,) -> Response { let _ = auth;
let scope_parts: Vec<&str> = input.scope.split_whitespace().collect(); let mut resolved_scopes: Vec<String> = Vec::new();
for part in scope_parts { if let Some(cid_str) = part.strip_prefix("ref:") { let cache_key = format!("scope_ref:{}", cid_str); if let Some(cached) = state.cache.get(&cache_key).await { for s in cached.split_whitespace() { if !resolved_scopes.contains(&s.to_string()) { resolved_scopes.push(s.to_string()); } } continue; }
let cid = match Cid::from_str(cid_str) { Ok(c) => c, Err(_) => { tracing::warn!("Invalid CID in scope ref: {}", cid_str); continue; } };
let block_bytes = match state.block_store.get(&cid).await { Ok(Some(b)) => b, Ok(None) => { tracing::warn!("Scope ref block not found: {}", cid_str); continue; } Err(e) => { tracing::warn!("Error fetching scope ref block {}: {:?}", cid_str, e); continue; } };
let scope_record: serde_json::Value = match serde_ipld_dagcbor::from_slice(&block_bytes) { Ok(v) => v, Err(e) => { tracing::warn!("Failed to decode scope ref block {}: {:?}", cid_str, e); continue; } };
if let Some(scope_value) = scope_record.get("scope").and_then(|v| v.as_str()) { let _ = state .cache .set( &cache_key, scope_value, std::time::Duration::from_secs(3600), ) .await; for s in scope_value.split_whitespace() { if !resolved_scopes.contains(&s.to_string()) { resolved_scopes.push(s.to_string()); } } } } else if !resolved_scopes.contains(&part.to_string()) { resolved_scopes.push(part.to_string()); } }
Json(DereferenceScopeOutput { scope: resolved_scopes.join(" "), }) .into_response()}