Skip to main content

zlayer_types/api/
tokens.rs

1//! Scoped access-token management DTOs.
2//!
3//! Request/response shapes for the token-management API, which mints
4//! scoped, time-boxed bearer tokens (e.g. for CI runners / least-privilege
5//! automation). The raw JWT is returned exactly once, in
6//! [`CreateTokenResponse::token`].
7
8use serde::{Deserialize, Serialize};
9
10use crate::storage::TokenScope;
11
12/// Request body for minting a scoped access token.
13#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
14pub struct CreateTokenRequest {
15    /// Human-friendly label for the token (e.g. `"ci-runner"`).
16    pub name: String,
17
18    /// Scopes to bake into the token. The caller may only request scopes that
19    /// fall within their own authority.
20    #[serde(default)]
21    pub scopes: Vec<TokenScope>,
22
23    /// Role claims to carry on the token. Only admins may request privileged
24    /// roles (`admin`/`operator`); for least-privilege tokens this is empty.
25    #[serde(default)]
26    pub roles: Vec<String>,
27
28    /// Time-to-live in seconds. Defaults to 1h and is hard-capped at 365d.
29    #[serde(default)]
30    pub ttl_secs: Option<u64>,
31}
32
33/// Response returned once at token creation.
34///
35/// The `token` field is the raw JWT and is never persisted nor returned again.
36#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
37pub struct CreateTokenResponse {
38    /// UUID identifier — also the JWT `jti` claim (the revocation handle).
39    pub id: String,
40
41    /// The raw JWT bearer token. Shown only once; store it securely.
42    pub token: String,
43
44    /// The human-friendly label echoed back from the request.
45    pub name: String,
46
47    /// The scopes baked into the token.
48    pub scopes: Vec<TokenScope>,
49
50    /// When the token expires.
51    #[schema(value_type = String, example = "2026-04-15T12:00:00Z")]
52    pub expires_at: chrono::DateTime<chrono::Utc>,
53}