Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Add authorization for http and websocket #829

Merged
merged 10 commits into from
Jan 27, 2022
12 changes: 11 additions & 1 deletion ethers-providers/src/transports/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use ethers_core::types::U256;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;
use std::{fmt, string::ToString};
use thiserror::Error;

#[derive(Serialize, Deserialize, Debug, Clone, Error)]
Expand Down Expand Up @@ -103,6 +103,16 @@ pub enum Authorization {
Bearer(String),
}

impl Authorization {
pub fn basic(username: impl ToString, password: impl ToString) -> Self {
th4s marked this conversation as resolved.
Show resolved Hide resolved
Self::Basic(username.to_string(), password.to_string())
}

pub fn bearer(token: impl ToString) -> Self {
th4s marked this conversation as resolved.
Show resolved Hide resolved
Self::Bearer(token.to_string())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion ethers-providers/src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Provider {
/// use url::Url;
///
/// let url = Url::parse("http://localhost:8545").unwrap();
/// let provider = Http::new_with_auth(url, Authorization::Basic("admin".into(), "good_password".into()));
/// let provider = Http::new_with_auth(url, Authorization::basic("admin", "good_password"));
/// ```
pub fn new_with_auth(url: impl Into<Url>, auth: Authorization) -> Self {
let mut provider = Self::new(url);
Expand Down