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
// SPDX-FileCopyrightText: 2025-2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT
//! OpenAPI security scheme generation.
//!
//! This module generates security scheme definitions for the OpenAPI
//! specification. Security schemes define how API endpoints are protected
//! and how clients should authenticate.
//!
//! # Supported Security Types
//!
//! The macro supports three authentication mechanisms:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ Security Schemes │
//! ├─────────────────────────────────────────────────────────────────┤
//! │ │
//! │ 1. Bearer Token (JWT) │
//! │ ├─► Scheme name: "bearerAuth" │
//! │ ├─► Type: HTTP Bearer │
//! │ ├─► Header: Authorization: Bearer <token> │
//! │ └─► Format: JWT │
//! │ │
//! │ 2. Cookie Authentication │
//! │ ├─► Scheme name: "cookieAuth" │
//! │ ├─► Type: API Key (Cookie) │
//! │ ├─► Cookie name: "token" │
//! │ └─► Note: HTTP-only for XSS protection │
//! │ │
//! │ 3. API Key │
//! │ ├─► Scheme name: "apiKey" │
//! │ ├─► Type: API Key (Header) │
//! │ ├─► Header: X-API-Key: <key> │
//! │ └─► Use case: Service-to-service auth │
//! │ │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Configuration
//!
//! Security type is set via the `security` attribute:
//!
//! ```rust,ignore
//! #[entity(
//! table = "users",
//! api(
//! security = "bearer", // or "cookie", "api_key"
//! handlers
//! )
//! )]
//! pub struct User { ... }
//! ```
//!
//! # Generated Code Examples
//!
//! ## Bearer Token
//!
//! ```rust,ignore
//! components.add_security_scheme("bearerAuth",
//! security::SecurityScheme::Http(
//! security::HttpBuilder::new()
//! .scheme(security::HttpAuthScheme::Bearer)
//! .bearer_format("JWT")
//! .description(Some("JWT token in Authorization header"))
//! .build()
//! )
//! );
//! ```
//!
//! ## Cookie Authentication
//!
//! ```rust,ignore
//! components.add_security_scheme("cookieAuth",
//! security::SecurityScheme::ApiKey(
//! security::ApiKey::Cookie(
//! security::ApiKeyValue::with_description(
//! "token",
//! "JWT token stored in HTTP-only cookie"
//! )
//! )
//! )
//! );
//! ```
//!
//! ## API Key
//!
//! ```rust,ignore
//! components.add_security_scheme("apiKey",
//! security::SecurityScheme::ApiKey(
//! security::ApiKey::Header(
//! security::ApiKeyValue::with_description(
//! "X-API-Key",
//! "API key for service-to-service authentication"
//! )
//! )
//! )
//! );
//! ```
//!
//! # Swagger UI Integration
//!
//! When a security scheme is configured, Swagger UI displays:
//!
//! ```text
//! ┌──────────────────────────────────────────────┐
//! │ 🔒 Authorize │
//! │ ────────────────────────────────────────────│
//! │ bearerAuth (http, Bearer) │
//! │ JWT token in Authorization header │
//! │ │
//! │ Value: [________________] [Authorize] │
//! └──────────────────────────────────────────────┘
//! ```
//!
//! # Security Requirements
//!
//! Once a security scheme is defined, it can be applied to operations:
//!
//! ```rust,ignore
//! #[utoipa::path(
//! get,
//! security(("bearerAuth" = []))
//! )]
//! ```
//!
//! This adds a lock icon in Swagger UI indicating the endpoint requires
//! authentication.
use TokenStream;
use quote;
/// Generates security scheme code for the `Modify` implementation.
///
/// This function produces code that registers a security scheme in the
/// OpenAPI components section. The scheme defines how the API authenticates
/// requests and is displayed in Swagger UI's "Authorize" dialog.
///
/// # Arguments
///
/// * `security` - Optional security type string: `"bearer"`, `"cookie"`, or
/// `"api_key"`
///
/// # Returns
///
/// A `TokenStream` containing code to add the security scheme to components.
/// Returns empty stream if security is `None` or unrecognized.
///
/// # Security Type Mapping
///
/// | Input | Scheme Name | Type |
/// |-------|-------------|------|
/// | `"bearer"` | `bearerAuth` | HTTP Bearer with JWT format |
/// | `"cookie"` | `cookieAuth` | API Key in cookie named "token" |
/// | `"api_key"` | `apiKey` | API Key in "X-API-Key" header |
///
/// # Usage
///
/// Called within `generate_modifier()` to add security schemes:
///
/// ```rust,ignore
/// let security_code = generate_security_code(api_config.security.as_deref());
///
/// quote! {
/// fn modify(&self, openapi: &mut OpenApi) {
/// #security_code // Adds scheme to components
/// }
/// }
/// ```
/// Returns the OpenAPI security scheme name for a given security type.
///
/// This function maps user-facing security type names to their corresponding
/// OpenAPI security scheme identifiers. The scheme name is used both when
/// defining the security scheme and when applying it to operations.
///
/// # Arguments
///
/// * `security` - The security type: `"bearer"`, `"cookie"`, or `"api_key"`
///
/// # Returns
///
/// The canonical OpenAPI scheme name used throughout the specification.
///
/// # Mapping
///
/// | Input | Output | Description |
/// |-------|--------|-------------|
/// | `"bearer"` | `"bearerAuth"` | JWT in Authorization header |
/// | `"cookie"` | `"cookieAuth"` | JWT in HTTP-only cookie |
/// | `"api_key"` | `"apiKey"` | Key in X-API-Key header |
/// | other | `"cookieAuth"` | Default fallback |
///
/// # Usage
///
/// The scheme name is used in two places:
///
/// 1. **Defining the scheme** (in components/securitySchemes): ```rust,ignore
/// components.add_security_scheme("bearerAuth", scheme); ```
///
/// 2. **Applying to operations** (in path operations): ```rust,ignore
/// security::SecurityRequirement::new::<_, _, &str>("bearerAuth", []) ```
///
/// # Consistency
///
/// The same scheme name must be used in both places. This function ensures
/// consistency by providing a single source of truth for the mapping.