Skip to content

Commit 60baf59

Browse files
committed
builder httpclient for auth
1 parent 07533e2 commit 60baf59

File tree

3 files changed

+21
-29
lines changed

3 files changed

+21
-29
lines changed

client/src/auth.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use base64::Engine;
2-
use hyper::{Body, HeaderMap, Request, Response, StatusCode};
2+
use hyper::{http::HeaderValue, Body, HeaderMap, Request, Response, StatusCode};
3+
use jsonrpsee::{
4+
core::ClientError,
5+
http_client::{HttpClient, HttpClientBuilder},
6+
};
37
use std::{
48
error::Error,
59
future::Future,
@@ -99,3 +103,12 @@ pub fn auth_token_from_cookie(cookie: &str) -> String {
99103
pub fn auth_token_from_creds(user: &str, password: &str) -> String {
100104
base64::prelude::BASE64_STANDARD.encode(auth_cookie(user, password))
101105
}
106+
107+
pub fn http_client_with_auth(url: &str, auth_token: &str) -> Result<HttpClient, ClientError> {
108+
let mut headers = hyper::http::HeaderMap::new();
109+
headers.insert(
110+
"Authorization",
111+
HeaderValue::from_str(&format!("Basic {auth_token}")).unwrap(),
112+
);
113+
HttpClientBuilder::default().set_headers(headers).build(url)
114+
}

client/src/bin/space-cli.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ use domain::{
1616
};
1717
use jsonrpsee::{
1818
core::{client::Error, ClientError},
19-
http_client::{HttpClient, HttpClientBuilder},
19+
http_client::HttpClient,
2020
};
2121
use serde::{Deserialize, Serialize};
2222
use spaces_client::{
23-
auth::{auth_token_from_cookie, auth_token_from_creds},
23+
auth::{auth_token_from_cookie, auth_token_from_creds, http_client_with_auth},
2424
config::{default_cookie_path, default_spaces_rpc_port, ExtendedNetwork},
2525
deserialize_base64,
2626
format::{
@@ -415,16 +415,7 @@ impl SpaceCli {
415415
})?;
416416
auth_token_from_cookie(&cookie)
417417
};
418-
let client = {
419-
let mut headers = hyper::http::HeaderMap::new();
420-
headers.insert(
421-
"Authorization",
422-
hyper::http::HeaderValue::from_str(&format!("Basic {auth_token}")).unwrap(),
423-
);
424-
HttpClientBuilder::default()
425-
.set_headers(headers)
426-
.build(args.spaced_rpc_url.clone().unwrap())?
427-
};
418+
let client = http_client_with_auth(args.spaced_rpc_url.as_ref().unwrap(), &auth_token)?;
428419

429420
Ok((
430421
Self {

testutil/src/spaced.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ use anyhow::Result;
88
use assert_cmd::cargo::CommandCargoExt;
99
use bitcoind::{anyhow, anyhow::anyhow, get_available_port, tempfile::tempdir};
1010
use spaces_client::{
11-
auth::auth_token_from_creds,
12-
jsonrpsee::{
13-
http_client::{HttpClient, HttpClientBuilder},
14-
tokio,
15-
},
11+
auth::{auth_token_from_creds, http_client_with_auth},
12+
jsonrpsee::{http_client::HttpClient, tokio},
1613
log::{debug, error},
1714
rpc::RpcClient,
1815
};
@@ -64,17 +61,8 @@ impl SpaceD {
6461
.await
6562
.expect("spawn blocking task")?;
6663

67-
let client = {
68-
let auth_token = auth_token_from_creds("user", "pass");
69-
let mut headers = hyper::http::HeaderMap::new();
70-
headers.insert(
71-
"Authorization",
72-
hyper::http::HeaderValue::from_str(&format!("Basic {auth_token}")).unwrap(),
73-
);
74-
HttpClientBuilder::default()
75-
.set_headers(headers)
76-
.build(rpc_url(rpc_port))?
77-
};
64+
let client =
65+
http_client_with_auth(&rpc_url(rpc_port), &auth_token_from_creds("user", "pass"))?;
7866

7967
let mut spaced = Self {
8068
process,

0 commit comments

Comments
 (0)