Skip to content

Commit

Permalink
Merge 097982a into b0388c6
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphan Kochen committed Dec 4, 2017
2 parents b0388c6 + 097982a commit 0cdf934
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 199 deletions.
378 changes: 210 additions & 168 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 16 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ path = "src/main.rs"
glob = "0.2.11"

[dependencies]
base64 = "0.7.0"
base64 = "0.8.0"
docopt = "0.8.1"
env_logger = "0.4.3"
futures = "0.1.14"
futures = "0.1.17"
gettext = "0.2.0"
hyper = "0.11.2"
hyper = "0.11.7"
hyper-staticfile = "0.1.1"
hyper-tls = "0.1.2"
lettre = "0.6.2"
log = "0.3.6"
mustache = "0.8.0"
openssl = "0.9.15"
rand = "0.3.15"
lettre = "0.7.0"
lettre_email = "0.7.0"
log = "0.3.8"
mustache = "0.8.2"
openssl = "0.9.22"
rand = "0.3.18"
redis = "0.8.0"
serde = "1.0.11"
serde_derive = "1.0.11"
serde_json = "1.0.2"
time = "0.1.35"
tokio-core = "0.1.9"
toml = "0.4.4"
url = "1.5.1"
serde = "1.0.23"
serde_derive = "1.0.23"
serde_json = "1.0.7"
time = "0.1.38"
tokio-core = "0.1.10"
toml = "0.4.5"
url = "1.6.0"
20 changes: 11 additions & 9 deletions src/bridges/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use futures::future;
use http::{ContextHandle, HandlerResult};
use hyper::Response;
use hyper::header::ContentType;
use lettre::email::EmailBuilder;
use lettre::transport::EmailTransport;
use lettre::transport::smtp::SmtpTransportBuilder;
use lettre_email::EmailBuilder;
use lettre::EmailTransport;
use lettre::smtp::{ClientSecurity, SmtpTransportBuilder};
use lettre::smtp::authentication::Credentials;
use std::rc::Rc;
use url::percent_encoding::{utf8_percent_encode, QUERY_ENCODE_SET};

Expand Down Expand Up @@ -60,20 +61,21 @@ pub fn auth(ctx_handle: &ContextHandle, email_addr: &Rc<EmailAddress>) -> Handle
EmailBuilder::new()
.to(email_addr.as_str())
.from((&*ctx.app.from_address, &*ctx.app.from_name))
.alternative(&ctx.app.templates.email_html.render(params),
&ctx.app.templates.email_text.render(params))
.subject(&[catalog.gettext("Finish logging in to"), display_origin.as_str()].join(" "))
.alternative(ctx.app.templates.email_html.render(params),
ctx.app.templates.email_text.render(params))
.subject([catalog.gettext("Finish logging in to"), display_origin.as_str()].join(" "))
.build()
.unwrap_or_else(|err| panic!("unhandled error building email: {}", err))
};
let mut builder = match SmtpTransportBuilder::new(&ctx.app.smtp_server) {
// TODO: Configurable security.
let mut builder = match SmtpTransportBuilder::new(&ctx.app.smtp_server, ClientSecurity::None) {
Ok(builder) => builder,
Err(err) => return Box::new(future::err(BrokerError::Internal(
format!("could not create the smtp transport: {}", err)))),
};

if let (&Some(ref username), &Some(ref password)) = (&ctx.app.smtp_username, &ctx.app.smtp_password) {
builder = builder.credentials(username, password);
builder = builder.credentials(Credentials::new(username.to_owned(), password.to_owned()));
}

// Store the code in the session for use in the verify handler. We should never fail to claim
Expand All @@ -89,7 +91,7 @@ pub fn auth(ctx_handle: &ContextHandle, email_addr: &Rc<EmailAddress>) -> Handle

// Send the mail.
let mut mailer = builder.build();
if let Err(err) = mailer.send(email) {
if let Err(err) = mailer.send(&email) {
return Box::new(future::err(BrokerError::Internal(
format!("could not send mail: {}", err))))
}
Expand Down
2 changes: 1 addition & 1 deletion src/bridges/oidc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use webfinger::{Link, Relation};


/// The origin of the Google identity provider.
pub const GOOGLE_IDP_ORIGIN: &'static str = "https://accounts.google.com";
pub const GOOGLE_IDP_ORIGIN: &str = "https://accounts.google.com";


/// Normalization to apply to an email address.
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub struct I18n {
}


const SUPPORTED_LANGUAGES: &'static [&'static str] = &["en", "de", "nl"];
const SUPPORTED_LANGUAGES: &[&str] = &["en", "de", "nl"];

impl Default for I18n {
fn default() -> I18n {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub fn nonce() -> String {
/// Helper function to create a random string consisting of
/// characters from the z-base-32 set.
pub fn random_zbase32(len: usize) -> String {
const CHARSET: &'static [u8] = b"13456789abcdefghijkmnopqrstuwxyz";
const CHARSET: &[u8] = b"13456789abcdefghijkmnopqrstuwxyz";
String::from_utf8((0..len).map(|_| {
CHARSET[random::<usize>() % CHARSET.len()]
}).collect()).expect("failed to build one-time pad")
Expand Down
1 change: 1 addition & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod auth;
pub mod normalize;
pub mod pages;
30 changes: 30 additions & 0 deletions src/handlers/normalize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use email_address::EmailAddress;
use error::BrokerError;
use futures::future;
use http::{ContextHandle, HandlerResult};
use hyper::header::{CacheControl, CacheDirective, ContentType};
use hyper::server::Response;

/// Request handler for the email normalization endpoint.
///
/// Performs normalization of email addresses, for clients that cannot implement all the necessary
/// parts of the relevant specifications. (Unicode, WHATWG, etc.)
pub fn normalize(ctx_handle: &ContextHandle) -> HandlerResult {
let mut ctx = ctx_handle.borrow_mut();

let input = try_get_input_param!(ctx, "email");
let parsed = match input.parse::<EmailAddress>() {
Ok(addr) => addr,
Err(_) => return Box::new(future::err(BrokerError::Input(
"not a valid email address".to_owned()))),
};

let res = Response::new()
.with_header(ContentType::plaintext())
.with_header(CacheControl(vec![
CacheDirective::NoCache,
CacheDirective::NoStore,
]))
.with_body(parsed.to_string());
Box::new(future::ok(res))
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate hyper;
extern crate hyper_staticfile;
extern crate hyper_tls;
extern crate lettre;
extern crate lettre_email;
#[macro_use]
extern crate log;
extern crate mustache;
Expand Down Expand Up @@ -56,6 +57,7 @@ fn router(req: &Request) -> Option<http::Handler> {
(&Method::Get, "/.well-known/openid-configuration") => handlers::auth::discovery,
(&Method::Get, "/keys.json") => handlers::auth::key_set,
(&Method::Get, "/auth") | (&Method::Post, "/auth") => handlers::auth::auth,
(&Method::Get, "/normalize") => handlers::normalize::normalize,

// Identity provider endpoints
(&Method::Get, "/callback") => bridges::oidc::fragment_callback,
Expand All @@ -75,13 +77,13 @@ fn router(req: &Request) -> Option<http::Handler> {


/// Defines the program's version, as set by Cargo at compile time.
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const VERSION: &str = env!("CARGO_PKG_VERSION");


/// Defines the program's usage string.
///
/// [Docopt](http://docopt.org) parses this and generates a custom argv parser.
const USAGE: &'static str = r#"
const USAGE: &str = r#"
Portier Broker
Usage:
Expand Down
4 changes: 2 additions & 2 deletions src/webfinger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use url::Url;


/// Portier webfinger relation
pub const WEBFINGER_PORTIER_REL: &'static str = "https://portier.io/specs/auth/1.0/idp";
pub const WEBFINGER_PORTIER_REL: &str = "https://portier.io/specs/auth/1.0/idp";
/// Portier + Google webfinger relation
pub const WEBFINGER_GOOGLE_REL: &'static str = "https://portier.io/specs/auth/1.0/idp/google";
pub const WEBFINGER_GOOGLE_REL: &str = "https://portier.io/specs/auth/1.0/idp/google";


/// Deserialization types
Expand Down

0 comments on commit 0cdf934

Please sign in to comment.