REST: extract and handle API tokens

and refactor handling of headers in the REST server while we're at it.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2020-10-07 13:10:37 +02:00 committed by Wolfgang Bumiller
parent e6dc35acb8
commit c30816c1f8
1 changed files with 76 additions and 35 deletions

View File

@ -531,35 +531,59 @@ async fn handle_static_file_download(filename: PathBuf) -> Result<Response<Body
} }
} }
fn extract_auth_data(headers: &http::HeaderMap) -> (Option<String>, Option<String>, Option<String>) { fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
let mut ticket = None;
let mut language = None;
if let Some(raw_cookie) = headers.get("COOKIE") { if let Some(raw_cookie) = headers.get("COOKIE") {
if let Ok(cookie) = raw_cookie.to_str() { if let Ok(cookie) = raw_cookie.to_str() {
ticket = tools::extract_cookie(cookie, "PBSAuthCookie"); return tools::extract_cookie(cookie, "PBSLangCookie");
language = tools::extract_cookie(cookie, "PBSLangCookie");
} }
} }
None
}
struct UserAuthData{
ticket: String,
csrf_token: Option<String>,
}
enum AuthData {
User(UserAuthData),
ApiToken(String),
}
fn extract_auth_data(headers: &http::HeaderMap) -> Option<AuthData> {
if let Some(raw_cookie) = headers.get("COOKIE") {
if let Ok(cookie) = raw_cookie.to_str() {
if let Some(ticket) = tools::extract_cookie(cookie, "PBSAuthCookie") {
let csrf_token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) { let csrf_token = match headers.get("CSRFPreventionToken").map(|v| v.to_str()) {
Some(Ok(v)) => Some(v.to_owned()), Some(Ok(v)) => Some(v.to_owned()),
_ => None, _ => None,
}; };
return Some(AuthData::User(UserAuthData {
ticket,
csrf_token,
}));
}
}
}
(ticket, csrf_token, language) match headers.get("AUTHORIZATION").map(|v| v.to_str()) {
Some(Ok(v)) => Some(AuthData::ApiToken(v.to_owned())),
_ => None,
}
} }
fn check_auth( fn check_auth(
method: &hyper::Method, method: &hyper::Method,
ticket: &Option<String>, auth_data: &AuthData,
csrf_token: &Option<String>,
user_info: &CachedUserInfo, user_info: &CachedUserInfo,
) -> Result<Authid, Error> { ) -> Result<Authid, Error> {
match auth_data {
AuthData::User(user_auth_data) => {
let ticket = user_auth_data.ticket.clone();
let ticket_lifetime = tools::ticket::TICKET_LIFETIME; let ticket_lifetime = tools::ticket::TICKET_LIFETIME;
let ticket = ticket.as_ref().map(String::as_str); let userid: Userid = Ticket::parse(&ticket)?
let userid: Userid = Ticket::parse(&ticket.ok_or_else(|| format_err!("missing ticket"))?)?
.verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?; .verify_with_time_frame(public_auth_key(), "PBS", None, -300..ticket_lifetime)?;
let auth_id = Authid::from(userid.clone()); let auth_id = Authid::from(userid.clone());
@ -568,14 +592,28 @@ fn check_auth(
} }
if method != hyper::Method::GET { if method != hyper::Method::GET {
if let Some(csrf_token) = csrf_token { if let Some(csrf_token) = &user_auth_data.csrf_token {
verify_csrf_prevention_token(csrf_secret(), &userid, &csrf_token, -300, ticket_lifetime)?; verify_csrf_prevention_token(csrf_secret(), &userid, &csrf_token, -300, ticket_lifetime)?;
} else { } else {
bail!("missing CSRF prevention token"); bail!("missing CSRF prevention token");
} }
} }
Ok(Authid::from(userid)) Ok(auth_id)
},
AuthData::ApiToken(api_token) => {
let mut parts = api_token.splitn(2, ':');
let tokenid = parts.next()
.ok_or_else(|| format_err!("failed to split API token header"))?;
let tokenid: Authid = tokenid.parse()?;
let tokensecret = parts.next()
.ok_or_else(|| format_err!("failed to split API token header"))?;
crate::config::token_shadow::verify_secret(&tokenid, &tokensecret)?;
Ok(tokenid)
}
}
} }
async fn handle_request( async fn handle_request(
@ -631,8 +669,11 @@ async fn handle_request(
} }
if auth_required { if auth_required {
let (ticket, csrf_token, _) = extract_auth_data(&parts.headers); let auth_result = match extract_auth_data(&parts.headers) {
match check_auth(&method, &ticket, &csrf_token, &user_info) { Some(auth_data) => check_auth(&method, &auth_data, &user_info),
None => Err(format_err!("no authentication credentials provided.")),
};
match auth_result {
Ok(authid) => rpcenv.set_auth_id(Some(authid.to_string())), Ok(authid) => rpcenv.set_auth_id(Some(authid.to_string())),
Err(err) => { Err(err) => {
// always delay unauthorized calls by 3 seconds (from start of request) // always delay unauthorized calls by 3 seconds (from start of request)
@ -685,14 +726,14 @@ async fn handle_request(
} }
if comp_len == 0 { if comp_len == 0 {
let (ticket, csrf_token, language) = extract_auth_data(&parts.headers); let language = extract_lang_header(&parts.headers);
if ticket != None { if let Some(auth_data) = extract_auth_data(&parts.headers) {
match check_auth(&method, &ticket, &csrf_token, &user_info) { match check_auth(&method, &auth_data, &user_info) {
Ok(auth_id) => { Ok(auth_id) if !auth_id.is_token() => {
let userid = auth_id.user(); let userid = auth_id.user();
let new_csrf_token = assemble_csrf_prevention_token(csrf_secret(), userid); let new_csrf_token = assemble_csrf_prevention_token(csrf_secret(), userid);
return Ok(get_index(Some(userid.clone()), Some(new_csrf_token), language, &api, parts)); return Ok(get_index(Some(userid.clone()), Some(new_csrf_token), language, &api, parts));
} },
_ => { _ => {
tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await; tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await;
return Ok(get_index(None, None, language, &api, parts)); return Ok(get_index(None, None, language, &api, parts));