Code Monkey home page Code Monkey logo

users's Introduction

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.9.x nightly is required

$ rustc -V
rustc 1.9.0-nightly (998a6720b 2016-03-07)

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

$ multirust override nightly-2016-03-07

Exposing the HTTP API

extern crate foxbox_users;
extern crate iron;

use foxbox_users::UsersManager;
use iron::prelude::*;

fn main() {
    let manager = UsersManager::new("sqlite_db.sqlite");
    let router = manager.get_router_chain();
    Iron::new(router).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("[email protected]")
        .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 [email protected]:<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.

users's People

Contributors

ferjm avatar arcturus avatar delapuente avatar hfiguiere avatar jedireza avatar azasypkin avatar fabricedesre avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.