mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 18:44:35 +01:00
Added HTTP Authentication for Client #540
This commit is contained in:
parent
1cca25c276
commit
ab597dd98a
@ -41,6 +41,7 @@ flate2-rust = ["actix-http/flate2-rust"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-service = "0.3.4"
|
actix-service = "0.3.4"
|
||||||
actix-http = { path = "../actix-http/" }
|
actix-http = { path = "../actix-http/" }
|
||||||
|
base64 = "0.10.1"
|
||||||
bytes = "0.4"
|
bytes = "0.4"
|
||||||
futures = "0.1"
|
futures = "0.1"
|
||||||
log =" 0.4"
|
log =" 0.4"
|
||||||
|
@ -242,6 +242,30 @@ impl ClientRequest {
|
|||||||
self.header(header::CONTENT_LENGTH, wrt.get_mut().take().freeze())
|
self.header(header::CONTENT_LENGTH, wrt.get_mut().take().freeze())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set HTTP basic authorization
|
||||||
|
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
|
||||||
|
where
|
||||||
|
U: fmt::Display,
|
||||||
|
P: fmt::Display,
|
||||||
|
{
|
||||||
|
let auth = match password {
|
||||||
|
Some(password) => format!("{}:{}", username, password),
|
||||||
|
None => format!("{}", username),
|
||||||
|
};
|
||||||
|
self.header(
|
||||||
|
header::AUTHORIZATION,
|
||||||
|
format!("Basic {}", base64::encode(&auth)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set HTTP bearer authentication
|
||||||
|
pub fn bearer_auth<T>(self, token: T) -> Self
|
||||||
|
where
|
||||||
|
T: fmt::Display,
|
||||||
|
{
|
||||||
|
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cookies")]
|
#[cfg(feature = "cookies")]
|
||||||
/// Set a cookie
|
/// Set a cookie
|
||||||
///
|
///
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
use std::io::{Read, Write};
|
use std::io::Write;
|
||||||
use std::{net, thread};
|
|
||||||
|
|
||||||
use brotli2::write::BrotliEncoder;
|
use brotli2::write::BrotliEncoder;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
use flate2::write::GzEncoder;
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
use futures::stream::once;
|
|
||||||
use futures::Future;
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
use actix_http::HttpService;
|
use actix_http::HttpService;
|
||||||
use actix_http_test::TestServer;
|
use actix_http_test::TestServer;
|
||||||
use actix_web::{middleware, web, App, HttpRequest, HttpResponse};
|
use actix_web::{http::header, web, App, HttpMessage, HttpRequest, HttpResponse};
|
||||||
|
|
||||||
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
const STR: &str = "Hello World Hello World Hello World Hello World Hello World \
|
||||||
Hello World Hello World Hello World Hello World Hello World \
|
Hello World Hello World Hello World Hello World Hello World \
|
||||||
@ -479,30 +476,58 @@ fn test_client_brotli_encoding() {
|
|||||||
// assert_eq!(bytes, Bytes::from_static(b"welcome!"));
|
// assert_eq!(bytes, Bytes::from_static(b"welcome!"));
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// #[test]
|
#[test]
|
||||||
// fn client_basic_auth() {
|
fn client_basic_auth() {
|
||||||
// let mut srv =
|
let mut srv = TestServer::new(|| {
|
||||||
// test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
HttpService::new(App::new().route(
|
||||||
// /// set authorization header to Basic <base64 encoded username:password>
|
"/",
|
||||||
// let request = srv
|
web::to(|req: HttpRequest| {
|
||||||
// .get()
|
if req
|
||||||
// .basic_auth("username", Some("password"))
|
.headers()
|
||||||
// .finish()
|
.get(header::AUTHORIZATION)
|
||||||
// .unwrap();
|
.unwrap()
|
||||||
// let repr = format!("{:?}", request);
|
.to_str()
|
||||||
// assert!(repr.contains("Basic dXNlcm5hbWU6cGFzc3dvcmQ="));
|
.unwrap()
|
||||||
// }
|
== "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
||||||
|
{
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
});
|
||||||
|
|
||||||
// #[test]
|
// set authorization header to Basic <base64 encoded username:password>
|
||||||
// fn client_bearer_auth() {
|
let request = srv.get().basic_auth("username", Some("password"));
|
||||||
// let mut srv =
|
let response = srv.block_on(request.send()).unwrap();
|
||||||
// test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
assert!(response.status().is_success());
|
||||||
// /// set authorization header to Bearer <token>
|
}
|
||||||
// let request = srv
|
|
||||||
// .get()
|
#[test]
|
||||||
// .bearer_auth("someS3cr3tAutht0k3n")
|
fn client_bearer_auth() {
|
||||||
// .finish()
|
let mut srv = TestServer::new(|| {
|
||||||
// .unwrap();
|
HttpService::new(App::new().route(
|
||||||
// let repr = format!("{:?}", request);
|
"/",
|
||||||
// assert!(repr.contains("Bearer someS3cr3tAutht0k3n"));
|
web::to(|req: HttpRequest| {
|
||||||
// }
|
if req
|
||||||
|
.headers()
|
||||||
|
.get(header::AUTHORIZATION)
|
||||||
|
.unwrap()
|
||||||
|
.to_str()
|
||||||
|
.unwrap()
|
||||||
|
== "Bearer someS3cr3tAutht0k3n"
|
||||||
|
{
|
||||||
|
HttpResponse::Ok()
|
||||||
|
} else {
|
||||||
|
HttpResponse::BadRequest()
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
))
|
||||||
|
});
|
||||||
|
|
||||||
|
// set authorization header to Bearer <token>
|
||||||
|
let request = srv.get().bearer_auth("someS3cr3tAutht0k3n");
|
||||||
|
let response = srv.block_on(request.send()).unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user