netdb_auth_shared 0.1.2

Netdb Auth validation for rocket
Documentation
use std::env;

pub struct UserBase {
    pub  id: i32,
    pub  username: String,
    pub email: String,
    pub lang: String,
    pub country: String,
    pub scopes: String,
}

impl UserBase {
    pub fn has_scope(&self, category: &str, scope: Option<u32>) -> bool {
        has_scope(category, scope, &self.scopes)
    }
    pub fn get_avatar_url(&self) -> String {
        return String::from("https://cdn.".to_owned() + &env::var("DOMAIN").unwrap() + "/avatar/" + &self.id.to_string());
    }
}

pub fn has_scope(category: &str, scope: Option<u32>, scopes: &str) -> bool {
    if env::var("DISABLE_AUTH").is_ok() && env::var("DISABLE_AUTH").unwrap() == "true" {
        return  true;
    }

    let scopes_array: Vec<&str> = scopes.split(" ").collect();

    for s in scopes_array {
        let scope_parts: Vec<&str> = s.split(":").collect();

        if scope_parts[0] == category {
            if scope.is_none() {
                return true;
            }

            let scope_code: u32 = str::parse(scope_parts[1]).unwrap();

            if (scope_code & scope.unwrap()) == scope.unwrap() {
                return true;
            }
        }
    }

    false
}