Skip to main content

ferripfs_config/
api.rs

1// Ported from: kubo/config/api.go
2// Kubo version: v0.39.0
3// Original: https://github.com/ipfs/kubo/blob/v0.39.0/config/api.go
4//
5// Original work: Copyright (c) Protocol Labs, Inc.
6// Port: Copyright (c) 2026 ferripfs contributors
7// SPDX-License-Identifier: MIT OR Apache-2.0
8
9//! API configuration for HTTP RPC API.
10
11use serde::{Deserialize, Serialize};
12use std::collections::HashMap;
13
14/// API configuration section
15#[derive(Debug, Clone, Default, Serialize, Deserialize)]
16#[serde(rename_all = "PascalCase")]
17pub struct Api {
18    /// HTTP headers to include in API responses
19    #[serde(default, rename = "HTTPHeaders")]
20    pub http_headers: HashMap<String, Vec<String>>,
21
22    /// Authorization scopes for API access
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub authorizations: Option<HashMap<String, RpcAuthScope>>,
25}
26
27/// RPC authentication scope
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(rename_all = "PascalCase")]
30pub struct RpcAuthScope {
31    /// Authentication secret (format: "type:value", e.g., "bearer:token" or "basic:user:pass")
32    pub auth_secret: String,
33
34    /// Allowed API paths for this scope
35    pub allowed_paths: Vec<String>,
36}
37
38impl Api {
39    /// Get default HTTP headers for API
40    pub fn default_http_headers() -> HashMap<String, Vec<String>> {
41        let mut headers = HashMap::new();
42        headers.insert(
43            "Access-Control-Allow-Origin".to_string(),
44            vec!["*".to_string()],
45        );
46        headers
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_api_default() {
56        let api = Api::default();
57        assert!(api.http_headers.is_empty());
58        assert!(api.authorizations.is_none());
59    }
60
61    #[test]
62    fn test_rpc_auth_scope() {
63        let scope = RpcAuthScope {
64            auth_secret: "bearer:mytoken".to_string(),
65            allowed_paths: vec!["/api/v0/id".to_string()],
66        };
67        let json = serde_json::to_string(&scope).unwrap();
68        assert!(json.contains("AuthSecret"));
69        assert!(json.contains("AllowedPaths"));
70    }
71}