[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:
Richard Whitehouse 2019-08-17 12:30:47 +01:00 committed by GitHub
parent 44fda895d2
commit 1ce09788a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 176 additions and 36 deletions

View File

@ -750,10 +750,21 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
}
if (op.authMethods != null) {
boolean headerAuthMethods = false;
for (CodegenSecurity s : op.authMethods) {
if (s.isApiKey && s.isKeyInHeader) {
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");
}
}
}

View File

@ -417,29 +417,53 @@ impl<F, C> Api<C> for Client<F> where
{{/vendorExtensions}}
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
{{#authMethods}}
{{#isBasic}}
if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
if let AuthData::Basic(ref basic_header) = *auth_data {
request.headers_mut().set(hyper::header::Authorization(
basic_header.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}}
{{#isApiKey}}
{{#isKeyInHeader}}
&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(
basic_header.clone(),
))
},
{{/isBasicBasic}}
{{#isBasicBearer}}
&AuthData::Bearer(ref bearer_header) => {
request.headers_mut().set(hyper::header::Authorization(
bearer_header.clone(),
))
},
{{/isBasicBearer}}
{{#isOAuth}}
{{^isBasicBearer}}
&AuthData::Bearer(ref bearer_header) => {
request.headers_mut().set(hyper::header::Authorization(
bearer_header.clone(),
))
},
{{/isBasicBearer}}
{{/isOAuth}}
{{/authMethods}}
_ => {}
}
}
{{/isBasic}}
{{#isApiKey}}
{{#isKeyInHeader}}
header! { ({{#vendorExtensions}}{{x-apiKeyName}}{{/vendorExtensions}}, "{{keyParamName}}") => [String] }
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()));
}
}
{{/isKeyInHeader}}
{{/isApiKey}}
{{/authMethods}}
});
{{/vendorExtensions.hasHeaderAuthMethods}}
{{#headerParams}}
{{#-first}}
// Header parameters
{{/-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}}));

View File

@ -879,13 +879,18 @@ impl<F, C> Api<C> for Client<F> where
request.set_body(body.into_bytes());
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 {
request.headers_mut().set(hyper::header::Authorization(
basic_header.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::Basic(ref basic_header) => {
request.headers_mut().set(hyper::header::Authorization(
basic_header.clone(),
))
},
_ => {}
}
}
});
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@ -975,6 +980,7 @@ impl<F, C> Api<C> for Client<F> where
request.set_body(body.into_bytes());
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
// Header parameters
header! { (RequestEnumHeaderStringArray, "enum_header_string_array") => (String)* }
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(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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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()));
(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! { (RequestApiKey, "api_key") => [String] }
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()));
(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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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()));
(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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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()));
header! { (ApiKey, "api_key") => [String] }
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(ApiKey(api_key.to_string()));
(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] }
request.headers_mut().set(
ApiKey(api_key.to_string())
)
},
_ => {}
}
}
});
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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(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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {
@ -1757,6 +1831,18 @@ impl<F, C> Api<C> for Client<F> where
request.set_body(body.into_bytes());
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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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()));
(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)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.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()));
header! { (ApiKey, "api_key") => [String] }
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(ApiKey(api_key.to_string()));
(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] }
request.headers_mut().set(
ApiKey(api_key.to_string())
)
},
_ => {}
}
}
});
Box::new(self.client_service.call(request)
.map_err(|e| ApiError(format!("No response received: {}", e)))
.and_then(|mut response| {