proxmox-backup/src/api2/access.rs

101 lines
2.8 KiB
Rust
Raw Normal View History

2019-01-30 14:14:20 +00:00
use failure::*;
use serde_json::{json, Value};
use proxmox::api::api;
use proxmox::api::router::{Router, SubdirMap};
use proxmox::sortable;
use proxmox::{http_err, list_subdirs_api_method};
2019-01-30 14:14:20 +00:00
use crate::tools;
use crate::tools::ticket::*;
use crate::auth_helpers::*;
fn authenticate_user(username: &str, password: &str) -> Result<(), Error> {
let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
if password.starts_with("PBS:") {
if let Ok((_age, Some(ticket_username))) = tools::ticket::verify_rsa_ticket(public_auth_key(), "PBS", password, None, -300, ticket_lifetime) {
if ticket_username == username {
return Ok(());
} else {
bail!("ticket login failed - wrong username");
}
}
}
2020-04-08 09:57:14 +00:00
crate::auth::authenticate_user(username, password)
2019-01-30 14:14:20 +00:00
}
#[api(
input: {
properties: {
username: {
type: String,
description: "User name.",
max_length: 64,
},
password: {
type: String,
description: "The secret password. This can also be a valid ticket.",
},
},
},
returns: {
properties: {
username: {
type: String,
description: "User name.",
},
ticket: {
type: String,
description: "Auth ticket.",
},
CSRFPreventionToken: {
type: String,
description: "Cross Site Request Forgery Prevention Token.",
},
},
},
protected: true,
)]
/// Create or verify authentication ticket.
///
/// Returns: An authentication ticket with additional infos.
fn create_ticket(username: String, password: String) -> Result<Value, Error> {
match authenticate_user(&username, &password) {
2019-01-30 14:14:20 +00:00
Ok(_) => {
let ticket = assemble_rsa_ticket( private_auth_key(), "PBS", Some(&username), None)?;
2019-01-30 14:14:20 +00:00
let token = assemble_csrf_prevention_token(csrf_secret(), &username);
2019-01-30 14:14:20 +00:00
log::info!("successful auth for user '{}'", username);
2019-10-26 09:36:01 +00:00
Ok(json!({
2019-01-30 14:14:20 +00:00
"username": username,
"ticket": ticket,
"CSRFPreventionToken": token,
2019-10-26 09:36:01 +00:00
}))
2019-01-30 14:14:20 +00:00
}
Err(err) => {
let client_ip = "unknown"; // $rpcenv->get_client_ip() || '';
2019-01-30 14:14:20 +00:00
log::error!("authentication failure; rhost={} user={} msg={}", client_ip, username, err.to_string());
2019-10-26 09:36:01 +00:00
Err(http_err!(UNAUTHORIZED, "permission check failed.".into()))
2019-01-30 14:14:20 +00:00
}
}
}
#[sortable]
2019-11-21 08:36:41 +00:00
const SUBDIRS: SubdirMap = &[
(
"ticket", &Router::new()
.post(&API_METHOD_CREATE_TICKET)
2019-11-21 08:36:41 +00:00
)
];
pub const ROUTER: Router = Router::new()
.get(&list_subdirs_api_method!(SUBDIRS))
.subdirs(SUBDIRS);