forked from loafle/openapi-generator-original
[Rust Server] Support Bearer based Authentication (#3606)
Add handling case for Bearer Authentication on the client and update the samples
This commit is contained in:
parent
44fda895d2
commit
1ce09788a9
@ -750,10 +750,21 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (op.authMethods != null) {
|
if (op.authMethods != null) {
|
||||||
|
boolean headerAuthMethods = false;
|
||||||
|
|
||||||
for (CodegenSecurity s : op.authMethods) {
|
for (CodegenSecurity s : op.authMethods) {
|
||||||
if (s.isApiKey && s.isKeyInHeader) {
|
if (s.isApiKey && s.isKeyInHeader) {
|
||||||
s.vendorExtensions.put("x-apiKeyName", toModelName(s.keyParamName));
|
s.vendorExtensions.put("x-apiKeyName", toModelName(s.keyParamName));
|
||||||
|
headerAuthMethods = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s.isBasicBasic || s.isBasicBearer || s.isOAuth) {
|
||||||
|
headerAuthMethods = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (headerAuthMethods) {
|
||||||
|
op.vendorExtensions.put("hasHeaderAuthMethods", "true");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,29 +417,53 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
{{/vendorExtensions}}
|
{{/vendorExtensions}}
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
{{#vendorExtensions.hasHeaderAuthMethods}}
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
{{#authMethods}}
|
{{#authMethods}}
|
||||||
{{#isBasic}}
|
{{#isApiKey}}
|
||||||
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
|
{{#isKeyInHeader}}
|
||||||
if let AuthData::Basic(ref basic_header) = *auth_data {
|
&AuthData::ApiKey(ref api_key) => {
|
||||||
|
header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] }
|
||||||
|
request.headers_mut().set(
|
||||||
|
{{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}(api_key.to_string())
|
||||||
|
)
|
||||||
|
},
|
||||||
|
{{/isKeyInHeader}}
|
||||||
|
{{/isApiKey}}
|
||||||
|
{{#isBasicBasic}}
|
||||||
|
&AuthData::Basic(ref basic_header) => {
|
||||||
request.headers_mut().set(hyper::header::Authorization(
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
basic_header.clone(),
|
basic_header.clone(),
|
||||||
))
|
))
|
||||||
}
|
},
|
||||||
}
|
{{/isBasicBasic}}
|
||||||
{{/isBasic}}
|
{{#isBasicBearer}}
|
||||||
{{#isApiKey}}
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
{{#isKeyInHeader}}
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] }
|
bearer_header.clone(),
|
||||||
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
|
))
|
||||||
if let AuthData::ApiKey(ref api_key) = *auth_data {
|
},
|
||||||
request.headers_mut().set({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}(api_key.to_string()));
|
{{/isBasicBearer}}
|
||||||
}
|
{{#isOAuth}}
|
||||||
}
|
{{^isBasicBearer}}
|
||||||
{{/isKeyInHeader}}
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
{{/isApiKey}}
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
{{/isBasicBearer}}
|
||||||
|
{{/isOAuth}}
|
||||||
{{/authMethods}}
|
{{/authMethods}}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
{{/vendorExtensions.hasHeaderAuthMethods}}
|
||||||
{{#headerParams}}
|
{{#headerParams}}
|
||||||
{{#-first}}
|
{{#-first}}
|
||||||
|
|
||||||
// Header parameters
|
// Header parameters
|
||||||
{{/-first}}{{^isMapContainer}} header! { (Request{{vendorExtensions.typeName}}, "{{{baseName}}}") => {{#isListContainer}}({{{baseType}}})*{{/isListContainer}}{{^isListContainer}}[{{{dataType}}}]{{/isListContainer}} }
|
{{/-first}}{{^isMapContainer}} header! { (Request{{vendorExtensions.typeName}}, "{{{baseName}}}") => {{#isListContainer}}({{{baseType}}})*{{/isListContainer}}{{^isListContainer}}[{{{dataType}}}]{{/isListContainer}} }
|
||||||
{{#required}} request.headers_mut().set(Request{{vendorExtensions.typeName}}(param_{{{paramName}}}{{#isListContainer}}.clone(){{/isListContainer}}));
|
{{#required}} request.headers_mut().set(Request{{vendorExtensions.typeName}}(param_{{{paramName}}}{{#isListContainer}}.clone(){{/isListContainer}}));
|
||||||
|
@ -879,13 +879,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
request.set_body(body.into_bytes());
|
request.set_body(body.into_bytes());
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
|
|
||||||
if let AuthData::Basic(ref basic_header) = *auth_data {
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Basic(ref basic_header) => {
|
||||||
request.headers_mut().set(hyper::header::Authorization(
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
basic_header.clone(),
|
basic_header.clone(),
|
||||||
))
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -975,6 +980,7 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
request.set_body(body.into_bytes());
|
request.set_body(body.into_bytes());
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
// Header parameters
|
// Header parameters
|
||||||
header! { (RequestEnumHeaderStringArray, "enum_header_string_array") => (String)* }
|
header! { (RequestEnumHeaderStringArray, "enum_header_string_array") => (String)* }
|
||||||
param_enum_header_string_array.map(|header| request.headers_mut().set(RequestEnumHeaderStringArray(header.clone())));
|
param_enum_header_string_array.map(|header| request.headers_mut().set(RequestEnumHeaderStringArray(header.clone())));
|
||||||
@ -1272,6 +1278,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
request.headers_mut().set(ContentType(mimetypes::requests::ADD_PET.clone()));
|
request.headers_mut().set(ContentType(mimetypes::requests::ADD_PET.clone()));
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1333,6 +1351,19 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Header parameters
|
// Header parameters
|
||||||
header! { (RequestApiKey, "api_key") => [String] }
|
header! { (RequestApiKey, "api_key") => [String] }
|
||||||
param_api_key.map(|header| request.headers_mut().set(RequestApiKey(header)));
|
param_api_key.map(|header| request.headers_mut().set(RequestApiKey(header)));
|
||||||
@ -1398,6 +1429,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1483,6 +1526,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1567,12 +1622,19 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::ApiKey(ref api_key) => {
|
||||||
header! { (ApiKey, "api_key") => [String] }
|
header! { (ApiKey, "api_key") => [String] }
|
||||||
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
|
request.headers_mut().set(
|
||||||
if let AuthData::ApiKey(ref api_key) = *auth_data {
|
ApiKey(api_key.to_string())
|
||||||
request.headers_mut().set(ApiKey(api_key.to_string()));
|
)
|
||||||
}
|
},
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1670,6 +1732,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_PET.clone()));
|
request.headers_mut().set(ContentType(mimetypes::requests::UPDATE_PET.clone()));
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1757,6 +1831,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
request.set_body(body.into_bytes());
|
request.set_body(body.into_bytes());
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -1867,6 +1953,18 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::Bearer(ref bearer_header) => {
|
||||||
|
request.headers_mut().set(hyper::header::Authorization(
|
||||||
|
bearer_header.clone(),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
@ -2011,12 +2109,19 @@ impl<F, C> Api<C> for Client<F> where
|
|||||||
|
|
||||||
|
|
||||||
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
|
||||||
|
|
||||||
|
(context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
|
||||||
|
// Currently only authentication with Basic, API Key, and Bearer are supported
|
||||||
|
match auth_data {
|
||||||
|
&AuthData::ApiKey(ref api_key) => {
|
||||||
header! { (ApiKey, "api_key") => [String] }
|
header! { (ApiKey, "api_key") => [String] }
|
||||||
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
|
request.headers_mut().set(
|
||||||
if let AuthData::ApiKey(ref api_key) = *auth_data {
|
ApiKey(api_key.to_string())
|
||||||
request.headers_mut().set(ApiKey(api_key.to_string()));
|
)
|
||||||
}
|
},
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
Box::new(self.client_service.call(request)
|
Box::new(self.client_service.call(request)
|
||||||
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
.map_err(|e| ApiError(format!("No response received: {}", e)))
|
||||||
.and_then(|mut response| {
|
.and_then(|mut response| {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user