Skip to content

Commit

Permalink
Added API Key auth to rust-server (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Alejandro Herrera León authored and wing328 committed Apr 1, 2019
1 parent 69ad9a9 commit ee69861
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,14 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
}
header.nameInCamelCase = toModelName(header.baseName);
}

if (op.authMethods != null) {
for (CodegenSecurity s : op.authMethods) {
if (s.isApiKey && s.isKeyInHeader) {
s.vendorExtensions.put("x-apiKeyName", toModelName(s.keyParamName));
}
}
}
}

return objs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,23 @@ impl<F, C> Api<C> for Client<F> where
"{}{{{basePathWithoutHost}}}{{path}}",
self.base_path{{#pathParams}}, {{{baseName}}}=utf8_percent_encode(&param_{{{paramName}}}.to_string(), ID_ENCODE_SET){{/pathParams}}
);
{{#queryParams}}{{#-first}}
// Query parameters

let mut query_string = self::url::form_urlencoded::Serializer::new("".to_owned());
{{/-first}}{{#required}} query_string.append_pair("{{{baseName}}}", &param_{{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}{{^isListContainer}}.to_string(){{/isListContainer}});
{{/required}}{{^required}} if let Some({{{paramName}}}) = param_{{{paramName}}} {
{{#queryParams}}{{#required}} query_string.append_pair("{{{baseName}}}", &param_{{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}{{^isListContainer}}.to_string(){{/isListContainer}});{{/required}}
{{^required}} if let Some({{{paramName}}}) = param_{{{paramName}}} {
query_string.append_pair("{{{baseName}}}", &{{{paramName}}}{{#isListContainer}}.join(","){{/isListContainer}}{{^isListContainer}}.to_string(){{/isListContainer}});
}
{{/required}}{{#-last}}
}{{/required}}{{/queryParams}}
{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}} if let Some(auth_data) = (context as &Has<Option<AuthData>>).get().as_ref() {
if let AuthData::ApiKey(ref api_key) = *auth_data {
query_string.append_pair("{{keyParamName}}", api_key);
}
}{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}
let query_string_str = query_string.finish();
if !query_string_str.is_empty() {
uri += "?";
uri += &query_string_str;
}
{{/-last}}{{/queryParams}}

let uri = match Uri::from_str(&uri) {
Ok(uri) => uri,
Err(err) => return Box::new(futures::done(Err(ApiError(format!("Unable to build URI: {}", err))))),
Expand Down Expand Up @@ -305,13 +308,18 @@ impl<F, C> Api<C> for Client<F> where
request.headers_mut().set(ContentType(mimetypes::requests::{{#vendorExtensions}}{{{uppercase_operation_id}}}{{/vendorExtensions}}.clone()));
{{/bodyParam}}
request.headers_mut().set(XSpanId((context as &Has<XSpanIdString>).get().0.clone()));
{{#authMethods}}{{#isBasic}} (context as &Has<Option<AuthData>>).get().as_ref().map(|auth_data| {
if let &AuthData::Basic(ref basic_header) = auth_data {
{{#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(),
))
}
});{{/isBasic}}{{/authMethods}}{{#headerParams}}{{#-first}}
}{{/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}}{{#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}}));
Expand Down
Loading

0 comments on commit ee69861

Please sign in to comment.