1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use super::config;

use rocket;
use rocket::request::FromRequest;
use rocket::Request;
use rocket::http::{Cookie, Cookies};
use rocket::outcome::Outcome;

pub trait FromString{
    fn from_string(s: String) -> Self;
}

impl FromString for String{
    fn from_string(s: String) -> String{
        s
    }
}

pub struct UserPass<'c, T>{
    pub user: T,
    cookies: Cookies<'c>,
}

impl<'c, T> UserPass<'c, T> {
    /// Removes the cookie so the user can be logged out
    pub fn logout(&mut self) {
        self.cookies.remove_private(Cookie::named(config::get_cookie_identifier()));
    }

    /// Get the stored user type
    pub fn into_inner(self) -> T {
        self.user
    }
}

/// A request guard that checks if a private cookie was provided   
///
/// The name of the cookie can be configured with simpleauth_cookie_identifier config key in your
/// Rocket config file.
///
/// By default it is "sid" see the config module
impl<'a,'r, T: FromString> FromRequest<'a, 'r> for UserPass<'a, T>{
    type Error = ();

    fn from_request(request: &'a Request<'r>) -> rocket::request::Outcome<UserPass<'a, T>,Self::Error>{
        let cookie_id = config::get_cookie_identifier();
        let mut cookies = request.cookies();

        match cookies.get_private(&cookie_id){
            Some(cookie) => Outcome::Success(UserPass{user: T::from_string(cookie.value().to_string()),cookies: cookies}),
            None => Outcome::Forward(())
        }
    }
}