2018-12-18 01:53:21 +01:00
|
|
|
use serde_json::Value;
|
2019-06-02 00:08:52 +02:00
|
|
|
use std::process::Command;
|
2018-12-18 01:53:21 +01:00
|
|
|
|
2019-01-20 15:36:33 +01:00
|
|
|
use rocket::http::{Cookie, Cookies, SameSite};
|
2019-01-19 21:36:34 +01:00
|
|
|
use rocket::request::{self, FlashMessage, Form, FromRequest, Request};
|
|
|
|
use rocket::response::{content::Html, Flash, Redirect};
|
|
|
|
use rocket::{Outcome, Route};
|
2019-02-02 16:47:27 +01:00
|
|
|
use rocket_contrib::json::Json;
|
2019-01-19 21:36:34 +01:00
|
|
|
|
2019-04-05 20:49:58 +02:00
|
|
|
use crate::api::{ApiResult, EmptyResult, JsonResult};
|
2019-01-19 21:36:34 +01:00
|
|
|
use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp};
|
2019-02-02 16:47:27 +01:00
|
|
|
use crate::config::ConfigBuilder;
|
2019-06-02 00:08:52 +02:00
|
|
|
use crate::db::{backup_database, models::*, DbConn};
|
2019-01-19 21:36:34 +01:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::mail;
|
2018-12-18 18:52:58 +01:00
|
|
|
use crate::CONFIG;
|
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
pub fn routes() -> Vec<Route> {
|
2019-02-20 21:44:35 +01:00
|
|
|
if CONFIG.admin_token().is_none() && !CONFIG.disable_admin_token() {
|
2019-02-10 15:26:19 +01:00
|
|
|
return routes![admin_disabled];
|
2019-01-19 21:36:34 +01:00
|
|
|
}
|
2018-12-18 01:53:21 +01:00
|
|
|
|
2019-01-26 19:28:54 +01:00
|
|
|
routes![
|
|
|
|
admin_login,
|
2019-04-05 20:49:58 +02:00
|
|
|
get_users,
|
2019-01-26 19:28:54 +01:00
|
|
|
post_admin_login,
|
|
|
|
admin_page,
|
|
|
|
invite_user,
|
2019-12-01 21:15:14 +01:00
|
|
|
logout,
|
2019-01-26 19:28:54 +01:00
|
|
|
delete_user,
|
|
|
|
deauth_user,
|
2019-08-21 17:13:06 +02:00
|
|
|
remove_2fa,
|
2019-03-07 21:08:33 +01:00
|
|
|
update_revision_users,
|
2019-02-02 01:09:21 +01:00
|
|
|
post_config,
|
2019-02-06 17:32:13 +01:00
|
|
|
delete_config,
|
2019-05-03 15:46:29 +02:00
|
|
|
backup_db,
|
2019-01-26 19:28:54 +01:00
|
|
|
]
|
2019-01-19 21:36:34 +01:00
|
|
|
}
|
2018-12-18 01:53:21 +01:00
|
|
|
|
2019-06-02 00:08:52 +02:00
|
|
|
lazy_static! {
|
2019-10-01 11:40:22 +02:00
|
|
|
static ref CAN_BACKUP: bool = cfg!(feature = "sqlite") && Command::new("sqlite3").arg("-version").status().is_ok();
|
2019-06-02 00:08:52 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 15:26:19 +01:00
|
|
|
#[get("/")]
|
|
|
|
fn admin_disabled() -> &'static str {
|
|
|
|
"The admin panel is disabled, please configure the 'ADMIN_TOKEN' variable to enable it"
|
|
|
|
}
|
|
|
|
|
2019-01-28 23:58:32 +01:00
|
|
|
const COOKIE_NAME: &str = "BWRS_ADMIN";
|
|
|
|
const ADMIN_PATH: &str = "/admin";
|
2019-01-19 21:36:34 +01:00
|
|
|
|
2019-02-02 16:47:27 +01:00
|
|
|
const BASE_TEMPLATE: &str = "admin/base";
|
2019-02-10 15:46:51 +01:00
|
|
|
const VERSION: Option<&str> = option_env!("GIT_VERSION");
|
2019-01-19 22:12:52 +01:00
|
|
|
|
2020-02-19 06:27:00 +01:00
|
|
|
fn admin_path() -> String {
|
|
|
|
format!("{}{}", CONFIG.domain_path(), ADMIN_PATH)
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
#[get("/", rank = 2)]
|
2019-01-25 18:23:51 +01:00
|
|
|
fn admin_login(flash: Option<FlashMessage>) -> ApiResult<Html<String>> {
|
2019-01-19 21:36:34 +01:00
|
|
|
// If there is an error, show it
|
2019-01-19 22:12:52 +01:00
|
|
|
let msg = flash.map(|msg| format!("{}: {}", msg.name(), msg.msg()));
|
2019-02-10 15:46:51 +01:00
|
|
|
let json = json!({"page_content": "admin/login", "version": VERSION, "error": msg});
|
2019-01-19 21:36:34 +01:00
|
|
|
|
|
|
|
// Return the page
|
2019-02-02 16:47:27 +01:00
|
|
|
let text = CONFIG.render_template(BASE_TEMPLATE, &json)?;
|
2019-01-19 21:36:34 +01:00
|
|
|
Ok(Html(text))
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-19 22:12:52 +01:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct LoginForm {
|
|
|
|
token: String,
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
#[post("/", data = "<data>")]
|
|
|
|
fn post_admin_login(data: Form<LoginForm>, mut cookies: Cookies, ip: ClientIp) -> Result<Redirect, Flash<Redirect>> {
|
|
|
|
let data = data.into_inner();
|
|
|
|
|
2019-01-20 17:43:56 +01:00
|
|
|
// If the token is invalid, redirect to login page
|
2019-01-19 21:36:34 +01:00
|
|
|
if !_validate_token(&data.token) {
|
|
|
|
error!("Invalid admin token. IP: {}", ip.ip);
|
|
|
|
Err(Flash::error(
|
2020-02-19 06:27:00 +01:00
|
|
|
Redirect::to(admin_path()),
|
2019-01-19 21:36:34 +01:00
|
|
|
"Invalid admin token, please try again.",
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
// If the token received is valid, generate JWT and save it as a cookie
|
|
|
|
let claims = generate_admin_claims();
|
|
|
|
let jwt = encode_jwt(&claims);
|
|
|
|
|
|
|
|
let cookie = Cookie::build(COOKIE_NAME, jwt)
|
2020-02-19 06:27:00 +01:00
|
|
|
.path(admin_path())
|
2019-01-20 15:36:33 +01:00
|
|
|
.max_age(chrono::Duration::minutes(20))
|
|
|
|
.same_site(SameSite::Strict)
|
2019-01-19 21:36:34 +01:00
|
|
|
.http_only(true)
|
|
|
|
.finish();
|
|
|
|
|
|
|
|
cookies.add(cookie);
|
2020-02-19 06:27:00 +01:00
|
|
|
Ok(Redirect::to(admin_path()))
|
2019-01-19 21:36:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn _validate_token(token: &str) -> bool {
|
2019-01-25 18:23:51 +01:00
|
|
|
match CONFIG.admin_token().as_ref() {
|
2019-01-19 21:36:34 +01:00
|
|
|
None => false,
|
2019-03-07 20:21:50 +01:00
|
|
|
Some(t) => crate::crypto::ct_eq(t.trim(), token.trim()),
|
2019-01-19 21:36:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-02 16:47:27 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct AdminTemplateData {
|
|
|
|
page_content: String,
|
2019-02-10 15:46:51 +01:00
|
|
|
version: Option<&'static str>,
|
|
|
|
users: Vec<Value>,
|
2019-02-03 00:22:18 +01:00
|
|
|
config: Value,
|
2019-06-02 00:08:52 +02:00
|
|
|
can_backup: bool,
|
2019-12-27 18:37:14 +01:00
|
|
|
logged_in: bool,
|
2019-02-02 16:47:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AdminTemplateData {
|
|
|
|
fn new(users: Vec<Value>) -> Self {
|
|
|
|
Self {
|
|
|
|
page_content: String::from("admin/page"),
|
2019-02-10 15:46:51 +01:00
|
|
|
version: VERSION,
|
|
|
|
users,
|
2019-02-03 00:22:18 +01:00
|
|
|
config: CONFIG.prepare_json(),
|
2019-06-02 00:08:52 +02:00
|
|
|
can_backup: *CAN_BACKUP,
|
2019-12-27 18:37:14 +01:00
|
|
|
logged_in: true,
|
2019-02-02 16:47:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(self) -> Result<String, Error> {
|
|
|
|
CONFIG.render_template(BASE_TEMPLATE, &self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
#[get("/", rank = 1)]
|
2019-01-25 18:23:51 +01:00
|
|
|
fn admin_page(_token: AdminToken, conn: DbConn) -> ApiResult<Html<String>> {
|
2018-12-18 18:52:58 +01:00
|
|
|
let users = User::get_all(&conn);
|
2018-12-18 01:53:21 +01:00
|
|
|
let users_json: Vec<Value> = users.iter().map(|u| u.to_json(&conn)).collect();
|
2018-12-18 18:52:58 +01:00
|
|
|
|
2019-02-02 16:47:27 +01:00
|
|
|
let text = AdminTemplateData::new(users_json).render()?;
|
2019-01-19 21:36:34 +01:00
|
|
|
Ok(Html(text))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct InviteData {
|
2019-02-02 16:47:27 +01:00
|
|
|
email: String,
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 18:52:58 +01:00
|
|
|
#[post("/invite", data = "<data>")]
|
2019-02-02 16:47:27 +01:00
|
|
|
fn invite_user(data: Json<InviteData>, _token: AdminToken, conn: DbConn) -> EmptyResult {
|
|
|
|
let data: InviteData = data.into_inner();
|
|
|
|
let email = data.email.clone();
|
|
|
|
if User::find_by_mail(&data.email, &conn).is_some() {
|
2018-12-18 01:53:21 +01:00
|
|
|
err!("User already exists")
|
|
|
|
}
|
|
|
|
|
2019-04-12 23:42:42 +02:00
|
|
|
let mut user = User::new(email);
|
|
|
|
user.save(&conn)?;
|
|
|
|
|
2019-02-02 01:09:21 +01:00
|
|
|
if CONFIG.mail_enabled() {
|
2020-02-04 22:14:50 +01:00
|
|
|
mail::send_invite(&user.email, &user.uuid, None, None, &CONFIG.invitation_org_name(), None)
|
2019-01-08 15:11:16 +01:00
|
|
|
} else {
|
2019-02-22 20:25:50 +01:00
|
|
|
let invitation = Invitation::new(data.email);
|
2019-01-25 18:23:51 +01:00
|
|
|
invitation.save(&conn)
|
2019-01-04 16:32:51 +01:00
|
|
|
}
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
2019-12-01 21:15:14 +01:00
|
|
|
#[get("/logout")]
|
|
|
|
fn logout(mut cookies: Cookies) -> Result<Redirect, ()> {
|
|
|
|
cookies.remove(Cookie::named(COOKIE_NAME));
|
2020-02-19 06:27:00 +01:00
|
|
|
Ok(Redirect::to(admin_path()))
|
2019-12-01 21:15:14 +01:00
|
|
|
}
|
|
|
|
|
2019-04-05 20:49:58 +02:00
|
|
|
#[get("/users")]
|
2019-04-26 22:08:26 +02:00
|
|
|
fn get_users(_token: AdminToken, conn: DbConn) -> JsonResult {
|
2019-04-05 20:49:58 +02:00
|
|
|
let users = User::get_all(&conn);
|
|
|
|
let users_json: Vec<Value> = users.iter().map(|u| u.to_json(&conn)).collect();
|
|
|
|
|
|
|
|
Ok(Json(Value::Array(users_json)))
|
|
|
|
}
|
|
|
|
|
2018-12-18 18:52:58 +01:00
|
|
|
#[post("/users/<uuid>/delete")]
|
2019-01-25 18:23:51 +01:00
|
|
|
fn delete_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
|
2018-12-18 18:52:58 +01:00
|
|
|
let user = match User::find_by_uuid(&uuid, &conn) {
|
2018-12-18 01:53:21 +01:00
|
|
|
Some(user) => user,
|
2018-12-18 18:52:58 +01:00
|
|
|
None => err!("User doesn't exist"),
|
2018-12-18 01:53:21 +01:00
|
|
|
};
|
|
|
|
|
2019-01-25 18:23:51 +01:00
|
|
|
user.delete(&conn)
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 19:28:54 +01:00
|
|
|
#[post("/users/<uuid>/deauth")]
|
|
|
|
fn deauth_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
|
|
|
|
let mut user = match User::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("User doesn't exist"),
|
|
|
|
};
|
|
|
|
|
2019-02-16 23:06:26 +01:00
|
|
|
Device::delete_all_by_user(&user.uuid, &conn)?;
|
2019-01-26 19:28:54 +01:00
|
|
|
user.reset_security_stamp();
|
|
|
|
|
|
|
|
user.save(&conn)
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:13:06 +02:00
|
|
|
#[post("/users/<uuid>/remove-2fa")]
|
|
|
|
fn remove_2fa(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
|
|
|
|
let mut user = match User::find_by_uuid(&uuid, &conn) {
|
|
|
|
Some(user) => user,
|
|
|
|
None => err!("User doesn't exist"),
|
|
|
|
};
|
|
|
|
|
|
|
|
TwoFactor::delete_all_by_user(&user.uuid, &conn)?;
|
|
|
|
user.totp_recover = None;
|
|
|
|
user.save(&conn)
|
|
|
|
}
|
|
|
|
|
2019-03-07 21:08:33 +01:00
|
|
|
#[post("/users/update_revision")]
|
|
|
|
fn update_revision_users(_token: AdminToken, conn: DbConn) -> EmptyResult {
|
|
|
|
User::update_all_revisions(&conn)
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:09:21 +01:00
|
|
|
#[post("/config", data = "<data>")]
|
2019-02-02 16:47:27 +01:00
|
|
|
fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult {
|
|
|
|
let data: ConfigBuilder = data.into_inner();
|
|
|
|
CONFIG.update_config(data)
|
2019-02-02 01:09:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 17:32:13 +01:00
|
|
|
#[post("/config/delete")]
|
|
|
|
fn delete_config(_token: AdminToken) -> EmptyResult {
|
|
|
|
CONFIG.delete_user_config()
|
|
|
|
}
|
|
|
|
|
2019-05-03 15:46:29 +02:00
|
|
|
#[post("/config/backup_db")]
|
|
|
|
fn backup_db(_token: AdminToken) -> EmptyResult {
|
2019-06-02 00:08:52 +02:00
|
|
|
if *CAN_BACKUP {
|
|
|
|
backup_database()
|
|
|
|
} else {
|
|
|
|
err!("Can't back up current DB (either it's not SQLite or the 'sqlite' binary is not present)");
|
|
|
|
}
|
2019-05-03 15:46:29 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 01:53:21 +01:00
|
|
|
pub struct AdminToken {}
|
|
|
|
|
|
|
|
impl<'a, 'r> FromRequest<'a, 'r> for AdminToken {
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
2019-02-20 21:44:35 +01:00
|
|
|
if CONFIG.disable_admin_token() {
|
|
|
|
Outcome::Success(AdminToken {})
|
2019-03-03 16:11:55 +01:00
|
|
|
} else {
|
2019-02-20 21:44:35 +01:00
|
|
|
let mut cookies = request.cookies();
|
|
|
|
|
|
|
|
let access_token = match cookies.get(COOKIE_NAME) {
|
|
|
|
Some(cookie) => cookie.value(),
|
|
|
|
None => return Outcome::Forward(()), // If there is no cookie, redirect to login
|
|
|
|
};
|
|
|
|
|
|
|
|
let ip = match request.guard::<ClientIp>() {
|
|
|
|
Outcome::Success(ip) => ip.ip,
|
|
|
|
_ => err_handler!("Error getting Client IP"),
|
|
|
|
};
|
|
|
|
|
|
|
|
if decode_admin(access_token).is_err() {
|
|
|
|
// Remove admin cookie
|
|
|
|
cookies.remove(Cookie::named(COOKIE_NAME));
|
|
|
|
error!("Invalid or expired admin JWT. IP: {}.", ip);
|
|
|
|
return Outcome::Forward(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Outcome::Success(AdminToken {})
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 18:52:58 +01:00
|
|
|
}
|