Skip to content

Commit

Permalink
Merge c256c62 into 2f6d155
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphan Kochen committed Aug 9, 2017
2 parents 2f6d155 + c256c62 commit 9e8ecd4
Show file tree
Hide file tree
Showing 23 changed files with 1,523 additions and 1,193 deletions.
929 changes: 512 additions & 417 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ glob = "0.2.11"
docopt = "0.6.86"
emailaddress = "0.4.0"
env_logger = "0.3.5"
hyper = "0.9.14"
iron = "0.4.0"
lettre = "0.6.1"
futures = "0.1.14"
gettext = "0.2.0"
hyper = "0.11.2"
hyper-staticfile = "0.1.1"
hyper-tls = "0.1.2"
lettre = "0.6.2"
log = "0.3.6"
mustache = "0.8.0"
openssl = "0.7.14"
openssl = "0.9.15"
rand = "0.3.15"
redis = "0.8.0"
router = "0.4.0"
rustc-serialize = "0.3.22"
serde = "0.9.7"
serde_derive = "0.9.7"
serde_json = "0.9.6"
staticfile = { version = "0.3.1", features = ["cache"] }
time = "0.1.35"
tokio-core = "0.1.9"
toml = "0.3.0"
url = "1.2.4"
urlencoded = "0.4.1"
gettext = "0.2.0"
url = "1.5.1"
15 changes: 7 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,13 @@ keytext BROKER_KEYTEXT (none)

**[redis] section:**

================== ========================= ================
``config.toml`` Environment Variable Default
================== ========================= ================
url BROKER_REDIS_URL (none)
session_ttl BROKER_SESSION_TTL 900 (15 minutes)
cache_ttl BROKER_CACHE_TTL 3600 (1 hour)
cache_max_doc_size BROKER_CACHE_MAX_DOC_SIZE 8096 (8 KiB)
================== ========================= ================
=============== ==================== ================
``config.toml`` Environment Variable Default
=============== ==================== ================
url BROKER_REDIS_URL (none)
session_ttl BROKER_SESSION_TTL 900 (15 minutes)
cache_ttl BROKER_CACHE_TTL 3600 (1 hour)
=============== ==================== ================

**[smtp] section:**

Expand Down
22 changes: 10 additions & 12 deletions config.toml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ listen_ip = "127.0.0.1"
# Port to bind to - Default: 3333
listen_port = 3333
# Server's user-facing URL (mandatory, CHANGE THIS)
public_url =
public_url =
# Whitelist of client origins to allow - Default: (none (unrestricted))
allowed_origins =
allowed_origins =


[headers]
Expand All @@ -24,31 +24,29 @@ token_ttl = 600
# Array of file paths to PEM-formatted signing keys - (mandatory, CHANGE THIS)
keyfiles = []
# Text of the key file in PEM format (set either this or keyfiles) - Default: (none)
keytext =
keytext =


[redis]
# Redis connection URL - Default: (mandatory, CHANGE THIS)
url =
url =
# Time that users have to complete authentication - Default: 900 (15 minutes)
session_ttl = 900
# Time that provider discovery documents are cached - Default: 3600 (1 hour)
cache_ttl = 3600
# Maximum size of each cached discovery document - Default: 8096 (8 KiB)
cache_max_doc_size = 8096


[smtp]
# Display name for confirmation emails - Default: "Portier"
from_name = "Portier"
# Sender address for confirmation emails - (mandatory, CHANGE THIS)
from_address =
from_address =
# Outgoing mailserver address - (mandatory, CHANGE THIS)
server =
server =
# Outgoing mailserver username - Default: (none)
username =
username =
# Outgoing mailserver password - Default: (none)
password =
password =


[limit]
Expand All @@ -59,9 +57,9 @@ per_email = "5/min"

[providers."gmail.com"]
# Google OAuth API Client ID - Default: (none)
client_id =
client_id =
# Google OAuth API Secret Key - Default: (none)
secret =
secret =
# Google OpenID Connect discovery URL - Default: "https://accounts.google.com/.well-known/openid-configuration"
discovery_url = "https://accounts.google.com/.well-known/openid-configuration"
# Google OpenID Connect id_token issuer domain - Default: "accounts.google.com"
Expand Down
42 changes: 24 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#![allow(unknown_lints, cyclomatic_complexity)]

extern crate serde;
extern crate toml;

use std;
use crypto;
use gettext::Catalog;
use hyper;
use hyper::header::LanguageTag;
use hyper_tls::HttpsConnector;
use mustache;
use std::collections::HashMap;
use std::env;
use std::fmt::{self, Display};
use std::error::Error;
use std::fmt::{self, Display};
use std::fs::File;
use std::io::Read;
use std;
use store;
use store_limits::Ratelimit;
use tokio_core::reactor::Handle;

use toml;

use super::{crypto, store, mustache};
use super::gettext::Catalog;
use super::hyper::LanguageTag;
use super::store_limits::Ratelimit;

/// The type of HTTP client we use, with TLS enabled.
pub type HttpClient = hyper::Client<HttpsConnector<hyper::client::HttpConnector>>;


/// Union of all possible error types seen while parsing.
Expand Down Expand Up @@ -168,6 +175,7 @@ pub struct Config {
pub token_ttl: u16,
pub keys: Vec<crypto::NamedKey>,
pub store: store::Store,
pub http_client: HttpClient,
pub from_name: String,
pub from_address: String,
pub smtp_server: String,
Expand Down Expand Up @@ -195,7 +203,6 @@ pub struct ConfigBuilder {
pub redis_url: Option<String>,
pub redis_session_ttl: u16,
pub redis_cache_ttl: u16,
pub redis_cache_max_doc_size: u16,
pub from_name: String,
pub from_address: Option<String>,
pub smtp_server: Option<String>,
Expand Down Expand Up @@ -266,7 +273,6 @@ impl ConfigBuilder {
redis_url: None,
redis_session_ttl: 900,
redis_cache_ttl: 3600,
redis_cache_max_doc_size: 8096,
from_name: "Portier".to_string(),
from_address: None,
smtp_username: None,
Expand Down Expand Up @@ -308,7 +314,6 @@ impl ConfigBuilder {
self.redis_url = table.url.or_else(|| self.redis_url.clone());
if let Some(val) = table.session_ttl { self.redis_session_ttl = val; }
if let Some(val) = table.cache_ttl { self.redis_cache_ttl = val; }
if let Some(val) = table.cache_max_doc_size { self.redis_cache_max_doc_size = val; }
}

if let Some(table) = toml_config.smtp {
Expand Down Expand Up @@ -381,7 +386,6 @@ impl ConfigBuilder {
if let Some(val) = env_config.broker_redis_url { self.redis_url = Some(val); }
if let Some(val) = env_config.broker_session_ttl { self.redis_session_ttl = val; }
if let Some(val) = env_config.broker_cache_ttl { self.redis_cache_ttl = val; }
if let Some(val) = env_config.broker_cache_max_doc_size { self.redis_cache_max_doc_size = val; }

if let Some(val) = env_config.broker_from_name { self.from_name = val; }
if let Some(val) = env_config.broker_from_address { self.from_address = Some(val); }
Expand All @@ -405,7 +409,7 @@ impl ConfigBuilder {
self
}

pub fn done(self) -> Result<Config, ConfigError> {
pub fn done(self, handle: &Handle) -> Result<Config, ConfigError> {
// Additional validations
if self.smtp_username.is_none() != self.smtp_password.is_none() {
return Err(ConfigError::Custom(
Expand All @@ -428,9 +432,13 @@ impl ConfigBuilder {
&self.redis_url.expect("no redis url configured"),
self.redis_cache_ttl as usize,
self.redis_session_ttl as usize,
self.redis_cache_max_doc_size as u64,
).expect("unable to instantiate new redis store");

let http_connector = HttpsConnector::new(4, handle)
.expect("could not initialize https connector");
let http_client = hyper::Client::configure()
.connector(http_connector).build(handle);

let idx = self.limit_per_email.find('/')
.expect("unable to parse limit.per_email format");
let (count, unit) = self.limit_per_email.split_at(idx);
Expand Down Expand Up @@ -460,6 +468,7 @@ impl ConfigBuilder {
token_ttl: self.token_ttl,
keys: keys,
store: store,
http_client: http_client,
from_name: self.from_name,
from_address: self.from_address.expect("no smtp from address configured"),
smtp_server: self.smtp_server.expect("no smtp outserver address configured"),
Expand Down Expand Up @@ -513,7 +522,6 @@ struct TomlRedisTable {
url: Option<String>,
session_ttl: Option<u16>,
cache_ttl: Option<u16>,
cache_max_doc_size: Option<u16>,
}

#[derive(Clone,Debug,Deserialize)]
Expand Down Expand Up @@ -558,7 +566,6 @@ struct EnvConfig {
broker_redis_url: Option<String>,
broker_session_ttl: Option<u16>,
broker_cache_ttl: Option<u16>,
broker_cache_max_doc_size: Option<u16>,
broker_from_name: Option<String>,
broker_from_address: Option<String>,
broker_smtp_server: Option<String>,
Expand Down Expand Up @@ -593,7 +600,6 @@ impl EnvConfig {
broker_redis_url: env::var("BROKER_REDIS_URL").ok(),
broker_session_ttl: env::var("BROKER_SESSION_TTL").ok().and_then(|x| x.parse().ok()),
broker_cache_ttl: env::var("BROKER_CACHE_TTL").ok().and_then(|x| x.parse().ok()),
broker_cache_max_doc_size: env::var("BROKER_CACHE_MAX_DOC_SIZE").ok().and_then(|x| x.parse().ok()),

broker_from_name: env::var("BROKER_FROM_NAME").ok(),
broker_from_address: env::var("BROKER_FROM_ADDRESS").ok(),
Expand Down
Loading

0 comments on commit 9e8ecd4

Please sign in to comment.