Skip to content

Commit 777b7ee

Browse files
authored
[Rust-Axum] Basic Authorization - Basic Analytic - XSS Sanitize (#21578)
* Update * Update * Update * Update * Update * Update
1 parent 0b0d534 commit 777b7ee

File tree

131 files changed

+5460
-1340
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+5460
-1340
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
generatorName: rust-axum
2+
outputDir: samples/server/petstore/rust-axum/output/apikey-authorization
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/jetbrains/CheckoutBasicBearerCookieQueryHeaderBasicBearer.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/rust-axum
5+
generateAliasAsModel: true
6+
additionalProperties:
7+
hideGenerationTimestamp: "true"
8+
packageName: apikey-authorization
9+
basicAuthorization: true
10+
basicAnalytic: true
11+
ownedRequest: true
12+
globalProperties:
13+
skipFormModel: false
14+
enablePostProcessFile: true

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege
6161
private Boolean allowBlockingValidator = false;
6262
private Boolean allowBlockingResponseSerialize = false;
6363
private String externCrateName;
64+
private Boolean basicAuthorization = false;
65+
private Boolean basicAnalytic = false;
66+
private Boolean ownedRequest = false;
6467

6568
// Types
6669
private static final String uuidType = "uuid::Uuid";
@@ -286,7 +289,7 @@ public void processOpts() {
286289
LOGGER.info("Warning: Environment variable 'RUST_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
287290
}
288291

289-
if (!Boolean.TRUE.equals(ModelUtils.isGenerateAliasAsModel())) {
292+
if (!ModelUtils.isGenerateAliasAsModel()) {
290293
LOGGER.warn("generateAliasAsModel is set to false, which means array/map will be generated as model instead and the resulting code may have issues. Please enable `generateAliasAsModel` to address the issue.");
291294
}
292295

@@ -316,6 +319,24 @@ public void processOpts() {
316319
} else {
317320
additionalProperties.put("allowBlockingResponseSerialize", allowBlockingResponseSerialize);
318321
}
322+
323+
if (additionalProperties.containsKey("basicAuthorization")) {
324+
basicAuthorization = convertPropertyToBooleanAndWriteBack("basicAuthorization");
325+
} else {
326+
additionalProperties.put("basicAuthorization", basicAuthorization);
327+
}
328+
329+
if (additionalProperties.containsKey("basicAnalytic")) {
330+
basicAnalytic = convertPropertyToBooleanAndWriteBack("basicAnalytic");
331+
} else {
332+
additionalProperties.put("basicAnalytic", basicAnalytic);
333+
}
334+
335+
if (additionalProperties.containsKey("ownedRequest")) {
336+
ownedRequest = convertPropertyToBooleanAndWriteBack("ownedRequest");
337+
} else {
338+
additionalProperties.put("ownedRequest", ownedRequest);
339+
}
319340
}
320341

321342
private void setPackageName(String packageName) {
@@ -722,6 +743,21 @@ public OperationsMap postProcessOperationsWithModels(final OperationsMap operati
722743
operations.put("havingAuthMethod", true);
723744
operations.getOperation().forEach(op -> op.vendorExtensions.put("havingAuthMethod", true));
724745
this.havingAuthMethods = true;
746+
747+
if (basicAuthorization) {
748+
operations.put("basicAuthorization", true);
749+
operations.getOperation().forEach(op -> op.vendorExtensions.put("basicAuthorization", true));
750+
}
751+
}
752+
753+
if (basicAnalytic) {
754+
operations.put("basicAnalytic", true);
755+
operations.getOperation().forEach(op -> op.vendorExtensions.put("basicAnalytic", true));
756+
}
757+
758+
if (ownedRequest) {
759+
operations.put("ownedRequest", true);
760+
operations.getOperation().forEach(op -> op.vendorExtensions.put("ownedRequest", true));
725761
}
726762

727763
return operationsMap;
@@ -982,6 +1018,22 @@ public void postProcessModelProperty(CodegenModel model, CodegenProperty propert
9821018
property.dataType = objectType;
9831019
property.isNullable = false;
9841020
}
1021+
1022+
if (property.dataType.startsWith(vecType + "<String")) {
1023+
property.vendorExtensions.put("is-vec-string", true);
1024+
} else if (property.dataType.startsWith(vecType + "<models::")) {
1025+
property.vendorExtensions.put("is-vec-nested", true);
1026+
} else if (property.dataType.startsWith(mapType + "<String, String")) {
1027+
property.vendorExtensions.put("is-map-string", true);
1028+
} else if (property.dataType.startsWith(mapType + "<String, models::")) {
1029+
property.vendorExtensions.put("is-map-nested", true);
1030+
} else if (property.dataType.startsWith(mapType + "<String")) {
1031+
property.vendorExtensions.put("is-map", true);
1032+
} else if (property.dataType.startsWith("models::")) {
1033+
property.vendorExtensions.put("is-nested", true);
1034+
} else if (stringType.equals(property.dataType)) {
1035+
property.vendorExtensions.put("is-string", true);
1036+
}
9851037
}
9861038

9871039
@Override

modules/openapi-generator/src/main/resources/rust-axum/Cargo.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description = "{{{.}}}"
1313
{{#licenseInfo}}
1414
license = "{{.}}"
1515
{{/licenseInfo}}
16-
edition = "2021"
16+
edition = "2024"
1717
{{#publishRustRegistry}}
1818
publish = ["{{.}}"]
1919
{{/publishRustRegistry}}
@@ -39,6 +39,7 @@ conversion = [
3939
]
4040

4141
[dependencies]
42+
ammonia = "4"
4243
async-trait = "0.1"
4344
axum = { version = "0.8", features = ["multipart"] }
4445
axum-extra = { version = "0.10", features = ["cookie", "query"] }

modules/openapi-generator/src/main/resources/rust-axum/apis-mod.mustache

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ pub mod {{classFilename}};
44
{{/apis}}
55
{{/apiInfo}}
66

7+
{{#basicAuthorization}}
8+
#[allow(dead_code)]
9+
#[derive(Debug, Eq, PartialEq)]
10+
pub enum Authorization {
11+
Authorized,
12+
Forbidden,
13+
}
14+
{{/basicAuthorization}}
15+
16+
{{#basicAnalytic}}
17+
pub mod event {
18+
/// Anything to be recorded.
19+
pub type Event = std::collections::HashMap<String, String>;
20+
21+
pub mod convention {
22+
pub const EVENT_SERVICE: &str = "_service_";
23+
pub const EVENT_ACTOR: &str = "_actor_";
24+
pub const EVENT_ACTION: &str = "_action_";
25+
pub const EVENT_RESOURCE_TYPE: &str = "_resource_type_";
26+
pub const EVENT_RESOURCE: &str = "_resource_";
27+
pub const EVENT_STATUS_CODE: &str = "_status_code_";
28+
pub const EVENT_LATENCY_SECS: &str = "_latency_secs_";
29+
pub const EVENT_TIMESTAMP: &str = "timestamp";
30+
}
31+
}
32+
33+
#[async_trait::async_trait]
34+
pub trait EventDispatcher {
35+
fn service_name(&self) -> String;
36+
async fn dispatch(&self, event: event::Event) {}
37+
}
38+
{{/basicAnalytic}}
39+
740
{{#authMethods}}
841
{{#isApiKey}}
942
{{#isKeyInCookie}}

modules/openapi-generator/src/main/resources/rust-axum/apis.mustache

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,75 @@ use crate::{models, types::*};
1515
{{/operations}}
1616

1717
{{#operations}}
18+
19+
{{#basicAuthorization}}
20+
/// {{classnamePascalCase}} APIs - Authorization.
21+
#[async_trait]
22+
#[allow(clippy::ptr_arg)]
23+
pub trait {{classnamePascalCase}}Authorization {
24+
type Claims;
25+
26+
{{#operation}}
27+
{{#vendorExtensions}}
28+
{{#x-has-auth-methods}}
29+
{{#basicAuthorization}}
30+
{{#vendorExtensions}}
31+
/// Authorization{{#summary}} - {{{.}}}{{/summary}}.
32+
/// {{{operationId}}} - {{{httpMethod}}} {{{basePathWithoutHost}}}{{{path}}}
33+
async fn {{{x-operation-id}}}_authorize(
34+
&self,
35+
method: &Method,
36+
host: &Host,
37+
cookies: &CookieJar,
38+
claims: &Self::Claims,
39+
{{#headerParams.size}}
40+
header_params: &models::{{{operationIdCamelCase}}}HeaderParams,
41+
{{/headerParams.size}}
42+
{{#pathParams.size}}
43+
path_params: &models::{{{operationIdCamelCase}}}PathParams,
44+
{{/pathParams.size}}
45+
{{#queryParams.size}}
46+
query_params: &models::{{{operationIdCamelCase}}}QueryParams,
47+
{{/queryParams.size}}
48+
{{^x-consumes-multipart-related}}
49+
{{^x-consumes-multipart}}
50+
{{#bodyParam}}
51+
{{#vendorExtensions}}
52+
{{^x-consumes-plain-text}}
53+
body: &{{^required}}Option<{{/required}}{{{dataType}}}{{^required}}>{{/required}},
54+
{{/x-consumes-plain-text}}
55+
{{#x-consumes-plain-text}}
56+
{{#isString}}
57+
body: &String,
58+
{{/isString}}
59+
{{^isString}}
60+
body: &Bytes,
61+
{{/isString}}
62+
{{/x-consumes-plain-text}}
63+
{{/vendorExtensions}}
64+
{{/bodyParam}}
65+
{{/x-consumes-multipart}}
66+
{{/x-consumes-multipart-related}}
67+
{{#x-consumes-multipart}}
68+
body: &Multipart,
69+
{{/x-consumes-multipart}}
70+
{{#x-consumes-multipart-related}}
71+
body: &axum::body::Body,
72+
{{/x-consumes-multipart-related}}
73+
) -> Result<super::Authorization, ()> {
74+
Ok(super::Authorization::Authorized)
75+
}
76+
{{/vendorExtensions}}
77+
{{/basicAuthorization}}
78+
{{/x-has-auth-methods}}
79+
{{/vendorExtensions}}
80+
{{^-last}}
81+
82+
{{/-last}}
83+
{{/operation}}
84+
}
85+
{{/basicAuthorization}}
86+
1887
/// {{classnamePascalCase}}
1988
#[async_trait]
2089
#[allow(clippy::ptr_arg)]
@@ -32,36 +101,37 @@ pub trait {{classnamePascalCase}}<E: std::fmt::Debug + Send + Sync + 'static = (
32101
/// {{{operationId}}} - {{{httpMethod}}} {{{basePathWithoutHost}}}{{{path}}}
33102
async fn {{{x-operation-id}}}(
34103
&self,
35-
method: &Method,
36-
host: &Host,
37-
cookies: &CookieJar,
104+
{{#basicAnalytic}}event: &mut super::event::Event,{{/basicAnalytic}}
105+
method: {{^ownedRequest}}&{{/ownedRequest}}Method,
106+
host: {{^ownedRequest}}&{{/ownedRequest}}Host,
107+
cookies: {{^ownedRequest}}&{{/ownedRequest}}CookieJar,
38108
{{#vendorExtensions}}
39109
{{#x-has-auth-methods}}
40-
claims: &Self::Claims,
110+
claims: {{^ownedRequest}}&{{/ownedRequest}}Self::Claims,
41111
{{/x-has-auth-methods}}
42112
{{/vendorExtensions}}
43113
{{#headerParams.size}}
44-
header_params: &models::{{{operationIdCamelCase}}}HeaderParams,
114+
header_params: {{^ownedRequest}}&{{/ownedRequest}}models::{{{operationIdCamelCase}}}HeaderParams,
45115
{{/headerParams.size}}
46116
{{#pathParams.size}}
47-
path_params: &models::{{{operationIdCamelCase}}}PathParams,
117+
path_params: {{^ownedRequest}}&{{/ownedRequest}}models::{{{operationIdCamelCase}}}PathParams,
48118
{{/pathParams.size}}
49119
{{#queryParams.size}}
50-
query_params: &models::{{{operationIdCamelCase}}}QueryParams,
120+
query_params: {{^ownedRequest}}&{{/ownedRequest}}models::{{{operationIdCamelCase}}}QueryParams,
51121
{{/queryParams.size}}
52122
{{^x-consumes-multipart-related}}
53123
{{^x-consumes-multipart}}
54124
{{#bodyParam}}
55125
{{#vendorExtensions}}
56126
{{^x-consumes-plain-text}}
57-
body: &{{^required}}Option<{{/required}}{{{dataType}}}{{^required}}>{{/required}},
127+
body: {{^ownedRequest}}&{{/ownedRequest}}{{^required}}Option<{{/required}}{{{dataType}}}{{^required}}>{{/required}},
58128
{{/x-consumes-plain-text}}
59129
{{#x-consumes-plain-text}}
60130
{{#isString}}
61-
body: &String,
131+
body: {{^ownedRequest}}&{{/ownedRequest}}String,
62132
{{/isString}}
63133
{{^isString}}
64-
body: &Bytes,
134+
body: {{^ownedRequest}}&{{/ownedRequest}}Bytes,
65135
{{/isString}}
66136
{{/x-consumes-plain-text}}
67137
{{/vendorExtensions}}

0 commit comments

Comments
 (0)