-
-
Notifications
You must be signed in to change notification settings - Fork 7k
To fix incorrect Rust code on multiple apiKeys #8868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Please update the samples by running:
|
cc @frol (2017/07) @farcaller (2017/08) @richardwhiuk (2019/07) @paladinzh (2020/05) |
…enerator into 8833-rust_multi_apikey
Is it correct to say that this is a breaking change? What if the API doesn't use API key for authentication? For example, it only uses HTTP basic for authentication. |
You need to specify a type that impls ApiKey, even if not using it. let conf = Configuration::<String>::default(); But this approach allows dynamic ApiKeys, for example in my use case there is a nonce header that must be different and incremental for every call, and a signature header that's an HMAC based on a secret, the nonce and the URL. I think it's a standard Rust approach. A retrocompatibility approach could be not exporting plain Configuration struct, but adding to parent mod a pub type Configuration = configuration::Configuration<String>; |
@nappa85 what about following other clients and use a hash to store the API key instead? e.g. Configuration.cs in the C# client: https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/csharp/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs#L88-L98 There's also something call API key prefix but it's not something that needs to be included in this PR. |
It's a less powerful approach, and, as you already told, it doesn't support the prefixes. In my use case I had to expand my approach passing also a reference to the RequestBuilder to be able to retrieve already setted headers and the URL: use openapi::apis::configuration::ApiKey;
use reqwest::RequestBuilder;
use chrono::Utc;
use hmac::{Hmac, Mac, NewMac};
use log::error;
type HmacSha512 = Hmac<sha2::Sha512>;
/// TheRockTrading ApiKeys manager
pub struct TrtKeys {
api_key: String,
secret: String,
}
impl TrtKeys {
/// costructor
pub fn new(api_key: String, secret: String) -> Self {
TrtKeys {
api_key,
secret
}
}
}
impl ApiKey for TrtKeys {
fn get_prefix(&self, _: &str) -> Option<String> {
None
}
fn get_key(&self, name: &str, req_builder: &RequestBuilder) -> Option<String> {
match name {
"X-TRT-KEY" => Some(self.api_key.clone()),
"X-TRT-NONCE" => {
let timestamp = Utc::now().timestamp_nanos();
Some(timestamp.to_string())
},
"X-TRT-SIGN" => {
let rb = req_builder.try_clone()?;
let req = rb.build()
.map_err(|e| error!("error building request: {}", e))
.ok()?;
let nonce = req.headers().get("X-TRT-NONCE")?;
let mut mac = HmacSha512::new_varkey(self.secret.as_bytes())
.map_err(|e| error!("error initializing HMAC-SHA521: {}", e))
.ok()?;
// this way we skip the utf8 check
let mut bytes = nonce.as_bytes().to_vec();
let mut url = req.url().to_string().into_bytes();
bytes.append(&mut url);
mac.update(&bytes);
Some(hex::encode(mac.finalize().into_bytes()))
},
_ => None,
}
}
} I think it's a poweful example |
It's evident this has been forgotten about, but is still an issue in 7.7.0. |
To fix #8833
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
,5.1.x
,6.0.x