2018-12-18 01:53:21 +01:00
|
|
|
use serde_json::Value;
|
|
|
|
|
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-01-25 18:23:51 +01:00
|
|
|
use crate::api::{ApiResult, EmptyResult, JsonUpcase};
|
2019-01-19 21:36:34 +01:00
|
|
|
use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp};
|
|
|
|
use crate::db::{models::*, DbConn};
|
|
|
|
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-01-25 18:23:51 +01:00
|
|
|
if CONFIG.admin_token().is_none() {
|
2019-01-19 21:36:34 +01:00
|
|
|
return Vec::new();
|
|
|
|
}
|
2018-12-18 01:53:21 +01:00
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
routes![admin_login, post_admin_login, admin_page, invite_user, delete_user]
|
|
|
|
}
|
2018-12-18 01:53:21 +01:00
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
const COOKIE_NAME: &'static str = "BWRS_ADMIN";
|
|
|
|
const ADMIN_PATH: &'static str = "/admin";
|
|
|
|
|
2019-01-19 22:12:52 +01:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct AdminTemplateData {
|
|
|
|
users: Vec<Value>,
|
|
|
|
page_content: String,
|
|
|
|
error: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AdminTemplateData {
|
|
|
|
fn login(error: Option<String>) -> Self {
|
|
|
|
Self {
|
|
|
|
users: Vec::new(),
|
2019-01-19 22:59:32 +01:00
|
|
|
page_content: String::from("admin/login"),
|
2019-01-19 22:12:52 +01:00
|
|
|
error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn admin(users: Vec<Value>) -> Self {
|
|
|
|
Self {
|
|
|
|
users,
|
2019-01-19 22:59:32 +01:00
|
|
|
page_content: String::from("admin/page"),
|
2019-01-19 22:12:52 +01:00
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(self) -> Result<String, Error> {
|
2019-01-20 17:43:56 +01:00
|
|
|
CONFIG.render_template("admin/base", &self)
|
2019-01-19 22:12:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-01-19 21:36:34 +01:00
|
|
|
|
|
|
|
// Return the page
|
2019-01-19 22:12:52 +01:00
|
|
|
let text = AdminTemplateData::login(msg).render()?;
|
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(
|
|
|
|
Redirect::to(ADMIN_PATH),
|
|
|
|
"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)
|
|
|
|
.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);
|
|
|
|
Ok(Redirect::to(ADMIN_PATH))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
Some(t) => t == token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-01-19 22:12:52 +01:00
|
|
|
let text = AdminTemplateData::admin(users_json).render()?;
|
2019-01-19 21:36:34 +01:00
|
|
|
Ok(Html(text))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct InviteData {
|
|
|
|
Email: String,
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
2018-12-18 18:52:58 +01:00
|
|
|
#[post("/invite", data = "<data>")]
|
2019-01-25 18:23:51 +01:00
|
|
|
fn invite_user(data: JsonUpcase<InviteData>, _token: AdminToken, conn: DbConn) -> EmptyResult {
|
2018-12-18 01:53:21 +01:00
|
|
|
let data: InviteData = data.into_inner().data;
|
2019-01-04 16:32:51 +01:00
|
|
|
let email = data.Email.clone();
|
2018-12-18 01:53:21 +01:00
|
|
|
if User::find_by_mail(&data.Email, &conn).is_some() {
|
|
|
|
err!("User already exists")
|
|
|
|
}
|
|
|
|
|
2019-01-25 18:23:51 +01:00
|
|
|
if !CONFIG.invitations_allowed() {
|
2018-12-19 21:52:53 +01:00
|
|
|
err!("Invitations are not allowed")
|
|
|
|
}
|
|
|
|
|
2019-01-25 18:23:51 +01:00
|
|
|
if let Some(ref mail_config) = CONFIG.mail() {
|
2019-01-04 16:32:51 +01:00
|
|
|
let mut user = User::new(email);
|
|
|
|
user.save(&conn)?;
|
|
|
|
let org_name = "bitwarden_rs";
|
2019-01-25 18:23:51 +01:00
|
|
|
mail::send_invite(&user.email, &user.uuid, None, None, &org_name, None, mail_config)
|
2019-01-08 15:11:16 +01:00
|
|
|
} else {
|
|
|
|
let mut 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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-01-19 21:36:34 +01:00
|
|
|
let mut cookies = request.cookies();
|
2018-12-18 18:52:58 +01:00
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
let access_token = match cookies.get(COOKIE_NAME) {
|
|
|
|
Some(cookie) => cookie.value(),
|
|
|
|
None => return Outcome::Forward(()), // If there is no cookie, redirect to login
|
2018-12-18 01:53:21 +01:00
|
|
|
};
|
|
|
|
|
2019-01-08 16:16:58 +01:00
|
|
|
let ip = match request.guard::<ClientIp>() {
|
2019-01-19 21:36:34 +01:00
|
|
|
Outcome::Success(ip) => ip.ip,
|
2019-01-08 16:16:58 +01:00
|
|
|
_ => err_handler!("Error getting Client IP"),
|
|
|
|
};
|
|
|
|
|
2019-01-19 21:36:34 +01:00
|
|
|
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(());
|
2018-12-18 01:53:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Outcome::Success(AdminToken {})
|
|
|
|
}
|
2018-12-18 18:52:58 +01:00
|
|
|
}
|