basic-auth-raw

Crates.iobasic-auth-raw
lib.rsbasic-auth-raw
version0.1.0
sourcesrc
created_at2018-05-16 09:41:25.401276
updated_at2018-05-16 09:41:25.401276
descriptionA library to provide a base for Basic Authentication for use in Rocket
homepagehttps://github.com/csharad/basic-auth-raw
repositoryhttps://github.com/csharad/basic-auth-raw
max_upload_size
id65662
size6,650
Sharad Chand (pepsighan)

documentation

https://docs.rs/basic-auth-raw

README

Raw Basic Authentication

A Rocket library to provide a base for Basic Authentication over which a concrete authentication mechanism can be built.

This library exports BasicAuthRaw which you could directly use on the request handler.

Example

use basic_auth_raw::BasicAuthRaw;

#[get("/secure-path")
fn secret(basic: BasicAuthRaw) -> String {
    format!("Your username is {}", basic.username);
}

Or you could build Request Guards on top of it (Recommended).

Example

use basic_auth_raw::BasicAuthRaw;

struct Admin(User);

impl<'a, 'r> FromRequest<'a, 'r> for Admin {
    type Error = ();

    fn from_request(request: &Request) -> Outcome<Self, Self::Error> {
        let basic = BasicAuthRaw::from_request(request)?;
        let user = User::from_db(basic.username, basic.password);
        if user.is_admin {
            Outcome::Success(user);
        } else {
            Outcome::Failure((Status::Unauthorized, ()));
        }
    }
}

#[get("/secure-path")
fn secret(admin: Admin) -> String {
    format!("Your username is {}", admin.user.username);
}
Commit count: 8

cargo fmt