ironflow-api 2.52.6

REST API for ironflow run management and observability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! `POST /api/v1/api-keys` -- Create a new API key.

use axum::Json;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use chrono::{DateTime, Utc};
use ironflow_auth::extractor::AuthenticatedUser;
use ironflow_auth::password;
use ironflow_store::entities::{ApiKeyScope, NewApiKey};
use rand::Rng;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::error::ApiError;
use crate::response::ok;
use crate::state::AppState;
use ironflow_auth::extractor::{API_KEY_PREFIX, API_KEY_SUFFIX_LEN};

/// Maximum length of an API key name, in characters (matches the
/// `iam.api_keys.name` VARCHAR(255) column).
const MAX_API_KEY_NAME_LEN: usize = 255;

/// Request body for creating an API key.
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Deserialize)]
pub struct CreateApiKeyRequest {
    /// Human-readable name for this key.
    pub name: String,
    /// Scopes to grant.
    pub scopes: Vec<ApiKeyScope>,
    /// Optional expiration date (ISO 8601).
    pub expires_at: Option<DateTime<Utc>>,
    /// Optional per-key rate limit override (requests per minute).
    /// When set, the server uses this value instead of the global rate
    /// limit for requests authenticated with this key.
    pub rate_limit_override: Option<u32>,
}

/// Response returned when creating an API key.
/// The raw key is only shown once.
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
#[derive(Debug, Serialize)]
pub struct CreateApiKeyResponse {
    /// API key ID.
    pub id: Uuid,
    /// The full raw API key (only returned at creation time).
    pub key: String,
    /// First characters for identification.
    pub key_prefix: String,
    /// Key name.
    pub name: String,
    /// Granted scopes.
    pub scopes: Vec<ApiKeyScope>,
    /// Expiration date.
    pub expires_at: Option<DateTime<Utc>>,
    /// Per-key rate limit override (requests per minute), if set.
    pub rate_limit_override: Option<u32>,
    /// Creation date.
    pub created_at: DateTime<Utc>,
}

/// Create a new API key for the authenticated user.
///
/// # Errors
///
/// - 400 if the name is empty or longer than 255 characters (after trimming), or scopes are invalid
#[cfg_attr(
    feature = "openapi",
    utoipa::path(
        post,
        path = "/api/v1/api-keys",
        tags = ["api-keys"],
        request_body(content = CreateApiKeyRequest, description = "API key configuration"),
        responses(
            (status = 201, description = "API key created successfully", body = CreateApiKeyResponse),
            (status = 400, description = "Invalid input"),
            (status = 401, description = "Unauthorized"),
            (status = 403, description = "Forbidden (member assigning forbidden scopes or a rate_limit_override)")
        ),
        security(("Bearer" = []))
    )
)]
pub async fn create_api_key(
    user: AuthenticatedUser,
    State(state): State<AppState>,
    Json(req): Json<CreateApiKeyRequest>,
) -> Result<impl IntoResponse, ApiError> {
    let name = validate_name(&req.name)?.to_string();

    if req.scopes.is_empty() {
        return Err(ApiError::BadRequest(
            "at least one scope is required".to_string(),
        ));
    }

    if !user.is_admin && !ApiKeyScope::all_allowed_for_member(&req.scopes) {
        return Err(ApiError::Forbidden);
    }

    // Only admins may lift their own key above the server-wide limit.
    if !user.is_admin && req.rate_limit_override.is_some() {
        return Err(ApiError::Forbidden);
    }

    if let Some(override_val) = req.rate_limit_override
        && override_val > 10_000
    {
        return Err(ApiError::BadRequest(
            "rate_limit_override must be between 0 and 10000".to_string(),
        ));
    }

    let raw_key = generate_api_key();
    let key_prefix = raw_key[..API_KEY_PREFIX.len() + API_KEY_SUFFIX_LEN].to_string();
    let key_hash =
        password::hash(&raw_key).map_err(|e| ApiError::Internal(format!("hashing: {e}")))?;

    let api_key = state
        .store
        .create_api_key(NewApiKey {
            user_id: user.user_id,
            name,
            key_hash,
            key_prefix: key_prefix.clone(),
            scopes: req.scopes,
            expires_at: req.expires_at,
            rate_limit_override: req.rate_limit_override,
        })
        .await
        .map_err(ApiError::from)?;

    let response = CreateApiKeyResponse {
        id: api_key.id,
        key: raw_key,
        key_prefix,
        name: api_key.name,
        scopes: api_key.scopes,
        expires_at: api_key.expires_at,
        rate_limit_override: api_key.rate_limit_override,
        created_at: api_key.created_at,
    };

    Ok((StatusCode::CREATED, ok(response)))
}

/// Trim `name` and check it is non-empty and within the column bound.
///
/// The trimmed value is the one to store, so the stored name always fits.
/// Characters are counted, not bytes, as Postgres does for VARCHAR(n).
fn validate_name(name: &str) -> Result<&str, ApiError> {
    let name = name.trim();
    if name.is_empty() {
        return Err(ApiError::BadRequest("name must not be empty".to_string()));
    }
    if name.chars().count() > MAX_API_KEY_NAME_LEN {
        return Err(ApiError::BadRequest(format!(
            "name must be at most {MAX_API_KEY_NAME_LEN} characters"
        )));
    }
    Ok(name)
}

/// Generate a random API key with the `irfl_` prefix.
fn generate_api_key() -> String {
    let mut rng = rand::rng();
    let random_bytes: Vec<u8> = (0..32).map(|_| rng.random::<u8>()).collect();
    let encoded = hex::encode(&random_bytes);
    format!("{API_KEY_PREFIX}{encoded}")
}

#[cfg(test)]
mod tests {
    use axum::Router;
    use axum::body::Body;
    use axum::http::Request;
    use axum::routing::post;
    use http_body_util::BodyExt;
    use serde_json::{Value as JsonValue, from_slice};
    use tower::ServiceExt;

    use super::*;
    use crate::routes::test_helpers::{create_user_auth_header, test_state};

    use std::sync::Arc;

    use ironflow_auth::jwt::JwtConfig;
    use ironflow_core::providers::claude::ClaudeCodeProvider;
    use ironflow_engine::engine::Engine;
    use ironflow_engine::notify::Event;
    use ironflow_store::entities::NewUser;
    use ironflow_store::memory::InMemoryStore;
    use ironflow_store::store::Store;
    use tokio::sync::broadcast;

    async fn admin_state() -> (AppState, AuthenticatedUser) {
        let store: Arc<dyn Store> = Arc::new(InMemoryStore::new());
        let provider = Arc::new(ClaudeCodeProvider::new());
        let engine = Arc::new(Engine::new(store.clone(), provider));
        let jwt_config = Arc::new(JwtConfig {
            secret: "test-secret-for-api-keys".to_string(),
            access_token_ttl_secs: 900,
            refresh_token_ttl_secs: 604800,
            cookie_domain: None,
            cookie_secure: false,
        });
        let (event_sender, _) = broadcast::channel::<Event>(1);
        let state = AppState::new(
            store.clone(),
            engine,
            jwt_config,
            "test-worker-token".to_string(),
            event_sender,
        );

        let user = store
            .create_user(NewUser {
                email: "admin@example.com".to_string(),
                username: "admin".to_string(),
                password_hash: password::hash("password123").expect("hash"),
                is_admin: Some(true),
            })
            .await
            .expect("create user");
        let auth = AuthenticatedUser {
            user_id: user.id,
            username: user.username,
            is_admin: true,
        };
        (state, auth)
    }

    fn request(name: String) -> CreateApiKeyRequest {
        CreateApiKeyRequest {
            name,
            scopes: vec![ApiKeyScope::RunsRead],
            expires_at: None,
            rate_limit_override: None,
        }
    }

    #[test]
    fn validate_name_rejects_empty_and_whitespace() {
        assert!(matches!(validate_name(""), Err(ApiError::BadRequest(_))));
        assert!(matches!(validate_name("   "), Err(ApiError::BadRequest(_))));
    }

    #[test]
    fn validate_name_accepts_exactly_255_chars() {
        let name = "a".repeat(255);
        assert_eq!(validate_name(&name).expect("valid"), name);
    }

    #[test]
    fn validate_name_rejects_256_chars() {
        let name = "a".repeat(256);
        match validate_name(&name) {
            Err(ApiError::BadRequest(msg)) => assert!(msg.contains("255")),
            _ => panic!("expected BadRequest"),
        }
    }

    #[test]
    fn validate_name_trims_before_counting() {
        let padded = format!("  {}  ", "a".repeat(255));
        let name = validate_name(&padded).expect("valid");
        assert_eq!(name.len(), 255);
    }

    #[test]
    fn validate_name_counts_chars_not_bytes() {
        let ok = "é".repeat(255);
        assert!(validate_name(&ok).is_ok());
        let too_long = "é".repeat(256);
        assert!(matches!(
            validate_name(&too_long),
            Err(ApiError::BadRequest(_))
        ));
    }

    #[tokio::test]
    async fn create_api_key_name_longer_than_255_returns_400() {
        let (state, auth) = admin_state().await;
        let user_id = auth.user_id;
        let store = state.store.clone();

        let result = create_api_key(auth, State(state), Json(request("a".repeat(300)))).await;
        assert!(matches!(result, Err(ApiError::BadRequest(_))));

        let keys = store
            .list_api_keys_by_user(user_id)
            .await
            .expect("list keys");
        assert!(keys.is_empty());
    }

    #[tokio::test]
    async fn create_api_key_name_of_255_chars_returns_201() {
        let (state, auth) = admin_state().await;

        let response = create_api_key(auth, State(state), Json(request("a".repeat(255))))
            .await
            .expect("created")
            .into_response();
        assert_eq!(response.status(), StatusCode::CREATED);
    }

    #[tokio::test]
    async fn create_api_key_stores_trimmed_name() {
        let (state, auth) = admin_state().await;
        let user_id = auth.user_id;
        let store = state.store.clone();

        let response = create_api_key(auth, State(state), Json(request("  key  ".to_string())))
            .await
            .expect("created")
            .into_response();
        assert_eq!(response.status(), StatusCode::CREATED);

        let keys = store
            .list_api_keys_by_user(user_id)
            .await
            .expect("list keys");
        assert_eq!(keys.len(), 1);
        assert_eq!(keys[0].name, "key");
    }

    #[test]
    fn generate_api_key_has_correct_prefix() {
        let key = generate_api_key();
        assert!(key.starts_with(API_KEY_PREFIX));
    }

    #[test]
    fn generate_api_key_is_long_enough_for_prefix_extraction() {
        let key = generate_api_key();
        let expected_prefix_len = API_KEY_PREFIX.len() + API_KEY_SUFFIX_LEN;
        assert!(
            key.len() >= expected_prefix_len,
            "key length {} is shorter than expected prefix length {}",
            key.len(),
            expected_prefix_len
        );

        let prefix = &key[..expected_prefix_len];
        assert_eq!(prefix.len(), 13);
        assert!(prefix.starts_with(API_KEY_PREFIX));
    }

    #[test]
    fn generate_api_key_prefix_fits_varchar_16() {
        let key = generate_api_key();
        let prefix = &key[..API_KEY_PREFIX.len() + API_KEY_SUFFIX_LEN];
        assert!(
            prefix.len() <= 16,
            "prefix length {} exceeds VARCHAR(16)",
            prefix.len()
        );
    }

    #[test]
    fn generate_api_key_produces_unique_keys() {
        let key1 = generate_api_key();
        let key2 = generate_api_key();
        assert_ne!(key1, key2);
    }

    async fn post_create(is_admin: bool, body: &str) -> (StatusCode, JsonValue) {
        let state = test_state();
        let username = if is_admin { "admin" } else { "member" };
        let auth = create_user_auth_header(&state, username, is_admin).await;
        let app = Router::new()
            .route("/", post(create_api_key))
            .with_state(state);
        let req = Request::builder()
            .method("POST")
            .uri("/")
            .header("authorization", auth)
            .header("content-type", "application/json")
            .body(Body::from(body.to_string()))
            .unwrap();
        let resp = app.oneshot(req).await.unwrap();
        let status = resp.status();
        let bytes = resp.into_body().collect().await.unwrap().to_bytes();
        let json = if bytes.is_empty() {
            JsonValue::Null
        } else {
            from_slice(&bytes).unwrap()
        };
        (status, json)
    }

    #[tokio::test]
    async fn member_setting_rate_limit_override_is_forbidden() {
        let body = r#"{"name":"k","scopes":["runs_read"],"rate_limit_override":0}"#;
        let (status, _) = post_create(false, body).await;
        assert_eq!(status, StatusCode::FORBIDDEN);
    }

    #[tokio::test]
    async fn admin_can_set_rate_limit_override() {
        let body = r#"{"name":"k","scopes":["runs_read"],"rate_limit_override":0}"#;
        let (status, json) = post_create(true, body).await;
        assert_eq!(status, StatusCode::CREATED);
        assert_eq!(json["data"]["rate_limit_override"], 0);
    }

    #[tokio::test]
    async fn member_without_rate_limit_override_can_create_key() {
        let body = r#"{"name":"k","scopes":["runs_read"]}"#;
        let (status, json) = post_create(false, body).await;
        assert_eq!(status, StatusCode::CREATED);
        assert!(json["data"]["rate_limit_override"].is_null());
    }
}