Skip to content

Commit 756eec2

Browse files
authored
Add credential using a base64 encoded API key (#238)
1 parent 52c74a4 commit 756eec2

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

api_generator/src/generator/code_gen/url/enum_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ mod tests {
357357
methods: vec![HttpMethod::Get, HttpMethod::Post],
358358
parts: {
359359
let mut map = BTreeMap::new();
360-
map.insert("index".to_string(), Type {i
360+
map.insert("index".to_string(), Type {
361361
ty: TypeKind::List,
362362
description: Some("A comma-separated list of index names to search".to_string()),
363363
options: vec![],

elasticsearch/src/auth.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ pub enum Credentials {
3131
/// This requires the `native-tls` or `rustls-tls` feature to be enabled.
3232
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
3333
Certificate(ClientCertificate),
34-
/// An id and api_key to use for API key authentication
34+
/// An API key id and secret to use for API key authentication
3535
ApiKey(String, String),
36+
/// An API key as a base64-encoded id and secret
37+
EncodedApiKey(String),
3638
}
3739

3840
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]

elasticsearch/src/http/transport.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use lazy_static::lazy_static;
4141
use serde::Serialize;
4242
use serde_json::Value;
4343
use std::{
44+
convert::TryFrom,
4445
error, fmt,
4546
fmt::Debug,
4647
io::{self, Write},
@@ -492,10 +493,14 @@ impl Transport {
492493
write!(encoder, "{}:", i).unwrap();
493494
write!(encoder, "{}", k).unwrap();
494495
}
495-
request_builder.header(
496-
AUTHORIZATION,
497-
HeaderValue::from_bytes(&header_value).unwrap(),
498-
)
496+
let mut header_value = HeaderValue::from_bytes(&header_value).unwrap();
497+
header_value.set_sensitive(true);
498+
request_builder.header(AUTHORIZATION, header_value)
499+
}
500+
Credentials::EncodedApiKey(k) => {
501+
let mut header_value = HeaderValue::try_from(format!("ApiKey {}", k)).unwrap();
502+
header_value.set_sensitive(true);
503+
request_builder.header(AUTHORIZATION, header_value)
499504
}
500505
}
501506
}

elasticsearch/tests/auth.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,32 @@ async fn api_key_header() -> Result<(), failure::Error> {
7474
Ok(())
7575
}
7676

77+
#[tokio::test]
78+
async fn encoded_api_key_header() -> Result<(), failure::Error> {
79+
let server = server::http(move |req| async move {
80+
let mut header_value = b"ApiKey ".to_vec();
81+
{
82+
let mut encoder = EncoderWriter::new(&mut header_value, &BASE64_STANDARD);
83+
write!(encoder, "id:api_key").unwrap();
84+
}
85+
86+
assert_eq!(
87+
req.headers()["authorization"],
88+
String::from_utf8(header_value).unwrap()
89+
);
90+
http::Response::default()
91+
});
92+
93+
let builder = client::create_builder(format!("http://{}", server.addr()).as_ref())
94+
// result of `echo -n "id:api_key" | base64`
95+
.auth(Credentials::EncodedApiKey("aWQ6YXBpX2tleQ==".into()));
96+
97+
let client = client::create(builder);
98+
let _response = client.ping().send().await?;
99+
100+
Ok(())
101+
}
102+
77103
#[tokio::test]
78104
async fn bearer_header() -> Result<(), failure::Error> {
79105
let server = server::http(move |req| async move {

0 commit comments

Comments
 (0)