ryuk-cli 0.1.0

The `ryuk` command: scaffold a Ryuk app (axum, SeaORM, Inertia/Svelte), generate controllers, models, jobs and mails, run migrations and the dev server.
use super::owner;
use crate::AppState;
use axum::{
    Json,
    extract::{Path, State},
    http::StatusCode,
};
use ryuk::{
    api::{ApiAuth, ApiError},
    oauth_server::Grant,
};
use serde::Serialize;
use utoipa::ToSchema;

/// An authorized app (an OAuth grant). `id` is what `DELETE /sessions/{id}` takes.
#[derive(Serialize, ToSchema)]
pub struct SessionOut {
    id: String,
    client_id: String,
    client_name: String,
    scopes: Vec<String>,
    created_at: i64,
    last_used_at: Option<i64>,
    expires_at: i64,
}

impl From<Grant> for SessionOut {
    fn from(g: Grant) -> Self {
        SessionOut {
            id: g.id,
            client_id: g.client_id,
            client_name: g.client_name,
            scopes: g.scopes,
            created_at: g.created_at,
            last_used_at: g.last_used_at,
            expires_at: g.expires_at,
        }
    }
}

/// The caller's active OAuth grants, newest first.
#[utoipa::path(get, path = "/sessions", security(("api_key" = []), ("oauth2" = ["account"])),
    responses((status = 200, body = Vec<SessionOut>), (status = 401, description = "Missing or invalid credential"), (status = 403, description = "Missing scope account, or no user")))]
pub async fn index(State(s): State<AppState>, ApiAuth(p): ApiAuth) -> Result<Json<Vec<SessionOut>>, ApiError> {
    p.require("account")?;
    let grants = s.oauth.list_grants(owner(&p)?).await?;
    Ok(Json(grants.into_iter().map(Into::into).collect()))
}

/// Revoke one grant (its access and refresh tokens stop working at once); someone else's id is 404.
#[utoipa::path(delete, path = "/sessions/{id}", params(("id" = String, Path, description = "Grant id")),
    security(("api_key" = []), ("oauth2" = ["account"])),
    responses((status = 204, description = "Revoked"), (status = 404, description = "Not found")))]
pub async fn destroy(State(s): State<AppState>, ApiAuth(p): ApiAuth, Path(id): Path<String>) -> Result<StatusCode, ApiError> {
    p.require("account")?;
    if s.oauth.revoke_grant(owner(&p)?, &id).await? { Ok(StatusCode::NO_CONTENT) } else { Err(ApiError::not_found()) }
}