Code Monkey home page Code Monkey logo

iron-auth's Introduction

iron-auth Build Status

Under development!

Authentication middleware for the Iron web framework.

Allows you to wrap iron routes to protect it and store data in a session for each user.

Uses JWT for bearer tokens and Redis as sessions storage.

Example

extern crate iron;
extern crate router;
extern crate iron_auth;

use iron::prelude::*;
use iron::status;
use router::Router;
use iron_auth::{AuthConfigMiddleware, AuthWrapper, Claims, AuthReqExt};

fn without_token(req: &mut Request) -> IronResult<Response> {
    Ok(Response::with((status::Ok, "Public info")))
}

fn with_token(req: &mut Request) -> IronResult<Response> {
    let session = req.session().unwrap();
    let claims = session.get_claims();
    // let new_token = req.create_token(Claims { ... }).unwrap();
    // ...
    Ok(Response::with((status::Ok, "Private info")))
}

fn main() {
    let mut router = Router::new();
    router.get("/without_token", AuthWrapper::wrap(without_token), "without_token");
    router.get("/with_token", AuthWrapper::wrap(with_token), "with_token");

    let mut chain = Chain::new(router);
    chain.link_before(AuthConfigMiddleware::new(
        "secret".to_string(),
        "redis://localhost",
    ));

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

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.