mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-30 10:42:55 +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]
|
||||
actix-service = "0.3.4"
|
||||
actix-http = { path = "../actix-http/" }
|
||||
base64 = "0.10.1"
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
log =" 0.4"
|
||||
|
@ -242,6 +242,30 @@ impl ClientRequest {
|
||||
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")]
|
||||
/// Set a cookie
|
||||
///
|
||||
|
@ -1,17 +1,14 @@
|
||||
use std::io::{Read, Write};
|
||||
use std::{net, thread};
|
||||
use std::io::Write;
|
||||
|
||||
use brotli2::write::BrotliEncoder;
|
||||
use bytes::Bytes;
|
||||
use flate2::write::{GzEncoder, ZlibEncoder};
|
||||
use flate2::write::GzEncoder;
|
||||
use flate2::Compression;
|
||||
use futures::stream::once;
|
||||
use futures::Future;
|
||||
use rand::Rng;
|
||||
|
||||
use actix_http::HttpService;
|
||||
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 \
|
||||
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!"));
|
||||
// }
|
||||
|
||||
// #[test]
|
||||
// fn client_basic_auth() {
|
||||
// let mut srv =
|
||||
// test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
||||
// /// set authorization header to Basic <base64 encoded username:password>
|
||||
// let request = srv
|
||||
// .get()
|
||||
// .basic_auth("username", Some("password"))
|
||||
// .finish()
|
||||
// .unwrap();
|
||||
// let repr = format!("{:?}", request);
|
||||
// assert!(repr.contains("Basic dXNlcm5hbWU6cGFzc3dvcmQ="));
|
||||
// }
|
||||
#[test]
|
||||
fn client_basic_auth() {
|
||||
let mut srv = TestServer::new(|| {
|
||||
HttpService::new(App::new().route(
|
||||
"/",
|
||||
web::to(|req: HttpRequest| {
|
||||
if req
|
||||
.headers()
|
||||
.get(header::AUTHORIZATION)
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
== "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
|
||||
{
|
||||
HttpResponse::Ok()
|
||||
} else {
|
||||
HttpResponse::BadRequest()
|
||||
}
|
||||
}),
|
||||
))
|
||||
});
|
||||
|
||||
// #[test]
|
||||
// fn client_bearer_auth() {
|
||||
// let mut srv =
|
||||
// test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
||||
// /// set authorization header to Bearer <token>
|
||||
// let request = srv
|
||||
// .get()
|
||||
// .bearer_auth("someS3cr3tAutht0k3n")
|
||||
// .finish()
|
||||
// .unwrap();
|
||||
// let repr = format!("{:?}", request);
|
||||
// assert!(repr.contains("Bearer someS3cr3tAutht0k3n"));
|
||||
// }
|
||||
// set authorization header to Basic <base64 encoded username:password>
|
||||
let request = srv.get().basic_auth("username", Some("password"));
|
||||
let response = srv.block_on(request.send()).unwrap();
|
||||
assert!(response.status().is_success());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn client_bearer_auth() {
|
||||
let mut srv = TestServer::new(|| {
|
||||
HttpService::new(App::new().route(
|
||||
"/",
|
||||
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