pub fn validate(&mut self, conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>) -> Result<Option<i64>, status::Custom<Json>> {
        if self.basic.is_some() {
            let (username, password) = self.basic.clone().unwrap();

            match conn.query("
                SELECT
                    id,
                    access
                FROM users
                WHERE
                    username = $1
                    AND password = crypt($2, password)
            ", &[ &username, &password ]) {
                Ok(res) => {
                    if res.len() != 1 {
                        return Err(not_authed());
                    }

                    let uid: i64 = res.get(0).get(0);
                    let access: Option<String> = res.get(0).get(1);

                    self.secure(Some((uid, access)));

                    return Ok(Some(uid));
                },
                _ => {
                    return Err(not_authed());
                }
            }
        } else if self.token.is_some() {
            let token = self.token.clone().unwrap();

            match conn.query("
                SELECT
                    users_tokens.uid,
                    users.access
                FROM
                    users_tokens,
                    users
                WHERE
                    token = $1
                    AND now() < expiry
                    AND users_tokens.uid = users.id
            ", &[ &token ]) {
                Ok(res) => {
                    if res.len() == 0 {
                        return Err(not_authed());
                    }

                    let uid: i64 = res.get(0).get(0);
                    let access: Option<String> = res.get(0).get(1);

                    self.secure(Some((uid, access)));

                    return Ok(Some(uid));
                },
                _ => {
                    return Err(not_authed());
                }
            }
        }

        Ok(None)
    }
///
/// The Rocket Request guard simply provides a utility wrapper from the request to a more
/// easily parsable auth object. It **does not** perform any authentication.
///
/// This function takes the populated Auth struct and checks if the token/basic auth is valid,
/// populated the uid field
///
/// Note: Once validated the token/basic auth used to validate the user will be set to null
///
The Rocket Request guard simply provides a utility wrapper from the request to a more
easily parsable auth object. It **does not** perform any authentication.

This function takes the populated Auth struct and checks if the token/basic auth is valid,
populated the uid field

Once validated the token/basic auth used to validate the user will be set to null
The Rocket Request guard simply provides a utility wrapper from the request to a more easily parsable auth object . It ** does not ** perform any authentication . This function takes the populated Auth struct and checks if the token / basic auth is valid populated the uid field Once validated the token / basic auth used to validate the user will be set to null
pub fn validate(&mut self, conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>) -> Result<Option<i64>, status::Custom<Json>> {
        if self.basic.is_some() {
            let (username, password) = self.basic.clone().unwrap();
            match conn.query("
                SELECT
                    id,
                    access
                FROM users
                WHERE
                    username = $1
                    AND password = crypt($2, password)
            ", &[ &username, &password ]) {
                Ok(res) => {
                    if res.len() != 1 {
                        return Err(not_authed());
                    }
                    let uid: i64 = res.get(0).get(0);
                    let access: Option<String> = res.get(0).get(1);
                    self.secure(Some((uid, access)));
                    return Ok(Some(uid));
                },
                _ => {
                    return Err(not_authed());
                }
            }
        } else if self.token.is_some() {
            let token = self.token.clone().unwrap();
            match conn.query("
                SELECT
                    users_tokens.uid,
                    users.access
                FROM
                    users_tokens,
                    users
                WHERE
                    token = $1
                    AND now() < expiry
                    AND users_tokens.uid = users.id
            ", &[ &token ]) {
                Ok(res) => {
                    if res.len() == 0 {
                        return Err(not_authed());
                    }
                    let uid: i64 = res.get(0).get(0);
                    let access: Option<String> = res.get(0).get(1);
                    self.secure(Some((uid, access)));
                    return Ok(Some(uid));
                },
                _ => {
                    return Err(not_authed());
                }
            }
        }
        Ok(None)
    }
pub fn validate ( & mut self , conn : & r2d2 :: PooledConnection < r2d2_postgres :: PostgresConnectionManager > ) -> Result < Option < i64 > , status :: Custom < Json > > { if self . basic . is_some ( ) { let ( username , password ) = self . basic . clone ( ) . unwrap ( ) ; match conn . query ( " " , & [ & username , & password ] ) { Ok ( res ) => { if res . len ( ) != 1 { return Err ( not_authed ( ) ) ; } let uid : i64 = res . get ( 0 ) . get ( 0 ) ; let access : Option < String > = res . get ( 0 ) . get ( 1 ) ; self . secure ( Some ( ( uid , access ) ) ) ; return Ok ( Some ( uid ) ) ; } , _ => { return Err ( not_authed ( ) ) ; } } } else if self . token . is_some ( ) { let token = self . token . clone ( ) . unwrap ( ) ; match conn . query ( " " , & [ & token ] ) { Ok ( res ) => { if res . len ( ) == 0 { return Err ( not_authed ( ) ) ; } let uid : i64 = res . get ( 0 ) . get ( 0 ) ; let access : Option < String > = res . get ( 0 ) . get ( 1 ) ; self . secure ( Some ( ( uid , access ) ) ) ; return Ok ( Some ( uid ) ) ; } , _ => { return Err ( not_authed ( ) ) ; } } } Ok ( None ) }
The Rocket Request guard simply provides a utility wrapper from the request to a more
easily parsable auth object.
The Rocket Request guard simply provides a utility wrapper from the request to a more easily parsable auth object .
&mut self
conn
&r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>
&mut self
conn
&r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>