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

Commit

Permalink
chore: Temporarily allow Authorization header (#162)
Browse files Browse the repository at this point in the history
* chore: Temporarily allow Authorization header

* Fix lint
  • Loading branch information
amaury1093 authored Dec 11, 2020
1 parent 1968524 commit 277b25a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
6 changes: 0 additions & 6 deletions migrations/2020-08-02-220301_init/down.sql

This file was deleted.

29 changes: 0 additions & 29 deletions migrations/2020-08-02-220301_init/up.sql

This file was deleted.

5 changes: 5 additions & 0 deletions src/routes/check_email/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const DEFAULT_SAASIFY_SECRET: &str = "reacher_dev_secret";
/// but there might be others in the future
#[derive(Debug, PartialEq)]
pub enum HeaderSecret {
Authorization,
Saasify,
}

Expand All @@ -40,6 +41,8 @@ fn get_saasify_secret() -> String {
/// for auth that match:
/// - `x-saasify-proxy-secret`: this means auth is handled by saasify, we don't
/// care about auth anymore.
/// - `Authorization`: this is a temporary fix to allow all requests with this
/// header.
pub fn check_header(
) -> impl warp::Filter<Extract = (HeaderSecret,), Error = warp::Rejection> + Clone {
let saasify_secret = get_saasify_secret();
Expand All @@ -48,4 +51,6 @@ pub fn check_header(

warp::header::exact_ignore_case(SAASIFY_SECRET_HEADER, saasify_secret)
.map(|| HeaderSecret::Saasify)
.or(warp::header::<String>("authorization").map(|_| HeaderSecret::Authorization))
.unify()
}
51 changes: 35 additions & 16 deletions tests/check_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use serde_json;
use warp::http::StatusCode;
use warp::test::request;

const FOO_BAR_RESPONSE: &str = r#"{"input":"foo@bar","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
const FOO_BAR_BAZ_RESPONSE: &str = r#"{"input":"foo@bar.baz","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":"foo@bar.baz","domain":"bar.baz","is_valid_syntax":true,"username":"foo"}}"#;

#[tokio::test]
async fn test_missing_header() {
let resp = request()
Expand All @@ -36,10 +39,7 @@ async fn test_missing_header() {

println!("{:?}", resp);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
assert_eq!(
resp.body(),
r#"Missing request header "x-saasify-proxy-secret""#
);
assert_eq!(resp.body(), r#"Missing request header "authorization""#);
}

#[tokio::test]
Expand All @@ -54,10 +54,7 @@ async fn test_wrong_saasify_secret() {

println!("{:?}", resp);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
assert_eq!(
resp.body(),
r#"Invalid request header "x-saasify-proxy-secret""#
);
assert_eq!(resp.body(), r#"Missing request header "authorization""#);
}

#[tokio::test]
Expand All @@ -71,10 +68,7 @@ async fn test_input_foo_bar() {
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body(),
r#"{"input":"foo@bar","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#
);
assert_eq!(resp.body(), FOO_BAR_RESPONSE);
}

#[tokio::test]
Expand All @@ -88,8 +82,33 @@ async fn test_input_foo_bar_baz() {
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(
resp.body(),
r#"{"input":"foo@bar.baz","is_reachable":"invalid","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"can_connect_smtp":false,"has_full_inbox":false,"is_catch_all":false,"is_deliverable":false,"is_disabled":false},"syntax":{"address":"foo@bar.baz","domain":"bar.baz","is_valid_syntax":true,"username":"foo"}}"#
);
assert_eq!(resp.body(), FOO_BAR_BAZ_RESPONSE);
}

#[tokio::test]
async fn test_authorization_header() {
let resp = request()
.path("/v0/check_email")
.method("POST")
.header("authorization", "foo")
.json(&serde_json::from_str::<EndpointRequest>(r#"{"to_email": "foo@bar.baz"}"#).unwrap())
.reply(&create_routes())
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body(), FOO_BAR_BAZ_RESPONSE);
}

#[tokio::test]
async fn test_authorization_capital_header() {
let resp = request()
.path("/v0/check_email")
.method("POST")
.header("Authorization", "foo")
.json(&serde_json::from_str::<EndpointRequest>(r#"{"to_email": "foo@bar.baz"}"#).unwrap())
.reply(&create_routes())
.await;

assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.body(), FOO_BAR_BAZ_RESPONSE);
}

0 comments on commit 277b25a

Please sign in to comment.