roas-asyncapi 0.2.0

Rust implementation of the AsyncAPI Specification v2.6 / v3.0 / v3.1 — parse and validate
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! AsyncAPI v2.6 `Security Scheme`, `Security Requirement`, and OAuth 2
//! flow objects.
//!
//! Per [Security Scheme Object](https://www.asyncapi.com/docs/reference/specification/v2.6.0#securitySchemeObject).
//!
//! Two shapes differ from v3. A *requirement* here is the OpenAPI-style
//! map of scheme name → scopes ([`SecurityRequirement`]), where v3
//! carries a list of schemes inline. And an OAuth flow spells its scope
//! map `scopes`, which v3 renamed to `availableScopes`.

use crate::validation::{Context, ValidateWithContext};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// A map of security scheme name → the scopes it requires.
///
/// The scope list must be empty for every scheme type except `oauth2`
/// and `openIdConnect`; the document validator checks that, since only
/// it can resolve a name to its scheme.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
#[serde(transparent)]
pub struct SecurityRequirement(pub BTreeMap<String, Vec<String>>);

impl SecurityRequirement {
    /// The scopes required for `name`, if the requirement names it.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&Vec<String>> {
        self.0.get(name)
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl ValidateWithContext for SecurityRequirement {
    fn validate_with_context(&self, ctx: &mut Context) {
        for (name, scopes) in &self.0 {
            // `uniqueItems` on the scope array.
            for (i, scope) in scopes.iter().enumerate() {
                if scopes[..i].contains(scope) {
                    ctx.error_field(name, format!("duplicate scope `{scope}`"));
                }
            }
        }
    }
}

/// The `type` discriminator of a security scheme.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq, Default)]
pub enum SecuritySchemeType {
    #[default]
    #[serde(rename = "userPassword")]
    UserPassword,
    #[serde(rename = "apiKey")]
    ApiKey,
    #[serde(rename = "X509")]
    X509,
    #[serde(rename = "symmetricEncryption")]
    SymmetricEncryption,
    #[serde(rename = "asymmetricEncryption")]
    AsymmetricEncryption,
    #[serde(rename = "httpApiKey")]
    HttpApiKey,
    #[serde(rename = "http")]
    Http,
    #[serde(rename = "oauth2")]
    OAuth2,
    #[serde(rename = "openIdConnect")]
    OpenIdConnect,
    #[serde(rename = "plain")]
    Plain,
    #[serde(rename = "scramSha256")]
    ScramSha256,
    #[serde(rename = "scramSha512")]
    ScramSha512,
    #[serde(rename = "gssapi")]
    Gssapi,
}

impl SecuritySchemeType {
    /// The `type` value as it appears in a document.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::UserPassword => "userPassword",
            Self::ApiKey => "apiKey",
            Self::X509 => "X509",
            Self::SymmetricEncryption => "symmetricEncryption",
            Self::AsymmetricEncryption => "asymmetricEncryption",
            Self::HttpApiKey => "httpApiKey",
            Self::Http => "http",
            Self::OAuth2 => "oauth2",
            Self::OpenIdConnect => "openIdConnect",
            Self::Plain => "plain",
            Self::ScramSha256 => "scramSha256",
            Self::ScramSha512 => "scramSha512",
            Self::Gssapi => "gssapi",
        }
    }

    /// Whether a security requirement naming this scheme may list
    /// scopes.
    #[must_use]
    pub fn takes_scopes(self) -> bool {
        matches!(self, Self::OAuth2 | Self::OpenIdConnect)
    }
}

/// Where an API key lives.
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ApiKeyLocation {
    User,
    Password,
    Query,
    Header,
    Cookie,
}

#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct SecurityScheme {
    /// **Required** The type of the security scheme.
    #[serde(rename = "type")]
    pub scheme_type: SecuritySchemeType,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// **Required for `apiKey` / `httpApiKey`** — where the key is
    /// carried.
    #[serde(rename = "in", skip_serializing_if = "Option::is_none")]
    pub in_: Option<ApiKeyLocation>,

    /// **Required for `httpApiKey`** — the header / query / cookie name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// **Required for `http`** — the HTTP Authorization scheme.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheme: Option<String>,

    #[serde(rename = "bearerFormat", skip_serializing_if = "Option::is_none")]
    pub bearer_format: Option<String>,

    /// **Required for `oauth2`** — the supported flows.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub flows: Option<OAuthFlows>,

    /// **Required for `openIdConnect`** — the discovery URL.
    #[serde(rename = "openIdConnectUrl", skip_serializing_if = "Option::is_none")]
    pub open_id_connect_url: Option<String>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl SecurityScheme {
    /// The fields this scheme's `type` may carry, beyond `type`,
    /// `description`, and `x-` extensions.
    fn allowed_fields(&self) -> &'static [&'static str] {
        match self.scheme_type {
            SecuritySchemeType::ApiKey => &["in"],
            SecuritySchemeType::HttpApiKey => &["in", "name"],
            SecuritySchemeType::Http => &["scheme", "bearerFormat"],
            SecuritySchemeType::OAuth2 => &["flows"],
            SecuritySchemeType::OpenIdConnect => &["openIdConnectUrl"],
            _ => &[],
        }
    }

    /// Report any field belonging to a different variant — each branch
    /// of the schema's `oneOf` is `additionalProperties: false`.
    fn check_forbidden_fields(&self, ctx: &mut Context) {
        let allowed = self.allowed_fields();
        for (field, present) in [
            ("in", self.in_.is_some()),
            ("name", self.name.is_some()),
            ("scheme", self.scheme.is_some()),
            ("bearerFormat", self.bearer_format.is_some()),
            ("flows", self.flows.is_some()),
            ("openIdConnectUrl", self.open_id_connect_url.is_some()),
        ] {
            if present && !allowed.contains(&field) {
                ctx.error_field(
                    field,
                    format!(
                        "is not allowed on a `{}` security scheme",
                        self.scheme_type.as_str()
                    ),
                );
            }
        }
    }
}

impl ValidateWithContext for SecurityScheme {
    fn validate_with_context(&self, ctx: &mut Context) {
        self.check_forbidden_fields(ctx);
        match self.scheme_type {
            SecuritySchemeType::ApiKey => match self.in_ {
                None => ctx.error_field("in", "is required for the `apiKey` type"),
                Some(ApiKeyLocation::User | ApiKeyLocation::Password) => {}
                Some(_) => ctx.error_field("in", "must be `user` or `password` for `apiKey`"),
            },
            SecuritySchemeType::HttpApiKey => {
                match self.in_ {
                    None => ctx.error_field("in", "is required for the `httpApiKey` type"),
                    Some(
                        ApiKeyLocation::Query | ApiKeyLocation::Header | ApiKeyLocation::Cookie,
                    ) => {}
                    Some(_) => ctx.error_field(
                        "in",
                        "must be `query`, `header` or `cookie` for `httpApiKey`",
                    ),
                }
                match &self.name {
                    None => ctx.error_field("name", "is required for the `httpApiKey` type"),
                    Some(name) => ctx.require_non_empty("name", name),
                }
            }
            SecuritySchemeType::Http => {
                match &self.scheme {
                    None => ctx.error_field("scheme", "is required for the `http` type"),
                    Some(scheme) => ctx.require_non_empty("scheme", scheme),
                }
                if self.bearer_format.is_some()
                    && self.scheme.as_deref().is_some_and(|s| s != "bearer")
                {
                    ctx.error_field("bearerFormat", "is only allowed when `scheme` is `bearer`");
                }
            }
            SecuritySchemeType::OAuth2 => match &self.flows {
                None => ctx.error_field("flows", "is required for the `oauth2` type"),
                Some(flows) => ctx.in_field("flows", |ctx| flows.validate_with_context(ctx)),
            },
            SecuritySchemeType::OpenIdConnect => match &self.open_id_connect_url {
                None => ctx.error_field(
                    "openIdConnectUrl",
                    "is required for the `openIdConnect` type",
                ),
                Some(url) => ctx.require_non_empty("openIdConnectUrl", url),
            },
            _ => {}
        }
    }
}

/// The OAuth 2 flows a scheme supports.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OAuthFlows {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub implicit: Option<OAuthFlow>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub password: Option<OAuthFlow>,

    #[serde(rename = "clientCredentials", skip_serializing_if = "Option::is_none")]
    pub client_credentials: Option<OAuthFlow>,

    #[serde(rename = "authorizationCode", skip_serializing_if = "Option::is_none")]
    pub authorization_code: Option<OAuthFlow>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl ValidateWithContext for OAuthFlows {
    fn validate_with_context(&self, ctx: &mut Context) {
        if self.implicit.is_none()
            && self.password.is_none()
            && self.client_credentials.is_none()
            && self.authorization_code.is_none()
        {
            ctx.error("must define at least one OAuth flow");
        }

        for (field, flow, required, forbidden) in [
            (
                "implicit",
                self.implicit.as_ref(),
                &["authorizationUrl"][..],
                &["tokenUrl"][..],
            ),
            (
                "password",
                self.password.as_ref(),
                &["tokenUrl"][..],
                &["authorizationUrl"][..],
            ),
            (
                "clientCredentials",
                self.client_credentials.as_ref(),
                &["tokenUrl"][..],
                &["authorizationUrl"][..],
            ),
            (
                "authorizationCode",
                self.authorization_code.as_ref(),
                &["authorizationUrl", "tokenUrl"][..],
                &[][..],
            ),
        ] {
            let Some(flow) = flow else { continue };
            ctx.in_field(field, |ctx| {
                flow.validate_grant(ctx, field, required, forbidden);
            });
        }
    }
}

/// Configuration for one OAuth 2 flow.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Default)]
pub struct OAuthFlow {
    #[serde(rename = "authorizationUrl", skip_serializing_if = "Option::is_none")]
    pub authorization_url: Option<String>,

    #[serde(rename = "tokenUrl", skip_serializing_if = "Option::is_none")]
    pub token_url: Option<String>,

    #[serde(rename = "refreshUrl", skip_serializing_if = "Option::is_none")]
    pub refresh_url: Option<String>,

    /// **Required** The available scopes, keyed by scope name. v3
    /// renamed this field to `availableScopes`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scopes: Option<BTreeMap<String, String>>,

    /// `x-`-prefixed Specification Extensions.
    #[serde(flatten)]
    #[serde(with = "crate::common::extensions")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extensions: Option<BTreeMap<String, serde_json::Value>>,
}

impl OAuthFlow {
    fn url(&self, field: &str) -> Option<&String> {
        match field {
            "authorizationUrl" => self.authorization_url.as_ref(),
            "tokenUrl" => self.token_url.as_ref(),
            _ => None,
        }
    }

    /// Validate this flow against its grant type's contract.
    fn validate_grant(
        &self,
        ctx: &mut Context,
        grant: &str,
        required: &[&str],
        forbidden: &[&str],
    ) {
        for field in required {
            match self.url(field) {
                None => ctx.error_field(field, format!("is required for the `{grant}` flow")),
                Some(url) => ctx.require_non_empty(field, url),
            }
        }
        for field in forbidden {
            if self.url(field).is_some() {
                ctx.error_field(field, format!("is not allowed on the `{grant}` flow"));
            }
        }
        if self.scopes.is_none() {
            ctx.error_field("scopes", format!("is required for the `{grant}` flow"));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use enumset::EnumSet;
    use serde_json::json;

    fn validate(value: serde_json::Value) -> Vec<String> {
        let scheme: SecurityScheme = serde_json::from_value(value).unwrap();
        let mut ctx = Context::with_path(EnumSet::empty(), "#.securitySchemes.s");
        scheme.validate_with_context(&mut ctx);
        ctx.errors.iter().map(ToString::to_string).collect()
    }

    #[test]
    fn simple_types_need_nothing_beyond_the_discriminator() {
        for scheme_type in [
            "userPassword",
            "X509",
            "symmetricEncryption",
            "asymmetricEncryption",
            "plain",
            "scramSha256",
            "scramSha512",
            "gssapi",
        ] {
            assert!(
                validate(json!({ "type": scheme_type })).is_empty(),
                "{scheme_type} should be valid"
            );
        }
    }

    #[test]
    fn oauth_flows_use_the_2_6_scopes_field() {
        let value = json!({
            "type": "oauth2",
            "flows": {
                "authorizationCode": {
                    "authorizationUrl": "https://example.com/auth",
                    "tokenUrl": "https://example.com/token",
                    "scopes": { "read": "Read access" }
                }
            }
        });
        let scheme: SecurityScheme = serde_json::from_value(value.clone()).unwrap();
        assert_eq!(serde_json::to_value(&scheme).unwrap(), value);
        assert!(validate(value).is_empty());

        // `availableScopes` is the v3 spelling and is not recognized
        // here, so the flow reads as missing its scopes.
        let errors = validate(json!({
            "type": "oauth2",
            "flows": { "implicit": { "authorizationUrl": "https://e/a", "availableScopes": {} } }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("implicit.scopes: is required")),
            "got: {errors:?}"
        );
    }

    #[test]
    fn each_grant_type_forbids_the_url_it_does_not_use() {
        let errors = validate(json!({
            "type": "oauth2",
            "flows": {
                "implicit": {
                    "authorizationUrl": "https://e/a",
                    "tokenUrl": "https://e/t",
                    "scopes": {}
                }
            }
        }));
        assert!(
            errors
                .iter()
                .any(|e| e.contains("implicit.tokenUrl: is not allowed")),
            "got: {errors:?}"
        );

        for grant in ["password", "clientCredentials"] {
            let errors = validate(json!({
                "type": "oauth2",
                "flows": {
                    grant: { "tokenUrl": "https://e/t", "authorizationUrl": "https://e/a", "scopes": {} }
                }
            }));
            assert!(
                errors
                    .iter()
                    .any(|e| e.contains(&format!("{grant}.authorizationUrl: is not allowed"))),
                "{grant}: {errors:?}"
            );
        }
    }

    #[test]
    fn per_type_requirements_are_enforced() {
        assert!(validate(json!({ "type": "apiKey", "in": "user" })).is_empty());
        assert!(
            validate(json!({ "type": "apiKey", "in": "header" }))[0]
                .contains("must be `user` or `password`")
        );
        assert!(validate(json!({ "type": "http" }))[0].contains("scheme: is required"));
        assert!(
            validate(json!({ "type": "http", "scheme": "basic", "bearerFormat": "JWT" }))[0]
                .contains("only allowed when `scheme` is `bearer`")
        );
        assert!(validate(json!({ "type": "oauth2" }))[0].contains("flows: is required"));
        assert!(validate(json!({ "type": "openIdConnect" }))[0].contains("openIdConnectUrl"));
        assert!(
            validate(json!({ "type": "httpApiKey", "in": "header", "name": "X-Key" })).is_empty()
        );
    }

    #[test]
    fn missing_and_empty_per_type_fields_are_each_reported() {
        for (value, needle) in [
            (
                json!({ "type": "apiKey" }),
                "in: is required for the `apiKey` type",
            ),
            (
                json!({ "type": "httpApiKey", "name": "X-Key" }),
                "in: is required for the `httpApiKey` type",
            ),
            (
                json!({ "type": "httpApiKey", "in": "user", "name": "X-Key" }),
                "must be `query`, `header` or `cookie`",
            ),
            (
                json!({ "type": "httpApiKey", "in": "header" }),
                "name: is required for the `httpApiKey` type",
            ),
            (
                json!({ "type": "httpApiKey", "in": "header", "name": "" }),
                "name: must not be empty",
            ),
            (
                json!({ "type": "openIdConnect", "openIdConnectUrl": "" }),
                "openIdConnectUrl: must not be empty",
            ),
            (
                json!({ "type": "oauth2", "flows": {} }),
                "at least one OAuth flow",
            ),
            (
                json!({
                    "type": "oauth2",
                    "flows": { "password": { "tokenUrl": "", "scopes": {} } }
                }),
                "password.tokenUrl: must not be empty",
            ),
            (
                json!({
                    "type": "oauth2",
                    "flows": { "authorizationCode": { "scopes": {} } }
                }),
                "authorizationCode.authorizationUrl: is required",
            ),
        ] {
            let errors = validate(value.clone());
            assert!(
                errors.iter().any(|e| e.contains(needle)),
                "{value}: expected {needle}, got {errors:?}"
            );
        }
    }

    #[test]
    fn fields_belonging_to_another_variant_are_rejected() {
        let errors = validate(json!({ "type": "userPassword", "flows": {} }));
        assert!(
            errors.iter().any(|e| e
                == "#.securitySchemes.s.flows: is not allowed on a `userPassword` security scheme"),
            "got: {errors:?}"
        );
    }

    #[test]
    fn type_renders_as_its_document_spelling_and_knows_about_scopes() {
        for (scheme_type, text, scopes) in [
            (SecuritySchemeType::UserPassword, "userPassword", false),
            (SecuritySchemeType::ApiKey, "apiKey", false),
            (SecuritySchemeType::X509, "X509", false),
            (
                SecuritySchemeType::SymmetricEncryption,
                "symmetricEncryption",
                false,
            ),
            (
                SecuritySchemeType::AsymmetricEncryption,
                "asymmetricEncryption",
                false,
            ),
            (SecuritySchemeType::HttpApiKey, "httpApiKey", false),
            (SecuritySchemeType::Http, "http", false),
            (SecuritySchemeType::OAuth2, "oauth2", true),
            (SecuritySchemeType::OpenIdConnect, "openIdConnect", true),
            (SecuritySchemeType::Plain, "plain", false),
            (SecuritySchemeType::ScramSha256, "scramSha256", false),
            (SecuritySchemeType::ScramSha512, "scramSha512", false),
            (SecuritySchemeType::Gssapi, "gssapi", false),
        ] {
            assert_eq!(scheme_type.as_str(), text);
            assert_eq!(serde_json::to_value(scheme_type).unwrap(), json!(text));
            assert_eq!(scheme_type.takes_scopes(), scopes, "{text}");
        }
    }

    #[test]
    fn security_requirement_round_trips_and_rejects_duplicate_scopes() {
        let value = json!({ "petstore_auth": ["write:pets", "read:pets"] });
        let requirement: SecurityRequirement = serde_json::from_value(value.clone()).unwrap();
        assert_eq!(serde_json::to_value(&requirement).unwrap(), value);
        assert_eq!(requirement.get("petstore_auth").map(Vec::len), Some(2));
        assert!(requirement.get("absent").is_none());
        assert!(!requirement.is_empty());
        assert!(SecurityRequirement::default().is_empty());

        let dup: SecurityRequirement =
            serde_json::from_value(json!({ "auth": ["read", "read"] })).unwrap();
        let mut ctx = Context::with_path(EnumSet::empty(), "#.security[0]");
        dup.validate_with_context(&mut ctx);
        assert!(
            ctx.errors
                .iter()
                .any(|e| e == "#.security[0].auth: duplicate scope `read`"),
            "got: {:?}",
            ctx.errors
        );
    }
}