2018-12-30 17:34:31 -05:00
|
|
|
//
|
|
|
|
// JWT Handling
|
|
|
|
//
|
2018-12-06 20:05:45 -05:00
|
|
|
use crate::util::read_file;
|
2019-01-04 10:32:51 -05:00
|
|
|
use chrono::{Duration, Utc};
|
2018-02-09 19:00:55 -05:00
|
|
|
|
2018-12-06 20:05:45 -05:00
|
|
|
use jsonwebtoken::{self, Algorithm, Header};
|
2019-01-19 15:36:34 -05:00
|
|
|
use serde::de::DeserializeOwned;
|
2018-02-09 19:00:55 -05:00
|
|
|
use serde::ser::Serialize;
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
use crate::error::{Error, MapResult};
|
2018-12-06 20:05:45 -05:00
|
|
|
use crate::CONFIG;
|
2018-02-09 19:00:55 -05:00
|
|
|
|
2018-12-06 20:05:45 -05:00
|
|
|
const JWT_ALGORITHM: Algorithm = Algorithm::RS256;
|
2018-02-09 19:00:55 -05:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref DEFAULT_VALIDITY: Duration = Duration::hours(2);
|
2018-12-06 20:05:45 -05:00
|
|
|
static ref JWT_HEADER: Header = Header::new(JWT_ALGORITHM);
|
2019-01-25 12:23:51 -05:00
|
|
|
pub static ref JWT_LOGIN_ISSUER: String = format!("{}|login", CONFIG.domain());
|
|
|
|
pub static ref JWT_INVITE_ISSUER: String = format!("{}|invite", CONFIG.domain());
|
2019-11-25 00:28:49 -05:00
|
|
|
pub static ref JWT_DELETE_ISSUER: String = format!("{}|delete", CONFIG.domain());
|
|
|
|
pub static ref JWT_VERIFYEMAIL_ISSUER: String = format!("{}|verifyemail", CONFIG.domain());
|
2019-01-25 12:23:51 -05:00
|
|
|
pub static ref JWT_ADMIN_ISSUER: String = format!("{}|admin", CONFIG.domain());
|
|
|
|
static ref PRIVATE_RSA_KEY: Vec<u8> = match read_file(&CONFIG.private_rsa_key()) {
|
2018-02-09 19:00:55 -05:00
|
|
|
Ok(key) => key,
|
2019-03-03 10:11:55 -05:00
|
|
|
Err(e) => panic!("Error loading private RSA Key.\n Error: {}", e),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
2019-01-25 12:23:51 -05:00
|
|
|
static ref PUBLIC_RSA_KEY: Vec<u8> = match read_file(&CONFIG.public_rsa_key()) {
|
2018-02-09 19:00:55 -05:00
|
|
|
Ok(key) => key,
|
2019-03-03 10:11:55 -05:00
|
|
|
Err(e) => panic!("Error loading public RSA Key.\n Error: {}", e),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn encode_jwt<T: Serialize>(claims: &T) -> String {
|
2018-12-06 20:05:45 -05:00
|
|
|
match jsonwebtoken::encode(&JWT_HEADER, claims, &PRIVATE_RSA_KEY) {
|
2018-06-11 09:44:37 -04:00
|
|
|
Ok(token) => token,
|
2018-12-21 16:08:04 -05:00
|
|
|
Err(e) => panic!("Error encoding jwt {}", e),
|
2018-06-11 09:44:37 -04:00
|
|
|
}
|
2018-02-09 19:00:55 -05:00
|
|
|
}
|
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
fn decode_jwt<T: DeserializeOwned>(token: &str, issuer: String) -> Result<T, Error> {
|
2018-12-06 20:05:45 -05:00
|
|
|
let validation = jsonwebtoken::Validation {
|
2018-02-09 19:00:55 -05:00
|
|
|
leeway: 30, // 30 seconds
|
|
|
|
validate_exp: true,
|
|
|
|
validate_nbf: true,
|
|
|
|
aud: None,
|
2019-01-19 15:36:34 -05:00
|
|
|
iss: Some(issuer),
|
2018-02-09 19:00:55 -05:00
|
|
|
sub: None,
|
|
|
|
algorithms: vec![JWT_ALGORITHM],
|
|
|
|
};
|
|
|
|
|
2019-01-07 14:37:14 -05:00
|
|
|
let token = token.replace(char::is_whitespace, "");
|
|
|
|
|
|
|
|
jsonwebtoken::decode(&token, &PUBLIC_RSA_KEY, &validation)
|
2018-12-21 16:08:04 -05:00
|
|
|
.map(|d| d.claims)
|
2019-01-19 15:36:34 -05:00
|
|
|
.map_res("Error decoding JWT")
|
2018-02-09 19:00:55 -05:00
|
|
|
}
|
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
pub fn decode_login(token: &str) -> Result<LoginJWTClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_LOGIN_ISSUER.to_string())
|
|
|
|
}
|
2018-12-14 21:52:16 -05:00
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
pub fn decode_invite(token: &str) -> Result<InviteJWTClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_INVITE_ISSUER.to_string())
|
|
|
|
}
|
2019-01-07 14:37:14 -05:00
|
|
|
|
2019-11-25 00:28:49 -05:00
|
|
|
pub fn decode_delete(token: &str) -> Result<DeleteJWTClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_DELETE_ISSUER.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_verify_email(token: &str) -> Result<VerifyEmailJWTClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_VERIFYEMAIL_ISSUER.to_string())
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
pub fn decode_admin(token: &str) -> Result<AdminJWTClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_ADMIN_ISSUER.to_string())
|
2018-12-14 21:52:16 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 19:00:55 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2019-01-19 15:36:34 -05:00
|
|
|
pub struct LoginJWTClaims {
|
2018-02-09 19:00:55 -05:00
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
|
|
|
|
pub premium: bool,
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
pub email_verified: bool,
|
|
|
|
|
2018-04-24 16:01:55 -04:00
|
|
|
pub orgowner: Vec<String>,
|
|
|
|
pub orgadmin: Vec<String>,
|
|
|
|
pub orguser: Vec<String>,
|
2018-12-09 11:58:38 -05:00
|
|
|
pub orgmanager: Vec<String>,
|
2018-04-24 16:01:55 -04:00
|
|
|
|
2018-02-09 19:00:55 -05:00
|
|
|
// user security_stamp
|
|
|
|
pub sstamp: String,
|
|
|
|
// device uuid
|
|
|
|
pub device: String,
|
|
|
|
// [ "api", "offline_access" ]
|
|
|
|
pub scope: Vec<String>,
|
|
|
|
// [ "Application" ]
|
|
|
|
pub amr: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2018-12-14 21:52:16 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct InviteJWTClaims {
|
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
|
|
|
|
pub email: String,
|
2019-01-05 23:03:49 -05:00
|
|
|
pub org_id: Option<String>,
|
2018-12-18 23:16:03 -05:00
|
|
|
pub user_org_id: Option<String>,
|
2019-01-04 10:32:51 -05:00
|
|
|
pub invited_by_email: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
pub fn generate_invite_claims(
|
|
|
|
uuid: String,
|
|
|
|
email: String,
|
|
|
|
org_id: Option<String>,
|
2019-11-02 12:39:01 -04:00
|
|
|
user_org_id: Option<String>,
|
2019-01-19 15:36:34 -05:00
|
|
|
invited_by_email: Option<String>,
|
2019-01-04 10:32:51 -05:00
|
|
|
) -> InviteJWTClaims {
|
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
InviteJWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
2019-01-19 15:36:34 -05:00
|
|
|
iss: JWT_INVITE_ISSUER.to_string(),
|
2019-11-02 12:39:01 -04:00
|
|
|
sub: uuid,
|
|
|
|
email,
|
|
|
|
org_id,
|
|
|
|
user_org_id,
|
|
|
|
invited_by_email,
|
2019-01-04 10:32:51 -05:00
|
|
|
}
|
2018-12-14 21:52:16 -05:00
|
|
|
}
|
|
|
|
|
2019-11-25 00:28:49 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct DeleteJWTClaims {
|
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
}
|
|
|
|
|
2019-12-27 12:37:14 -05:00
|
|
|
pub fn generate_delete_claims(uuid: String) -> DeleteJWTClaims {
|
2019-11-25 00:28:49 -05:00
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
DeleteJWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
|
|
|
iss: JWT_DELETE_ISSUER.to_string(),
|
|
|
|
sub: uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct VerifyEmailJWTClaims {
|
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
}
|
|
|
|
|
2019-12-27 12:37:14 -05:00
|
|
|
pub fn generate_verify_email_claims(uuid: String) -> DeleteJWTClaims {
|
2019-11-25 00:28:49 -05:00
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
DeleteJWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
|
|
|
iss: JWT_VERIFYEMAIL_ISSUER.to_string(),
|
|
|
|
sub: uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 15:36:34 -05:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct AdminJWTClaims {
|
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn generate_admin_claims() -> AdminJWTClaims {
|
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
AdminJWTClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::minutes(20)).timestamp(),
|
|
|
|
iss: JWT_ADMIN_ISSUER.to_string(),
|
|
|
|
sub: "admin_panel".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:34:31 -05:00
|
|
|
//
|
|
|
|
// Bearer token authentication
|
|
|
|
//
|
|
|
|
use rocket::request::{self, FromRequest, Request};
|
2018-02-09 19:00:55 -05:00
|
|
|
use rocket::Outcome;
|
|
|
|
|
2018-12-30 17:34:31 -05:00
|
|
|
use crate::db::models::{Device, User, UserOrgStatus, UserOrgType, UserOrganization};
|
2018-12-06 20:05:45 -05:00
|
|
|
use crate::db::DbConn;
|
2018-02-09 19:00:55 -05:00
|
|
|
|
|
|
|
pub struct Headers {
|
2018-02-14 19:49:36 -05:00
|
|
|
pub host: String,
|
2018-02-09 19:00:55 -05:00
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Headers {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
let headers = request.headers();
|
|
|
|
|
2018-02-14 19:49:36 -05:00
|
|
|
// Get host
|
2019-01-25 12:23:51 -05:00
|
|
|
let host = if CONFIG.domain_set() {
|
|
|
|
CONFIG.domain()
|
2018-07-12 17:28:01 -04:00
|
|
|
} else if let Some(referer) = headers.get_one("Referer") {
|
|
|
|
referer.to_string()
|
2018-12-21 16:08:04 -05:00
|
|
|
} else {
|
2018-07-12 17:28:01 -04:00
|
|
|
// Try to guess from the headers
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
let protocol = if let Some(proto) = headers.get_one("X-Forwarded-Proto") {
|
|
|
|
proto
|
|
|
|
} else if env::var("ROCKET_TLS").is_ok() {
|
|
|
|
"https"
|
|
|
|
} else {
|
|
|
|
"http"
|
|
|
|
};
|
|
|
|
|
2018-07-12 18:33:28 -04:00
|
|
|
let host = if let Some(host) = headers.get_one("X-Forwarded-Host") {
|
|
|
|
host
|
|
|
|
} else if let Some(host) = headers.get_one("Host") {
|
2018-07-12 17:28:01 -04:00
|
|
|
host
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
format!("{}://{}", protocol, host)
|
2018-02-14 19:49:36 -05:00
|
|
|
};
|
|
|
|
|
2018-02-14 18:53:11 -05:00
|
|
|
// Get access_token
|
2018-12-17 19:53:21 -05:00
|
|
|
let access_token: &str = match headers.get_one("Authorization") {
|
2018-12-09 11:58:38 -05:00
|
|
|
Some(a) => match a.rsplit("Bearer ").next() {
|
|
|
|
Some(split) => split,
|
|
|
|
None => err_handler!("No access token provided"),
|
|
|
|
},
|
|
|
|
None => err_handler!("No access token provided"),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
|
2018-02-14 18:53:11 -05:00
|
|
|
// Check JWT token is valid and get device and user from it
|
2019-01-19 15:36:34 -05:00
|
|
|
let claims = match decode_login(access_token) {
|
2018-02-09 19:00:55 -05:00
|
|
|
Ok(claims) => claims,
|
2018-12-21 16:08:04 -05:00
|
|
|
Err(_) => err_handler!("Invalid claim"),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let device_uuid = claims.device;
|
|
|
|
let user_uuid = claims.sub;
|
|
|
|
|
|
|
|
let conn = match request.guard::<DbConn>() {
|
|
|
|
Outcome::Success(conn) => conn,
|
2018-12-21 16:08:04 -05:00
|
|
|
_ => err_handler!("Error getting DB"),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let device = match Device::find_by_uuid(&device_uuid, &conn) {
|
|
|
|
Some(device) => device,
|
2018-12-21 16:08:04 -05:00
|
|
|
None => err_handler!("Invalid device id"),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let user = match User::find_by_uuid(&user_uuid, &conn) {
|
|
|
|
Some(user) => user,
|
2018-12-21 16:08:04 -05:00
|
|
|
None => err_handler!("Device has no user associated"),
|
2018-02-09 19:00:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if user.security_stamp != claims.sstamp {
|
|
|
|
err_handler!("Invalid security stamp")
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:05:57 -05:00
|
|
|
Outcome::Success(Headers { host, device, user })
|
2018-02-09 19:00:55 -05:00
|
|
|
}
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OrgHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
2018-11-12 12:13:25 -05:00
|
|
|
pub org_user_type: UserOrgType,
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for OrgHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
match request.guard::<Headers>() {
|
2018-12-07 09:01:29 -05:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 08:28:31 -04:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2018-11-06 10:53:34 -05:00
|
|
|
// org_id is expected to be the second param ("/organizations/<org_id>")
|
|
|
|
match request.get_param::<String>(1) {
|
2018-10-10 14:40:39 -04:00
|
|
|
Some(Ok(org_id)) => {
|
2018-05-30 08:28:31 -04:00
|
|
|
let conn = match request.guard::<DbConn>() {
|
|
|
|
Outcome::Success(conn) => conn,
|
2018-12-30 17:34:31 -05:00
|
|
|
_ => err_handler!("Error getting DB"),
|
2018-05-30 08:28:31 -04:00
|
|
|
};
|
|
|
|
|
2018-12-30 17:34:31 -05:00
|
|
|
let user = headers.user;
|
|
|
|
let org_user = match UserOrganization::find_by_user_and_org(&user.uuid, &org_id, &conn) {
|
2018-07-16 05:23:45 -04:00
|
|
|
Some(user) => {
|
|
|
|
if user.status == UserOrgStatus::Confirmed as i32 {
|
|
|
|
user
|
|
|
|
} else {
|
|
|
|
err_handler!("The current user isn't confirmed member of the organization")
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 17:34:31 -05:00
|
|
|
None => err_handler!("The current user isn't member of the organization"),
|
2018-05-30 08:28:31 -04:00
|
|
|
};
|
|
|
|
|
2018-12-21 16:08:04 -05:00
|
|
|
Outcome::Success(Self {
|
2018-05-30 08:28:31 -04:00
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
2018-12-30 17:34:31 -05:00
|
|
|
user,
|
2018-12-21 16:08:04 -05:00
|
|
|
org_user_type: {
|
2019-05-20 15:24:29 -04:00
|
|
|
if let Some(org_usr_type) = UserOrgType::from_i32(org_user.atype) {
|
2018-11-12 12:13:25 -05:00
|
|
|
org_usr_type
|
2018-12-30 17:34:31 -05:00
|
|
|
} else {
|
|
|
|
// This should only happen if the DB is corrupted
|
2018-11-12 12:13:25 -05:00
|
|
|
err_handler!("Unknown user type in the database")
|
|
|
|
}
|
|
|
|
},
|
2018-05-30 08:28:31 -04:00
|
|
|
})
|
2018-12-21 16:08:04 -05:00
|
|
|
}
|
2018-10-10 14:40:39 -04:00
|
|
|
_ => err_handler!("Error getting the organization id"),
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AdminHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
2018-11-12 12:13:25 -05:00
|
|
|
pub org_user_type: UserOrgType,
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for AdminHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
match request.guard::<OrgHeaders>() {
|
2018-12-07 09:01:29 -05:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 08:28:31 -04:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2018-11-12 12:13:25 -05:00
|
|
|
if headers.org_user_type >= UserOrgType::Admin {
|
2018-12-09 11:58:38 -05:00
|
|
|
Outcome::Success(Self {
|
2018-05-30 08:28:31 -04:00
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
org_user_type: headers.org_user_type,
|
|
|
|
})
|
2018-11-12 12:13:25 -05:00
|
|
|
} else {
|
|
|
|
err_handler!("You need to be Admin or Owner to call this endpoint")
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OwnerHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for OwnerHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
match request.guard::<OrgHeaders>() {
|
2018-12-07 09:01:29 -05:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 08:28:31 -04:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2018-11-12 12:13:25 -05:00
|
|
|
if headers.org_user_type == UserOrgType::Owner {
|
2018-12-09 11:58:38 -05:00
|
|
|
Outcome::Success(Self {
|
2018-05-30 08:28:31 -04:00
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
})
|
2018-11-12 12:13:25 -05:00
|
|
|
} else {
|
|
|
|
err_handler!("You need to be Owner to call this endpoint")
|
2018-05-30 08:28:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 11:58:38 -05:00
|
|
|
}
|
|
|
|
|
2018-12-30 17:34:31 -05:00
|
|
|
//
|
|
|
|
// Client IP address detection
|
|
|
|
//
|
2018-12-09 11:58:38 -05:00
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
|
|
pub struct ClientIp {
|
|
|
|
pub ip: IpAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for ClientIp {
|
|
|
|
type Error = ();
|
|
|
|
|
2019-12-27 12:42:39 -05:00
|
|
|
fn from_request(req: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
|
|
|
let ip = if CONFIG._ip_header_enabled() {
|
|
|
|
req.headers().get_one(&CONFIG.ip_header()).and_then(|ip| {
|
|
|
|
ip.parse()
|
|
|
|
.map_err(|_| warn_!("'{}' header is malformed: {}", CONFIG.ip_header(), ip))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2018-12-09 11:58:38 -05:00
|
|
|
};
|
|
|
|
|
2019-12-27 12:42:39 -05:00
|
|
|
let ip = ip
|
|
|
|
.or_else(|| req.remote().map(|r| r.ip()))
|
|
|
|
.unwrap_or_else(|| "0.0.0.0".parse().unwrap());
|
|
|
|
|
2018-12-09 11:58:38 -05:00
|
|
|
Outcome::Success(ClientIp { ip })
|
|
|
|
}
|
|
|
|
}
|