diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java index e54439b40fa0..5938778e0a15 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java @@ -977,7 +977,6 @@ private void postProcessOperationWithModels(CodegenOperation op, List for (CodegenSecurity s : op.authMethods) { if (s.isApiKey && s.isKeyInHeader) { - s.vendorExtensions.put("x-api-key-name", toModelName(s.keyParamName)); headerAuthMethods = true; } diff --git a/modules/openapi-generator/src/main/resources/rust-server/bin-cli.mustache b/modules/openapi-generator/src/main/resources/rust-server/bin-cli.mustache index 994bcd4109e9..013262c1d12d 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/bin-cli.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/bin-cli.mustache @@ -86,6 +86,12 @@ struct Cli { bearer_token: Option, {{/hasOAuthMethods}} {{/hasHttpBearerMethods}} +{{#hasApiKeyMethods}} + + /// API key for authentication + #[arg(long, env = "{{#lambda.uppercase}}{{externCrateName}}{{/lambda.uppercase}}_API_KEY", hide_env = true)] + api_key: Option, +{{/hasApiKeyMethods}} } #[derive(Parser, Debug)] @@ -187,6 +193,12 @@ async fn main() -> Result<()> { debug!("Using bearer token"); auth_data = AuthData::bearer(bearer_token); } +{{#hasApiKeyMethods}} + if let Some(ref api_key) = args.api_key { + debug!("Using API key"); + auth_data = Some(AuthData::apikey(api_key)); + } +{{/hasApiKeyMethods}} {{/hasHttpBearerMethods}} {{^hasHttpBearerMethods}} {{#hasOAuthMethods}} @@ -196,11 +208,27 @@ async fn main() -> Result<()> { debug!("Using bearer token"); auth_data = AuthData::bearer(bearer_token); } +{{#hasApiKeyMethods}} + if let Some(ref api_key) = args.api_key { + debug!("Using API key"); + auth_data = Some(AuthData::apikey(api_key)); + } +{{/hasApiKeyMethods}} {{/hasOAuthMethods}} {{/hasHttpBearerMethods}} {{^hasHttpBearerMethods}} {{^hasOAuthMethods}} +{{#hasApiKeyMethods}} + let mut auth_data: Option = None; + + if let Some(ref api_key) = args.api_key { + debug!("Using API key"); + auth_data = Some(AuthData::apikey(api_key)); + } +{{/hasApiKeyMethods}} +{{^hasApiKeyMethods}} let auth_data: Option = None; +{{/hasApiKeyMethods}} {{/hasOAuthMethods}} {{/hasHttpBearerMethods}} diff --git a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache index 4a0f1ada3873..d231cadd51a5 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/client-operation.mustache @@ -99,7 +99,6 @@ #[allow(clippy::collapsible_match)] if let Some(auth_data) = Has::>::get(context).as_ref() { use headers::authorization::Credentials; - {{! Currently only authentication with Basic and Bearer are supported }} #[allow(clippy::single_match, clippy::match_single_binding)] match auth_data { {{#authMethods}} @@ -135,6 +134,19 @@ }, {{/isBasicBearer}} {{/isOAuth}} + {{#isApiKey}} + {{#isKeyInHeader}} + AuthData::ApiKey(ref api_key) => { + let header = match HeaderValue::from_str(api_key.as_str()) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {e}"))) + }; + request.headers_mut().insert( + HeaderName::from_static("{{#lambda.lowercase}}{{{keyParamName}}}{{/lambda.lowercase}}"), + header); + }, + {{/isKeyInHeader}} + {{/isApiKey}} {{/authMethods}} _ => {} } diff --git a/modules/openapi-generator/src/main/resources/rust-server/context.mustache b/modules/openapi-generator/src/main/resources/rust-server/context.mustache index f98ed9971401..b9ac507eb5e2 100644 --- a/modules/openapi-generator/src/main/resources/rust-server/context.mustache +++ b/modules/openapi-generator/src/main/resources/rust-server/context.mustache @@ -142,7 +142,7 @@ impl Service> for AddContext { use swagger::auth::api_key_from_header; - if let Some(header) = api_key_from_header(headers, "{{exts.x-key-param-name-lower}}") { + if let Some(header) = api_key_from_header(headers, "{{#lambda.lowercase}}{{{keyParamName}}}{{/lambda.lowercase}}") { let auth_data = AuthData::ApiKey(header); let context = context.push(Some(auth_data)); diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/bin/cli.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/bin/cli.rs index 9c46bce1182c..9ebc7336a225 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/bin/cli.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/bin/cli.rs @@ -97,6 +97,10 @@ struct Cli { /// Bearer token if used for authentication #[arg(env = "PETSTORE_WITH_FAKE_ENDPOINTS_MODELS_FOR_TESTING_BEARER_TOKEN", hide_env = true)] bearer_token: Option, + + /// API key for authentication + #[arg(long, env = "PETSTORE_WITH_FAKE_ENDPOINTS_MODELS_FOR_TESTING_API_KEY", hide_env = true)] + api_key: Option, } #[derive(Parser, Debug)] @@ -398,6 +402,10 @@ async fn main() -> Result<()> { debug!("Using bearer token"); auth_data = AuthData::bearer(bearer_token); } + if let Some(ref api_key) = args.api_key { + debug!("Using API key"); + auth_data = Some(AuthData::apikey(api_key)); + } #[allow(trivial_casts)] let context = swagger::make_context!( diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs index 1fd6875ed2f3..10886d73eed7 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/client/mod.rs @@ -2476,6 +2476,15 @@ impl Api for Client where use headers::authorization::Credentials; #[allow(clippy::single_match, clippy::match_single_binding)] match auth_data { + AuthData::ApiKey(ref api_key) => { + let header = match HeaderValue::from_str(api_key.as_str()) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {e}"))) + }; + request.headers_mut().insert( + HeaderName::from_static("api_key"), + header); + }, _ => {} } } @@ -2848,6 +2857,15 @@ impl Api for Client where use headers::authorization::Credentials; #[allow(clippy::single_match, clippy::match_single_binding)] match auth_data { + AuthData::ApiKey(ref api_key) => { + let header = match HeaderValue::from_str(api_key.as_str()) { + Ok(h) => h, + Err(e) => return Err(ApiError(format!("Unable to create header: {e}"))) + }; + request.headers_mut().insert( + HeaderName::from_static("api_key"), + header); + }, _ => {} } } diff --git a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/context.rs b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/context.rs index b3678c91f4b3..937ef9edc340 100644 --- a/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/context.rs +++ b/samples/server/petstore/rust-server/output/petstore-with-fake-endpoints-models-for-testing/src/context.rs @@ -114,7 +114,7 @@ impl Service> for AddContext { use swagger::auth::api_key_from_header; - if let Some(header) = api_key_from_header(headers, "") { + if let Some(header) = api_key_from_header(headers, "api_key") { let auth_data = AuthData::ApiKey(header); let context = context.push(Some(auth_data));