Skip to main content

iii_sdk/
structs.rs

1use std::collections::HashMap;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::protocol::TriggerAction;
8
9/// Input passed to the RBAC auth function during WebSocket upgrade.
10///
11/// Contains the HTTP headers, query parameters, and client IP from the
12/// connecting worker's upgrade request.
13#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
14pub struct AuthInput {
15    /// HTTP headers from the WebSocket upgrade request.
16    pub headers: HashMap<String, String>,
17    /// Query parameters from the upgrade URL. Each key maps to a vec of values
18    /// to support repeated keys (e.g. `?a=1&a=2`).
19    pub query_params: HashMap<String, Vec<String>>,
20    /// IP address of the connecting client.
21    pub ip_address: String,
22}
23
24/// Return value from the RBAC auth function.
25///
26/// Controls which functions the authenticated worker can invoke and what
27/// context is forwarded to the middleware.
28#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
29pub struct AuthResult {
30    /// Additional function IDs to allow beyond the `expose_functions` config.
31    #[serde(default)]
32    pub allowed_functions: Vec<String>,
33    /// Function IDs to deny even if they match `expose_functions`.
34    /// Takes precedence over allowed.
35    #[serde(default)]
36    pub forbidden_functions: Vec<String>,
37    /// Trigger type IDs the worker may register triggers for.
38    /// When `None`, all types are allowed.
39    #[serde(default, skip_serializing_if = "Option::is_none")]
40    pub allowed_trigger_types: Option<Vec<String>>,
41    /// Whether the worker may register new trigger types.
42    #[serde(default = "default_true")]
43    pub allow_trigger_type_registration: bool,
44    /// Whether the worker may register new functions. Defaults to `true`.
45    #[serde(default = "default_true")]
46    pub allow_function_registration: bool,
47    /// Arbitrary context forwarded to the middleware function on every
48    /// invocation.
49    #[serde(default = "default_context")]
50    pub context: Value,
51    /// Optional prefix applied to all function IDs registered by this worker.
52    #[serde(default, skip_serializing_if = "Option::is_none")]
53    pub function_registration_prefix: Option<String>,
54}
55
56fn default_true() -> bool {
57    true
58}
59
60fn default_context() -> Value {
61    Value::Object(Default::default())
62}
63
64/// Input passed to the RBAC middleware function on every function invocation
65/// through the RBAC port.
66///
67/// The middleware can inspect, modify, or reject the call before it reaches
68/// the target function.
69#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
70pub struct MiddlewareFunctionInput {
71    /// ID of the function being invoked.
72    pub function_id: String,
73    /// Payload sent by the caller.
74    pub payload: Value,
75    /// Routing action, if any.
76    #[serde(default, skip_serializing_if = "Option::is_none")]
77    pub action: Option<TriggerAction>,
78    /// Auth context returned by the auth function for this session.
79    pub context: Value,
80}
81
82/// Input passed to the `on_trigger_type_registration_function_id` hook
83/// when a worker attempts to register a new trigger type through the RBAC port.
84/// Return an [`OnTriggerTypeRegistrationResult`] with the (possibly mapped)
85/// fields, or return an error to deny the registration.
86#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
87pub struct OnTriggerTypeRegistrationInput {
88    /// ID of the trigger type being registered.
89    pub trigger_type_id: String,
90    /// Human-readable description of the trigger type.
91    pub description: String,
92    /// Auth context from `AuthResult.context` for this session.
93    pub context: Value,
94}
95
96/// Result returned from the `on_trigger_type_registration_function_id` hook.
97/// Omitted fields keep the original value from the registration request.
98#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
99pub struct OnTriggerTypeRegistrationResult {
100    /// Mapped trigger type ID.
101    #[serde(default, skip_serializing_if = "Option::is_none")]
102    pub trigger_type_id: Option<String>,
103    /// Mapped description.
104    #[serde(default, skip_serializing_if = "Option::is_none")]
105    pub description: Option<String>,
106}
107
108/// Input passed to the `on_trigger_registration_function_id` hook
109/// when a worker attempts to register a trigger through the RBAC port.
110/// Return an [`OnTriggerRegistrationResult`] with the (possibly mapped)
111/// fields, or return an error to deny the registration.
112#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
113pub struct OnTriggerRegistrationInput {
114    /// ID of the trigger being registered.
115    pub trigger_id: String,
116    /// Trigger type identifier.
117    pub trigger_type: String,
118    /// ID of the function this trigger is bound to.
119    pub function_id: String,
120    /// Trigger-specific configuration.
121    pub config: Value,
122    /// Arbitrary metadata attached to the trigger.
123    #[serde(skip_serializing_if = "Option::is_none")]
124    pub metadata: Option<Value>,
125    /// Auth context from `AuthResult.context` for this session.
126    pub context: Value,
127}
128
129/// Result returned from the `on_trigger_registration_function_id` hook.
130/// Omitted fields keep the original value from the registration request.
131#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
132pub struct OnTriggerRegistrationResult {
133    /// Mapped trigger ID.
134    #[serde(default, skip_serializing_if = "Option::is_none")]
135    pub trigger_id: Option<String>,
136    /// Mapped trigger type.
137    #[serde(default, skip_serializing_if = "Option::is_none")]
138    pub trigger_type: Option<String>,
139    /// Mapped function ID.
140    #[serde(default, skip_serializing_if = "Option::is_none")]
141    pub function_id: Option<String>,
142    /// Mapped trigger configuration.
143    #[serde(default, skip_serializing_if = "Option::is_none")]
144    pub config: Option<Value>,
145}
146
147/// Input passed to the `on_function_registration_function_id` hook
148/// when a worker attempts to register a function through the RBAC port.
149/// Return an [`OnFunctionRegistrationResult`] with the (possibly mapped)
150/// fields, or return an error to deny the registration.
151#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
152pub struct OnFunctionRegistrationInput {
153    /// ID of the function being registered.
154    pub function_id: String,
155    /// Human-readable description of the function.
156    #[serde(default, skip_serializing_if = "Option::is_none")]
157    pub description: Option<String>,
158    /// Arbitrary metadata attached to the function.
159    #[serde(default, skip_serializing_if = "Option::is_none")]
160    pub metadata: Option<Value>,
161    /// Auth context from `AuthResult.context` for this session.
162    pub context: Value,
163}
164
165/// Result returned from the `on_function_registration_function_id` hook.
166/// Omitted fields keep the original value from the registration request.
167#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
168pub struct OnFunctionRegistrationResult {
169    /// Mapped function ID.
170    #[serde(default, skip_serializing_if = "Option::is_none")]
171    pub function_id: Option<String>,
172    /// Mapped description.
173    #[serde(default, skip_serializing_if = "Option::is_none")]
174    pub description: Option<String>,
175    /// Mapped metadata.
176    #[serde(default, skip_serializing_if = "Option::is_none")]
177    pub metadata: Option<Value>,
178}