2018-12-30 22:34:31 +00:00
|
|
|
//
|
|
|
|
// JWT Handling
|
|
|
|
//
|
2019-01-04 15:32:51 +00:00
|
|
|
use chrono::{Duration, Utc};
|
2020-03-14 12:22:30 +00:00
|
|
|
use num_traits::FromPrimitive;
|
2020-07-14 16:00:09 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2020-07-14 16:00:09 +00:00
|
|
|
use jsonwebtoken::{self, Algorithm, DecodingKey, EncodingKey, Header};
|
2019-01-19 20:36:34 +00:00
|
|
|
use serde::de::DeserializeOwned;
|
2018-02-10 00:00:55 +00:00
|
|
|
use serde::ser::Serialize;
|
|
|
|
|
2020-07-14 16:00:09 +00:00
|
|
|
use crate::{
|
|
|
|
error::{Error, MapResult},
|
|
|
|
util::read_file,
|
|
|
|
CONFIG,
|
|
|
|
};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2018-12-07 01:05:45 +00:00
|
|
|
const JWT_ALGORITHM: Algorithm = Algorithm::RS256;
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2020-03-09 21:04:03 +00:00
|
|
|
pub static DEFAULT_VALIDITY: Lazy<Duration> = Lazy::new(|| Duration::hours(2));
|
|
|
|
static JWT_HEADER: Lazy<Header> = Lazy::new(|| Header::new(JWT_ALGORITHM));
|
2021-06-25 18:33:51 +00:00
|
|
|
|
2020-03-09 21:04:03 +00:00
|
|
|
pub static JWT_LOGIN_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|login", CONFIG.domain_origin()));
|
|
|
|
static JWT_INVITE_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|invite", CONFIG.domain_origin()));
|
|
|
|
static JWT_DELETE_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|delete", CONFIG.domain_origin()));
|
|
|
|
static JWT_VERIFYEMAIL_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|verifyemail", CONFIG.domain_origin()));
|
|
|
|
static JWT_ADMIN_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|admin", CONFIG.domain_origin()));
|
2021-06-25 18:33:51 +00:00
|
|
|
static JWT_SEND_ISSUER: Lazy<String> = Lazy::new(|| format!("{}|send", CONFIG.domain_origin()));
|
|
|
|
|
2021-06-25 18:49:44 +00:00
|
|
|
static PRIVATE_RSA_KEY_VEC: Lazy<Vec<u8>> = Lazy::new(|| {
|
|
|
|
read_file(&CONFIG.private_rsa_key()).unwrap_or_else(|e| panic!("Error loading private RSA Key.\n{}", e))
|
2020-03-09 21:04:03 +00:00
|
|
|
});
|
2021-06-25 18:49:44 +00:00
|
|
|
static PRIVATE_RSA_KEY: Lazy<EncodingKey> = Lazy::new(|| {
|
|
|
|
EncodingKey::from_rsa_pem(&PRIVATE_RSA_KEY_VEC).unwrap_or_else(|e| panic!("Error decoding private RSA Key.\n{}", e))
|
|
|
|
});
|
|
|
|
static PUBLIC_RSA_KEY_VEC: Lazy<Vec<u8>> = Lazy::new(|| {
|
|
|
|
read_file(&CONFIG.public_rsa_key()).unwrap_or_else(|e| panic!("Error loading public RSA Key.\n{}", e))
|
|
|
|
});
|
|
|
|
static PUBLIC_RSA_KEY: Lazy<DecodingKey> = Lazy::new(|| {
|
|
|
|
DecodingKey::from_rsa_pem(&PUBLIC_RSA_KEY_VEC).unwrap_or_else(|e| panic!("Error decoding public RSA Key.\n{}", e))
|
2020-03-09 21:04:03 +00:00
|
|
|
});
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2021-06-25 18:49:44 +00:00
|
|
|
pub fn load_keys() {
|
|
|
|
Lazy::force(&PRIVATE_RSA_KEY);
|
|
|
|
Lazy::force(&PUBLIC_RSA_KEY);
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
pub fn encode_jwt<T: Serialize>(claims: &T) -> String {
|
2021-06-25 18:49:44 +00:00
|
|
|
match jsonwebtoken::encode(&JWT_HEADER, claims, &PRIVATE_RSA_KEY) {
|
2018-06-11 13:44:37 +00:00
|
|
|
Ok(token) => token,
|
2018-12-21 21:08:04 +00:00
|
|
|
Err(e) => panic!("Error encoding jwt {}", e),
|
2018-06-11 13:44:37 +00:00
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 20:36:34 +00:00
|
|
|
fn decode_jwt<T: DeserializeOwned>(token: &str, issuer: String) -> Result<T, Error> {
|
2018-12-07 01:05:45 +00:00
|
|
|
let validation = jsonwebtoken::Validation {
|
2018-02-10 00:00:55 +00:00
|
|
|
leeway: 30, // 30 seconds
|
|
|
|
validate_exp: true,
|
|
|
|
validate_nbf: true,
|
|
|
|
aud: None,
|
2019-01-19 20:36:34 +00:00
|
|
|
iss: Some(issuer),
|
2018-02-10 00:00:55 +00:00
|
|
|
sub: None,
|
|
|
|
algorithms: vec![JWT_ALGORITHM],
|
|
|
|
};
|
|
|
|
|
2019-01-07 19:37:14 +00:00
|
|
|
let token = token.replace(char::is_whitespace, "");
|
2021-06-25 18:49:44 +00:00
|
|
|
jsonwebtoken::decode(&token, &&PUBLIC_RSA_KEY, &validation).map(|d| d.claims).map_res("Error decoding JWT")
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-27 14:26:32 +00:00
|
|
|
pub fn decode_login(token: &str) -> Result<LoginJwtClaims, Error> {
|
2019-01-19 20:36:34 +00:00
|
|
|
decode_jwt(token, JWT_LOGIN_ISSUER.to_string())
|
|
|
|
}
|
2018-12-15 02:52:16 +00:00
|
|
|
|
2021-03-27 14:26:32 +00:00
|
|
|
pub fn decode_invite(token: &str) -> Result<InviteJwtClaims, Error> {
|
2019-01-19 20:36:34 +00:00
|
|
|
decode_jwt(token, JWT_INVITE_ISSUER.to_string())
|
|
|
|
}
|
2019-01-07 19:37:14 +00:00
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn decode_delete(token: &str) -> Result<BasicJwtClaims, Error> {
|
2019-11-25 05:28:49 +00:00
|
|
|
decode_jwt(token, JWT_DELETE_ISSUER.to_string())
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn decode_verify_email(token: &str) -> Result<BasicJwtClaims, Error> {
|
2019-11-25 05:28:49 +00:00
|
|
|
decode_jwt(token, JWT_VERIFYEMAIL_ISSUER.to_string())
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn decode_admin(token: &str) -> Result<BasicJwtClaims, Error> {
|
2019-01-19 20:36:34 +00:00
|
|
|
decode_jwt(token, JWT_ADMIN_ISSUER.to_string())
|
2018-12-15 02:52:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn decode_send(token: &str) -> Result<BasicJwtClaims, Error> {
|
|
|
|
decode_jwt(token, JWT_SEND_ISSUER.to_string())
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:00:55 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-03-27 14:26:32 +00:00
|
|
|
pub struct LoginJwtClaims {
|
2018-02-10 00:00:55 +00: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 20:01:55 +00:00
|
|
|
pub orgowner: Vec<String>,
|
|
|
|
pub orgadmin: Vec<String>,
|
|
|
|
pub orguser: Vec<String>,
|
2018-12-09 16:58:38 +00:00
|
|
|
pub orgmanager: Vec<String>,
|
2018-04-24 20:01:55 +00:00
|
|
|
|
2018-02-10 00:00:55 +00: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-15 02:52:16 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-03-27 14:26:32 +00:00
|
|
|
pub struct InviteJwtClaims {
|
2018-12-15 02:52:16 +00:00
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
|
|
|
|
pub email: String,
|
2019-01-06 04:03:49 +00:00
|
|
|
pub org_id: Option<String>,
|
2018-12-19 04:16:03 +00:00
|
|
|
pub user_org_id: Option<String>,
|
2019-01-04 15:32:51 +00:00
|
|
|
pub invited_by_email: Option<String>,
|
|
|
|
}
|
|
|
|
|
2019-01-19 20:36:34 +00:00
|
|
|
pub fn generate_invite_claims(
|
|
|
|
uuid: String,
|
|
|
|
email: String,
|
|
|
|
org_id: Option<String>,
|
2019-11-02 16:39:01 +00:00
|
|
|
user_org_id: Option<String>,
|
2019-01-19 20:36:34 +00:00
|
|
|
invited_by_email: Option<String>,
|
2021-03-27 14:26:32 +00:00
|
|
|
) -> InviteJwtClaims {
|
2019-01-04 15:32:51 +00:00
|
|
|
let time_now = Utc::now().naive_utc();
|
2021-03-27 14:26:32 +00:00
|
|
|
InviteJwtClaims {
|
2019-01-04 15:32:51 +00:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
2019-01-19 20:36:34 +00:00
|
|
|
iss: JWT_INVITE_ISSUER.to_string(),
|
2019-11-02 16:39:01 +00:00
|
|
|
sub: uuid,
|
|
|
|
email,
|
|
|
|
org_id,
|
|
|
|
user_org_id,
|
|
|
|
invited_by_email,
|
2019-01-04 15:32:51 +00:00
|
|
|
}
|
2018-12-15 02:52:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 05:28:49 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-06-25 18:33:51 +00:00
|
|
|
pub struct BasicJwtClaims {
|
2019-11-25 05:28:49 +00:00
|
|
|
// Not before
|
|
|
|
pub nbf: i64,
|
|
|
|
// Expiration time
|
|
|
|
pub exp: i64,
|
|
|
|
// Issuer
|
|
|
|
pub iss: String,
|
|
|
|
// Subject
|
|
|
|
pub sub: String,
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn generate_delete_claims(uuid: String) -> BasicJwtClaims {
|
2019-11-25 05:28:49 +00:00
|
|
|
let time_now = Utc::now().naive_utc();
|
2021-06-25 18:33:51 +00:00
|
|
|
BasicJwtClaims {
|
2019-11-25 05:28:49 +00:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
|
|
|
iss: JWT_DELETE_ISSUER.to_string(),
|
|
|
|
sub: uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn generate_verify_email_claims(uuid: String) -> BasicJwtClaims {
|
2019-11-25 05:28:49 +00:00
|
|
|
let time_now = Utc::now().naive_utc();
|
2021-06-25 18:33:51 +00:00
|
|
|
BasicJwtClaims {
|
2019-11-25 05:28:49 +00:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::days(5)).timestamp(),
|
|
|
|
iss: JWT_VERIFYEMAIL_ISSUER.to_string(),
|
|
|
|
sub: uuid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn generate_admin_claims() -> BasicJwtClaims {
|
2019-01-19 20:36:34 +00:00
|
|
|
let time_now = Utc::now().naive_utc();
|
2021-06-25 18:33:51 +00:00
|
|
|
BasicJwtClaims {
|
2019-01-19 20:36:34 +00:00
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::minutes(20)).timestamp(),
|
|
|
|
iss: JWT_ADMIN_ISSUER.to_string(),
|
|
|
|
sub: "admin_panel".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 18:33:51 +00:00
|
|
|
pub fn generate_send_claims(send_id: &str, file_id: &str) -> BasicJwtClaims {
|
|
|
|
let time_now = Utc::now().naive_utc();
|
|
|
|
BasicJwtClaims {
|
|
|
|
nbf: time_now.timestamp(),
|
|
|
|
exp: (time_now + Duration::minutes(2)).timestamp(),
|
|
|
|
iss: JWT_SEND_ISSUER.to_string(),
|
|
|
|
sub: format!("{}/{}", send_id, file_id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
//
|
|
|
|
// Bearer token authentication
|
|
|
|
//
|
2020-12-14 18:58:23 +00:00
|
|
|
use rocket::request::{FromRequest, Outcome, Request};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2020-07-14 16:00:09 +00:00
|
|
|
use crate::db::{
|
2020-12-14 18:58:23 +00:00
|
|
|
models::{CollectionUser, Device, User, UserOrgStatus, UserOrgType, UserOrganization, UserStampException},
|
2020-07-14 16:00:09 +00:00
|
|
|
DbConn,
|
|
|
|
};
|
2018-02-10 00:00:55 +00:00
|
|
|
|
2021-03-14 22:24:47 +00:00
|
|
|
pub struct Host {
|
2021-03-31 20:18:35 +00:00
|
|
|
pub host: String,
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-14 22:24:47 +00:00
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for Host {
|
2018-02-10 00:00:55 +00:00
|
|
|
type Error = &'static str;
|
|
|
|
|
2020-07-19 19:01:31 +00:00
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
2018-02-10 00:00:55 +00:00
|
|
|
let headers = request.headers();
|
|
|
|
|
2018-02-15 00:49:36 +00:00
|
|
|
// Get host
|
2019-01-25 17:23:51 +00:00
|
|
|
let host = if CONFIG.domain_set() {
|
|
|
|
CONFIG.domain()
|
2018-07-12 21:28:01 +00:00
|
|
|
} else if let Some(referer) = headers.get_one("Referer") {
|
|
|
|
referer.to_string()
|
2018-12-21 21:08:04 +00:00
|
|
|
} else {
|
2018-07-12 21:28:01 +00: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 22:33:28 +00: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 21:28:01 +00:00
|
|
|
host
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
|
|
|
|
format!("{}://{}", protocol, host)
|
2018-02-15 00:49:36 +00:00
|
|
|
};
|
|
|
|
|
2021-04-06 20:54:42 +00:00
|
|
|
Outcome::Success(Host {
|
|
|
|
host,
|
|
|
|
})
|
2021-03-14 22:24:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Headers {
|
|
|
|
pub host: String,
|
|
|
|
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>) -> Outcome<Self, Self::Error> {
|
|
|
|
let headers = request.headers();
|
|
|
|
|
|
|
|
let host = match Host::from_request(request) {
|
|
|
|
Outcome::Forward(_) => return Outcome::Forward(()),
|
|
|
|
Outcome::Failure(f) => return Outcome::Failure(f),
|
|
|
|
Outcome::Success(host) => host.host,
|
|
|
|
};
|
|
|
|
|
2018-02-14 23:53:11 +00:00
|
|
|
// Get access_token
|
2018-12-18 00:53:21 +00:00
|
|
|
let access_token: &str = match headers.get_one("Authorization") {
|
2018-12-09 16:58:38 +00: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-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
2018-02-14 23:53:11 +00:00
|
|
|
// Check JWT token is valid and get device and user from it
|
2019-01-19 20:36:34 +00:00
|
|
|
let claims = match decode_login(access_token) {
|
2018-02-10 00:00:55 +00:00
|
|
|
Ok(claims) => claims,
|
2018-12-21 21:08:04 +00:00
|
|
|
Err(_) => err_handler!("Invalid claim"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let device_uuid = claims.device;
|
|
|
|
let user_uuid = claims.sub;
|
|
|
|
|
|
|
|
let conn = match request.guard::<DbConn>() {
|
|
|
|
Outcome::Success(conn) => conn,
|
2018-12-21 21:08:04 +00:00
|
|
|
_ => err_handler!("Error getting DB"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let device = match Device::find_by_uuid(&device_uuid, &conn) {
|
|
|
|
Some(device) => device,
|
2018-12-21 21:08:04 +00:00
|
|
|
None => err_handler!("Invalid device id"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let user = match User::find_by_uuid(&user_uuid, &conn) {
|
|
|
|
Some(user) => user,
|
2018-12-21 21:08:04 +00:00
|
|
|
None => err_handler!("Device has no user associated"),
|
2018-02-10 00:00:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if user.security_stamp != claims.sstamp {
|
2021-04-06 20:54:42 +00:00
|
|
|
if let Some(stamp_exception) =
|
|
|
|
user.stamp_exception.as_deref().and_then(|s| serde_json::from_str::<UserStampException>(s).ok())
|
2020-12-14 18:58:23 +00:00
|
|
|
{
|
|
|
|
let current_route = match request.route().and_then(|r| r.name) {
|
|
|
|
Some(name) => name,
|
|
|
|
_ => err_handler!("Error getting current route for stamp exception"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check if both match, if not this route is not allowed with the current security stamp.
|
|
|
|
if stamp_exception.route != current_route {
|
|
|
|
err_handler!("Invalid security stamp: Current route and exception route do not match")
|
|
|
|
} else if stamp_exception.security_stamp != claims.sstamp {
|
|
|
|
err_handler!("Invalid security stamp for matched stamp exception")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err_handler!("Invalid security stamp")
|
|
|
|
}
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 20:54:42 +00:00
|
|
|
Outcome::Success(Headers {
|
|
|
|
host,
|
|
|
|
device,
|
|
|
|
user,
|
|
|
|
})
|
2018-02-10 00:00:55 +00:00
|
|
|
}
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct OrgHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
2018-11-12 17:13:25 +00:00
|
|
|
pub org_user_type: UserOrgType,
|
2020-12-02 21:50:51 +00:00
|
|
|
pub org_user: UserOrganization,
|
|
|
|
pub org_id: String,
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 06:35:09 +00:00
|
|
|
// org_id is usually the second path param ("/organizations/<org_id>"),
|
|
|
|
// but there are cases where it is a query value.
|
|
|
|
// First check the path, if this is not a valid uuid, try the query values.
|
2020-03-19 16:37:10 +00:00
|
|
|
fn get_org_id(request: &Request) -> Option<String> {
|
|
|
|
if let Some(Ok(org_id)) = request.get_param::<String>(1) {
|
|
|
|
if uuid::Uuid::parse_str(&org_id).is_ok() {
|
|
|
|
return Some(org_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Ok(org_id)) = request.get_query_value::<String>("organizationId") {
|
|
|
|
if uuid::Uuid::parse_str(&org_id).is_ok() {
|
|
|
|
return Some(org_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:28:31 +00:00
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for OrgHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
2020-07-19 19:01:31 +00:00
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
2018-05-30 12:28:31 +00:00
|
|
|
match request.guard::<Headers>() {
|
2018-12-07 14:01:29 +00:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 12:28:31 +00:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2020-03-19 16:37:10 +00:00
|
|
|
match get_org_id(request) {
|
|
|
|
Some(org_id) => {
|
|
|
|
let conn = match request.guard::<DbConn>() {
|
|
|
|
Outcome::Success(conn) => conn,
|
|
|
|
_ => err_handler!("Error getting DB"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let user = headers.user;
|
|
|
|
let org_user = match UserOrganization::find_by_user_and_org(&user.uuid, &org_id, &conn) {
|
|
|
|
Some(user) => {
|
|
|
|
if user.status == UserOrgStatus::Confirmed as i32 {
|
|
|
|
user
|
|
|
|
} else {
|
|
|
|
err_handler!("The current user isn't confirmed member of the organization")
|
|
|
|
}
|
2020-03-19 15:50:47 +00:00
|
|
|
}
|
2020-03-19 16:37:10 +00:00
|
|
|
None => err_handler!("The current user isn't member of the organization"),
|
|
|
|
};
|
|
|
|
|
|
|
|
Outcome::Success(Self {
|
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user,
|
|
|
|
org_user_type: {
|
|
|
|
if let Some(org_usr_type) = UserOrgType::from_i32(org_user.atype) {
|
|
|
|
org_usr_type
|
|
|
|
} else {
|
|
|
|
// This should only happen if the DB is corrupted
|
|
|
|
err_handler!("Unknown user type in the database")
|
|
|
|
}
|
|
|
|
},
|
2020-12-02 21:50:51 +00:00
|
|
|
org_user,
|
|
|
|
org_id,
|
2020-03-19 16:37:10 +00:00
|
|
|
})
|
2020-07-14 16:00:09 +00:00
|
|
|
}
|
2020-03-19 16:37:10 +00:00
|
|
|
_ => err_handler!("Error getting the organization id"),
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AdminHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
2018-11-12 17:13:25 +00:00
|
|
|
pub org_user_type: UserOrgType,
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for AdminHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
2020-07-19 19:01:31 +00:00
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
2018-05-30 12:28:31 +00:00
|
|
|
match request.guard::<OrgHeaders>() {
|
2018-12-07 14:01:29 +00:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 12:28:31 +00:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2018-11-12 17:13:25 +00:00
|
|
|
if headers.org_user_type >= UserOrgType::Admin {
|
2018-12-09 16:58:38 +00:00
|
|
|
Outcome::Success(Self {
|
2018-05-30 12:28:31 +00:00
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
org_user_type: headers.org_user_type,
|
|
|
|
})
|
2018-11-12 17:13:25 +00:00
|
|
|
} else {
|
|
|
|
err_handler!("You need to be Admin or Owner to call this endpoint")
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:19:57 +00:00
|
|
|
impl From<AdminHeaders> for Headers {
|
|
|
|
fn from(h: AdminHeaders) -> Headers {
|
2020-03-14 12:22:30 +00:00
|
|
|
Headers {
|
2021-03-27 14:19:57 +00:00
|
|
|
host: h.host,
|
|
|
|
device: h.device,
|
|
|
|
user: h.user,
|
2020-03-14 12:22:30 +00:00
|
|
|
}
|
2020-07-14 16:00:09 +00:00
|
|
|
}
|
2020-03-14 12:22:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 06:35:09 +00:00
|
|
|
// col_id is usually the fourth path param ("/organizations/<org_id>/collections/<col_id>"),
|
|
|
|
// but there could be cases where it is a query value.
|
|
|
|
// First check the path, if this is not a valid uuid, try the query values.
|
2020-12-02 21:50:51 +00:00
|
|
|
fn get_col_id(request: &Request) -> Option<String> {
|
|
|
|
if let Some(Ok(col_id)) = request.get_param::<String>(3) {
|
|
|
|
if uuid::Uuid::parse_str(&col_id).is_ok() {
|
|
|
|
return Some(col_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Ok(col_id)) = request.get_query_value::<String>("collectionId") {
|
|
|
|
if uuid::Uuid::parse_str(&col_id).is_ok() {
|
|
|
|
return Some(col_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The ManagerHeaders are used to check if you are at least a Manager
|
|
|
|
/// and have access to the specific collection provided via the <col_id>/collections/collectionId.
|
|
|
|
/// This does strict checking on the collection_id, ManagerHeadersLoose does not.
|
|
|
|
pub struct ManagerHeaders {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
|
|
|
pub org_user_type: UserOrgType,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for ManagerHeaders {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
|
|
|
match request.guard::<OrgHeaders>() {
|
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
|
|
|
if headers.org_user_type >= UserOrgType::Manager {
|
|
|
|
match get_col_id(request) {
|
|
|
|
Some(col_id) => {
|
|
|
|
let conn = match request.guard::<DbConn>() {
|
|
|
|
Outcome::Success(conn) => conn,
|
|
|
|
_ => err_handler!("Error getting DB"),
|
|
|
|
};
|
|
|
|
|
2021-01-27 06:35:09 +00:00
|
|
|
if !headers.org_user.has_full_access() {
|
2021-03-31 20:18:35 +00:00
|
|
|
match CollectionUser::find_by_collection_and_user(
|
|
|
|
&col_id,
|
|
|
|
&headers.org_user.user_uuid,
|
|
|
|
&conn,
|
|
|
|
) {
|
2020-12-02 21:50:51 +00:00
|
|
|
Some(_) => (),
|
|
|
|
None => err_handler!("The current user isn't a manager for this collection"),
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 18:58:23 +00:00
|
|
|
}
|
2020-12-02 21:50:51 +00:00
|
|
|
_ => err_handler!("Error getting the collection id"),
|
|
|
|
}
|
|
|
|
|
|
|
|
Outcome::Success(Self {
|
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
org_user_type: headers.org_user_type,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
err_handler!("You need to be a Manager, Admin or Owner to call this endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:19:57 +00:00
|
|
|
impl From<ManagerHeaders> for Headers {
|
|
|
|
fn from(h: ManagerHeaders) -> Headers {
|
2020-12-02 21:50:51 +00:00
|
|
|
Headers {
|
2021-03-27 14:19:57 +00:00
|
|
|
host: h.host,
|
|
|
|
device: h.device,
|
|
|
|
user: h.user,
|
2020-12-02 21:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The ManagerHeadersLoose is used when you at least need to be a Manager,
|
|
|
|
/// but there is no collection_id sent with the request (either in the path or as form data).
|
|
|
|
pub struct ManagerHeadersLoose {
|
|
|
|
pub host: String,
|
|
|
|
pub device: Device,
|
|
|
|
pub user: User,
|
|
|
|
pub org_user_type: UserOrgType,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for ManagerHeadersLoose {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
|
|
|
match request.guard::<OrgHeaders>() {
|
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
|
|
|
if headers.org_user_type >= UserOrgType::Manager {
|
|
|
|
Outcome::Success(Self {
|
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
org_user_type: headers.org_user_type,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
err_handler!("You need to be a Manager, Admin or Owner to call this endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 14:19:57 +00:00
|
|
|
impl From<ManagerHeadersLoose> for Headers {
|
|
|
|
fn from(h: ManagerHeadersLoose) -> Headers {
|
2020-12-02 21:50:51 +00:00
|
|
|
Headers {
|
2021-03-27 14:19:57 +00:00
|
|
|
host: h.host,
|
|
|
|
device: h.device,
|
|
|
|
user: h.user,
|
2020-12-02 21:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 12:28:31 +00: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;
|
|
|
|
|
2020-07-19 19:01:31 +00:00
|
|
|
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
2018-05-30 12:28:31 +00:00
|
|
|
match request.guard::<OrgHeaders>() {
|
2018-12-07 14:01:29 +00:00
|
|
|
Outcome::Forward(_) => Outcome::Forward(()),
|
2018-05-30 12:28:31 +00:00
|
|
|
Outcome::Failure(f) => Outcome::Failure(f),
|
|
|
|
Outcome::Success(headers) => {
|
2018-11-12 17:13:25 +00:00
|
|
|
if headers.org_user_type == UserOrgType::Owner {
|
2018-12-09 16:58:38 +00:00
|
|
|
Outcome::Success(Self {
|
2018-05-30 12:28:31 +00:00
|
|
|
host: headers.host,
|
|
|
|
device: headers.device,
|
|
|
|
user: headers.user,
|
|
|
|
})
|
2018-11-12 17:13:25 +00:00
|
|
|
} else {
|
|
|
|
err_handler!("You need to be Owner to call this endpoint")
|
2018-05-30 12:28:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 16:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
//
|
|
|
|
// Client IP address detection
|
|
|
|
//
|
2018-12-09 16:58:38 +00:00
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
|
|
pub struct ClientIp {
|
|
|
|
pub ip: IpAddr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for ClientIp {
|
|
|
|
type Error = ();
|
|
|
|
|
2020-07-19 19:01:31 +00:00
|
|
|
fn from_request(req: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
2019-12-27 17:42:39 +00:00
|
|
|
let ip = if CONFIG._ip_header_enabled() {
|
|
|
|
req.headers().get_one(&CONFIG.ip_header()).and_then(|ip| {
|
2019-12-28 14:08:17 +00:00
|
|
|
match ip.find(',') {
|
|
|
|
Some(idx) => &ip[..idx],
|
|
|
|
None => ip,
|
|
|
|
}
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| warn!("'{}' header is malformed: {}", CONFIG.ip_header(), ip))
|
|
|
|
.ok()
|
2019-12-27 17:42:39 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
2018-12-09 16:58:38 +00:00
|
|
|
};
|
|
|
|
|
2021-04-06 20:54:42 +00:00
|
|
|
let ip = ip.or_else(|| req.remote().map(|r| r.ip())).unwrap_or_else(|| "0.0.0.0".parse().unwrap());
|
2019-12-27 17:42:39 +00:00
|
|
|
|
2021-04-06 20:54:42 +00:00
|
|
|
Outcome::Success(ClientIp {
|
|
|
|
ip,
|
|
|
|
})
|
2018-12-09 16:58:38 +00:00
|
|
|
}
|
|
|
|
}
|