Skip to content

fxbox/users

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License

Project Link Users

This crate implements a user management and authentication library for the first iteration of Project Link. It is currently used by FoxBox, the core of Project Link. It intentionally allows only the registration and authentication of a single user, although the database module is ready for a multi-user scenario. The crate is being built with Project Link's requirements in mind, but it is completely independent from it and could be reused and extended for other purposes.

The main modules that this crate exposes are:

  • users_router: Allows the extension of an Iron based server with the user management routes. You can read more about the HTTP API that is exposed on these routes on the API documentation.
  • users_db: This module allows direct modification of the users' database. Based on rusqlite.
  • auth_middleware: Iron middleware that allows the authentication of specific endpoints.

Usage

Rust

Currently v1.10.x nightly is required

$ rustc -V
rustc 1.10.0-nightly (62e2b2fb7 2016-05-06)

It's recommended that you use multirust to install and switch between versions of Rust.

$ multirust override nightly-2016-05-07

Exposing the HTTP API

extern crate foxbox_users;
extern crate iron;

use foxbox_users::{ EmailDispatcher, UsersManager };
use iron::prelude::*;

fn main() {
    let mut manager = UsersManager::new("sqlite_db.sqlite");
    manager.setup_invitation_middleware("http://knilxof.org:4000".to_owned(),
                                        "https://remote.whatever.knilxof.org".to_owned());
    Iron::new(manager.get_router_chain()).http("localhost:3000").unwrap();
}

Authenticating endpoints

extern crate foxbox_users;
extern crate iron;
extern crate router;

use foxbox_users::{ AuthEndpoint, UsersManager };
use iron::method::Method;
use iron::prelude::*;
use iron::status;
use router::Router;
use std::thread;
use std::time::Duration;

fn dummy_handler(_: &mut Request) -> IronResult<Response> {
    Ok(Response::with(status::Ok))
}

fn main() {
    let manager = UsersManager::new("sqlite_db.sqlite");
    let mut router =  Router::new();
    router.get("/authenticated", dummy_handler);
    router.get("/authenticated2", dummy_handler);
    router.get("/not_authenticated", dummy_handler);

    let mut chain = Chain::new(router);
    let mut middleware = manager.get_middleware(
        vec![AuthEndpoint(vec![Method::Get, Method::Delete],
                          "/authenticated".to_owned())]
    );

    chain.link_around(middleware.clone());

    thread::spawn(move || {
        println!("Adding new auth endpoint");
        thread::sleep(Duration::from_millis(3000));
        // Add new authenticated endpoints after the middleware has been given
        // to the Iron chain and the Iron server has started.
        middleware.add_auth_endpoints(vec![
             AuthEndpoint(vec![Method::Get],
                          "/authenticated2".to_owned())
        ]);
    });

    Iron::new(chain).http("localhost:3000").unwrap();
}

Direct access to users database

extern crate foxbox_users;

use foxbox_users::{ReadFilter, UserBuilder};

fn main() {
    let manager = UsersManager::new("sqlite_db.sqlite");
    let db = manager.get_db();
    let user = UserBuilder::new()
        .name("MrFox")
        .email("fox@foxlink.org")
        .password("pass12345678")
        .finalize()
        .unwrap();
    db.create(&user).unwrap();
    match db.read(ReadFilter::All) {
        Ok(users) => {
            println!("Users {:?}", users);
        },
        Err(err) => println!("{}", err)
    }
}

Contributing

Note: We're in an iterative prototyping phase of the project. Things are moving really fast so it may be easier to contribute when the dust starts to settle. You've been warned.

Forks and feature branches

You should fork the main repo and create pull requests against feature branches of your fork. If you need some guidance with this see:

Setup

$ git clone git@github.com:<username>/users.git
$ cd users

Building the lib

$ cargo build

Rust tests

$ cargo test

Documentation

$ cargo doc

Then open ./target/doc/foxbox_users/index.html. There is not online version available yet.

About

Project Link user management and authentication lib

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages