Skip to content

User activation logic and UI. r=fabrice #526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ foxbox_taxonomy = { path = "components/taxonomy/" }
openzwave-adapter = { path = "components/openzwave-adapter/", optional = true }
tls = { path = "components/tls/" }

foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "ec53c3a" }
foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "9edc24c" }
iron-cors = { git = "https://github.com/fxbox/iron-cors.git", rev = "f397cd2" }
multicast_dns = { git = "https://github.com/fxbox/multicast-dns.git", rev = "a6e4bcc" }

Expand Down
2 changes: 1 addition & 1 deletion components/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ features = ["ssl"]

[dependencies]
clippy = "0.0.76"
foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "ec53c3a" }
foxbox_users = { git = "https://github.com/fxbox/users.git", rev = "9edc24c" }
hyper = "0.8.1"
libc = "0.2.7"
log = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions components/core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_json;
use std::io;
use std::net::SocketAddr;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::sync::{ Arc, RwLock };
use std::vec::IntoIter;
use tls::{ CertificateRecord, CertificateManager };
use upnp::UpnpManager;
Expand All @@ -34,6 +34,6 @@ pub trait Controller : Send + Sync + Clone + Reflect + 'static {

fn get_config(&self) -> Arc<ConfigService>;
fn get_upnp_manager(&self) -> Arc<UpnpManager>;
fn get_users_manager(&self) -> Arc<UsersManager>;
fn get_users_manager(&self) -> Arc<RwLock<UsersManager>>;
fn get_profile(&self) -> &ProfileService;
}
8 changes: 4 additions & 4 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::io;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use std::sync::{ Arc, Mutex };
use std::sync::{ Arc, Mutex, RwLock };
use std::sync::atomic::{ AtomicBool, Ordering };
use std::vec::IntoIter;
use tls::{ CertificateManager, CertificateRecord, SniSslContextProvider, TlsOption };
Expand All @@ -36,7 +36,7 @@ pub struct FoxBox {
websockets: Arc<Mutex<HashMap<ws::util::Token, ws::Sender>>>,
pub config: Arc<ConfigService>,
upnp: Arc<UpnpManager>,
users_manager: Arc<UsersManager>,
users_manager: Arc<RwLock<UsersManager>>,
profile_service: Arc<ProfileService>,
}

Expand Down Expand Up @@ -64,7 +64,7 @@ impl FoxBox {
ws_port: ws_port,
config: config,
upnp: Arc::new(UpnpManager::new()),
users_manager: Arc::new(UsersManager::new(&profile_service.path_for("users_db.sqlite"))),
users_manager: Arc::new(RwLock::new(UsersManager::new(&profile_service.path_for("users_db.sqlite")))),
profile_service: Arc::new(profile_service)
}
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Controller for FoxBox {
self.upnp.clone()
}

fn get_users_manager(&self) -> Arc<UsersManager> {
fn get_users_manager(&self) -> Arc<RwLock<UsersManager>> {
self.users_manager.clone()
}

Expand Down
3 changes: 2 additions & 1 deletion src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ impl<T: Controller> HttpServer<T> {
adapter_api);

let users_manager = self.controller.get_users_manager();
let guard = users_manager.read().unwrap();
let mut mount = Mount::new();
mount.mount("/", static_router::create(users_manager.clone()))
.mount("/ping", Ping)
.mount("/api/v1", taxonomy_chain)
.mount("/users", users_manager.get_router_chain());
.mount("/users", guard.get_router_chain());

let mut chain = Chain::new(mount);
chain.link_after(Custom404);
Expand Down
44 changes: 37 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ Options:
--dns-domain <domain> Set the top level domain for public DNS [default: box.knilxof.org]
--dns-api <url> Set the DNS API endpoint [default: https://knilxof.org:5300]
-c, --config <namespace;key;value> Set configuration override
-h, --help Print this help menu.
-e, --email-server <url> User email server URL. [default: http://knilxof.org:4000]
-h, --help Print this help menu.
",
flag_local_name: String,
flag_port: u16,
Expand All @@ -139,7 +140,8 @@ Options:
flag_disable_tls: bool,
flag_dns_domain: String,
flag_dns_api: String,
flag_config: Option<Vec<String>>);
flag_config: Option<Vec<String>>,
flag_email_server: String);

/// Updates local host name with the provided host name string. If requested host name
/// is not available (used by anyone else on the same network) then collision
Expand Down Expand Up @@ -292,17 +294,44 @@ fn main() {

// Start the tunnel.
let mut tunnel: Option<Tunnel> = None;
if let Some(tunnel_url) = args.flag_tunnel {
tunnel = Some(Tunnel::new(TunnelConfig::new(tunnel_url,
if let Some(ref tunnel_url) = args.flag_tunnel {
tunnel = Some(Tunnel::new(TunnelConfig::new(tunnel_url.to_owned(),
args.flag_tunnel_secret,
args.flag_port,
args.flag_wsport,
registrar.get_remote_dns_name())));
registrar.clone().get_remote_dns_name())));
tunnel.as_mut().unwrap().start().unwrap();
}

registrar.start(args.flag_iface, &tunnel,
args.flag_port, &controller);
registrar.clone().start(args.flag_iface, &tunnel,
args.flag_port, &controller);

let scheme = if controller.get_tls_enabled() {
String::from("https://")
} else {
String::from("http://")
};

// If remote access is enabled, we use the remote address.
// Otherwise, we have to fallback to the local address and hope that the
// user clicks on the invitation link while she is connected to the same
// network foxbox is connected to.
let dns_name = match args.flag_tunnel {
Some(_) => registrar.get_remote_dns_name(),
None => registrar.get_local_dns_name()
};

let invitation_prepath = format!("{}{}?activation_url={}{}/users",
scheme, dns_name, scheme, dns_name);

debug!("email_server {} invitation prepath {}",
args.flag_email_server, invitation_prepath);

let manager = controller.get_users_manager().clone();
manager.write().unwrap().setup_invitation_middleware(
&args.flag_email_server,
&invitation_prepath
);

controller.run(&SHUTDOWN_FLAG);

Expand All @@ -329,6 +358,7 @@ describe! main {
assert_eq!(args.flag_iface, None);
assert_eq!(args.flag_tunnel, None);
assert_eq!(args.flag_config, None);
assert_eq!(args.flag_email_server, "http://knilxof.org:4000");
assert_eq!(args.flag_help, false);
}

Expand Down
8 changes: 4 additions & 4 deletions src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
/// after trying more aggressively at first run.

extern crate get_if_addrs;
extern crate hyper;

use self::hyper::Client;
use self::hyper::header::Connection;
use self::hyper::status::StatusCode;
use hyper::Client;
use hyper::header::Connection;
use hyper::status::StatusCode;
use self::get_if_addrs::{ IfAddr, Interface };
use foxbox_core::traits::Controller;
use serde_json;
Expand All @@ -23,6 +22,7 @@ use tunnel_controller:: { Tunnel };

const REGISTRATION_INTERVAL_IN_MINUTES: u32 = 1;

#[derive(Clone)]
pub struct Registrar {
certificate_manager: CertificateManager,
top_level_domain: String,
Expand Down
12 changes: 6 additions & 6 deletions src/static_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iron::status;
use router::Router;
use staticfile::Static;
use std::path::Path;
use std::sync::Arc;
use std::sync::{ Arc, RwLock };

fn handler(req: &mut Request, db: &UsersDb) -> IronResult<Response> {
let handler = match db.read(ReadFilter::IsAdmin(true)) {
Expand All @@ -27,15 +27,15 @@ fn handler(req: &mut Request, db: &UsersDb) -> IronResult<Response> {
Handler::handle(&handler, req)
}

pub fn create(manager: Arc<UsersManager>) -> Router {
pub fn create(manager: Arc<RwLock<UsersManager>>) -> Router {
let mut router = Router::new();
let usersmanager = manager.clone();
let cloned = manager.clone();
router.any("", move |req: &mut Request| -> IronResult<Response> {
handler(req, &usersmanager.get_db())
handler(req, &cloned.read().unwrap().get_db())
});
let usersmanager = manager.clone();
let manager = manager.clone();
router.any("*", move |req: &mut Request| -> IronResult<Response> {
handler(req, &usersmanager.get_db())
handler(req, &manager.read().unwrap().get_db())
});
router
}
7 changes: 4 additions & 3 deletions src/stubs/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::io;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::{ Arc, RwLock };
use std::sync::atomic::AtomicBool;
use tls::{ CertificateManager, CertificateRecord, SniSslContextProvider };
use ws;
Expand Down Expand Up @@ -67,8 +67,9 @@ impl Controller for ControllerStub {
fn get_upnp_manager(&self) -> Arc<UpnpManager> {
Arc::new(UpnpManager::new())
}
fn get_users_manager(&self) -> Arc<UsersManager> {
Arc::new(UsersManager::new(&self.profile_service.path_for("unused")))
fn get_users_manager(&self) -> Arc<RwLock<UsersManager>> {
Arc::new(RwLock::new(
UsersManager::new(&self.profile_service.path_for("unused"))))
}
fn get_profile(&self) -> &ProfileService {
&self.profile_service
Expand Down
3 changes: 2 additions & 1 deletion src/taxonomy_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ pub fn create<T>(controller: T, adapter_api: &Arc<AdapterManager>) -> Chain
};

let mut chain = Chain::new(router);
chain.around(controller.get_users_manager().get_middleware(auth_endpoints));
let manager = controller.get_users_manager().clone();
chain.around(manager.read().unwrap().get_middleware(auth_endpoints));

chain
}
Expand Down
3 changes: 2 additions & 1 deletion src/ws_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ impl<T: Controller> Handler for WsHandler<T> {
_ => return self.close_with_error("Missing authorization"),
};

if let Err(_) = self.controller.get_users_manager().verify_token(&token) {
let manager = self.controller.get_users_manager().clone();
if let Err(_) = manager.read().unwrap().verify_token(&token) {
return self.close_with_error("Authorization failed");
}

Expand Down
31 changes: 31 additions & 0 deletions static/main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@
</div>
</div>
</div>

<div id="signup" hidden>
<div class="container">
<div class="inner-container">
<p>Welcome to Link</p>
<form action="#">
<p>
<label for="signup-name">
Enter your user name:
</label>
<input type="text" id="signup-name" autofocus>
</p>
<p>
<label for="signup-pwd1">
Please choose a password (8 characters minimum):
</label>
<input type="password" id="signup-pwd1">
</p>
<p>
<label for="signup-pwd2">
Please repeat your password:
</label>
<input type="password" id="signup-pwd2">
</p>
<p>
<button id="signup-button">Sign up</button>
</p>
</form>
</div>
</div>
</div>
</body>
<script src="shared/bower_components/fetch/fetch.js"></script>
<script src="shared/js/url_search_params.js"></script>
Expand Down
Loading