2022-04-26 02:05:09 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
use super::db::{self, UserId};
|
2022-05-19 17:09:44 +00:00
|
|
|
use crate::{AppState, Error, Result};
|
|
|
|
use anyhow::{anyhow, Context};
|
2022-04-26 02:05:09 +00:00
|
|
|
use axum::{
|
|
|
|
http::{self, Request, StatusCode},
|
|
|
|
middleware::Next,
|
|
|
|
response::IntoResponse,
|
|
|
|
};
|
2022-04-25 23:51:13 +00:00
|
|
|
use rand::thread_rng;
|
|
|
|
use scrypt::{
|
|
|
|
password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString},
|
|
|
|
Scrypt,
|
|
|
|
};
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
pub async fn validate_header<B>(mut req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
2022-04-26 02:05:09 +00:00
|
|
|
let mut auth_header = req
|
|
|
|
.headers()
|
|
|
|
.get(http::header::AUTHORIZATION)
|
|
|
|
.and_then(|header| header.to_str().ok())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"missing authorization header".to_string(),
|
|
|
|
)
|
|
|
|
})?
|
|
|
|
.split_whitespace();
|
|
|
|
|
|
|
|
let user_id = UserId(auth_header.next().unwrap_or("").parse().map_err(|_| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"missing user id in authorization header".to_string(),
|
|
|
|
)
|
|
|
|
})?);
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
let access_token = auth_header.next().ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"missing access token in authorization header".to_string(),
|
|
|
|
)
|
|
|
|
})?;
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
let mut credentials_valid = false;
|
2022-10-19 00:36:54 +00:00
|
|
|
let state = req.extensions().get::<Arc<AppState>>().unwrap();
|
|
|
|
if let Some(admin_token) = access_token.strip_prefix("ADMIN_TOKEN:") {
|
|
|
|
if state.config.api_token == admin_token {
|
2022-04-26 02:05:09 +00:00
|
|
|
credentials_valid = true;
|
2022-10-19 00:36:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for password_hash in state.db.get_access_token_hashes(user_id).await? {
|
|
|
|
if verify_access_token(access_token, &password_hash)? {
|
|
|
|
credentials_valid = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-04-26 02:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 22:32:23 +00:00
|
|
|
|
2022-04-26 17:15:41 +00:00
|
|
|
if credentials_valid {
|
2022-05-12 18:06:06 +00:00
|
|
|
let user = state
|
|
|
|
.db
|
|
|
|
.get_user_by_id(user_id)
|
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow!("user {} not found", user_id))?;
|
|
|
|
req.extensions_mut().insert(user);
|
2022-04-26 17:15:41 +00:00
|
|
|
Ok::<_, Error>(next.run(req).await)
|
|
|
|
} else {
|
2022-04-26 02:05:09 +00:00
|
|
|
Err(Error::Http(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
"invalid credentials".to_string(),
|
2022-04-26 17:15:41 +00:00
|
|
|
))
|
2022-04-26 02:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
|
2021-09-23 20:34:53 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> Result<String> {
|
|
|
|
let access_token = rpc::auth::random_token();
|
|
|
|
let access_token_hash =
|
|
|
|
hash_access_token(&access_token).context("failed to hash access token")?;
|
|
|
|
db.create_access_token_hash(user_id, &access_token_hash, MAX_ACCESS_TOKENS_TO_STORE)
|
|
|
|
.await?;
|
|
|
|
Ok(access_token)
|
|
|
|
}
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
fn hash_access_token(token: &str) -> Result<String> {
|
|
|
|
// Avoid slow hashing in debug mode.
|
|
|
|
let params = if cfg!(debug_assertions) {
|
|
|
|
scrypt::Params::new(1, 1, 1).unwrap()
|
|
|
|
} else {
|
|
|
|
scrypt::Params::recommended()
|
|
|
|
};
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
Ok(Scrypt
|
|
|
|
.hash_password(
|
|
|
|
token.as_bytes(),
|
|
|
|
None,
|
|
|
|
params,
|
|
|
|
&SaltString::generate(thread_rng()),
|
2022-05-19 17:55:55 +00:00
|
|
|
)
|
|
|
|
.map_err(anyhow::Error::new)?
|
2022-04-25 23:51:13 +00:00
|
|
|
.to_string())
|
|
|
|
}
|
2021-07-12 20:14:39 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
pub fn encrypt_access_token(access_token: &str, public_key: String) -> Result<String> {
|
|
|
|
let native_app_public_key =
|
|
|
|
rpc::auth::PublicKey::try_from(public_key).context("failed to parse app public key")?;
|
|
|
|
let encrypted_access_token = native_app_public_key
|
2022-08-10 21:39:24 +00:00
|
|
|
.encrypt_string(access_token)
|
2022-04-25 23:51:13 +00:00
|
|
|
.context("failed to encrypt access token with public key")?;
|
|
|
|
Ok(encrypted_access_token)
|
|
|
|
}
|
2021-12-22 15:19:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
pub fn verify_access_token(token: &str, hash: &str) -> Result<bool> {
|
2022-05-19 17:55:55 +00:00
|
|
|
let hash = PasswordHash::new(hash).map_err(anyhow::Error::new)?;
|
2022-04-25 23:51:13 +00:00
|
|
|
Ok(Scrypt.verify_password(token.as_bytes(), &hash).is_ok())
|
|
|
|
}
|