Identity provider and SSO for Kupolo.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

54 lines
1.5 KiB

use rocket::request::{self, Outcome, Request, FromRequest};
use rocket::Outcome::{Success, Forward};
static REFERER_HEADER: &'static str = "referer";
static HOST_HEADER: &'static str = "host";
static AUTH_HEADER: &'static str = "authorization";
static AUTH_SCHEME: &'static str = "Bearer ";
#[derive(Into)]
pub struct Referer<'a>(&'a str);
impl<'a, 'r> FromRequest<'a, 'r> for Referer<'a> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Referer<'a>, ()> {
match request.headers().get_one(REFERER_HEADER) {
Some(referer) => Success(Referer(referer)),
None => Forward(())
}
}
}
#[derive(Into)]
pub struct Host<'a>(&'a str);
impl<'a, 'r> FromRequest<'a, 'r> for Host<'a> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Host<'a>, ()> {
match request.headers().get_one(HOST_HEADER) {
Some(host) => Success(Host(host)),
None => Forward(())
}
}
}
#[derive(Into)]
pub struct BearerToken<'a>(&'a str);
impl<'a, 'r> FromRequest<'a, 'r> for BearerToken<'a> {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<BearerToken<'a>, ()> {
println!("{:?}", request.headers());
match request.headers().get_one(AUTH_HEADER) {
Some(token) => if token.starts_with(AUTH_SCHEME) {
Success(BearerToken(token.get((AUTH_SCHEME.len()..)).unwrap()))
} else {
Forward(())
},
None => Forward(())
}
}
}