Skip to content

Commit

Permalink
drop base64 auth key support, impl generic apikey token input
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Nov 24, 2021
1 parent bc83d4b commit dd7761b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
37 changes: 14 additions & 23 deletions src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::fmt;

use near_primitives::serialize::to_base64;

pub struct AuthHeaderEntry {
pub header: String,
pub value: String,
pub struct AuthHeaderEntry<'a> {
pub header: &'a str,
pub value: &'a str,
}

mod private {
Expand Down Expand Up @@ -40,29 +36,24 @@ impl<T: AuthScheme> private::AuthState for Authenticated<T> {
}
}

#[derive(Clone)]
pub enum ApiKey {
Plain(String),
Base64(String),
}
#[derive(Clone, Debug)]
pub struct ApiKey(String);

impl fmt::Debug for ApiKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ApiKey::Plain(s) => f.debug_tuple("ApiKey::Plain").field(s).finish(),
ApiKey::Base64(s) => f.debug_tuple("ApiKey::Base64").field(s).finish(),
}
impl ApiKey {
pub fn new(api_key: impl Into<String>) -> Self {
Self(api_key.into())
}

pub fn as_str(&self) -> &str {
&self.0
}
}

impl AuthScheme for ApiKey {
fn get_auth_header(&self) -> AuthHeaderEntry {
AuthHeaderEntry {
header: "x-api-key".to_string(),
value: match self {
ApiKey::Plain(ref token) => to_base64(token),
ApiKey::Base64(ref token) => token.clone(),
},
header: "x-api-key",
value: self.0.as_str(),
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ impl<A: AuthState> JsonRpcClient<A> {
.post(&self.inner.server_addr)
.header("Content-Type", "application/json")
.body(request_payload);
if let Some(AuthHeaderEntry { ref header, value }) = self.auth_state.maybe_auth_header() {
if let Some(AuthHeaderEntry { header, value }) = self.auth_state.maybe_auth_header() {
request = request.header(header, value);
}
let response = request.send().await.map_err(|err| {
Expand Down

0 comments on commit dd7761b

Please sign in to comment.