You've already forked AstralRinth
forked from didirus/AstralRinth
* Update Rust version * Update async-compression 0.4.25 -> 0.4.27 * Update async-tungstenite 0.29.1 -> 0.30.0 * Update bytemuck 1.23.0 -> 1.23.1 * Update clap 4.5.40 -> 4.5.43 * Update deadpool-redis 0.21.1 -> 0.22.0 and redis 0.31.0 -> 0.32.4 * Update enumset 1.1.6 -> 1.1.7 * Update hyper-util 0.1.14 -> 0.1.16 * Update indexmap 2.9.0 -> 2.10.0 * Update indicatif 0.17.11 -> 0.18.0 * Update jemalloc_pprof 0.7.0 -> 0.8.1 * Update lettre 0.11.17 -> 0.11.18 * Update meilisearch-sdk 0.28.0 -> 0.29.1 * Update notify 8.0.0 -> 8.2.0 and notify-debouncer-mini 0.6.0 -> 0.7.0 * Update quick-xml 0.37.5 -> 0.38.1 * Fix theseus lint * Update reqwest 0.12.20 -> 0.12.22 * Cargo fmt in theseus * Update rgb 0.8.50 -> 0.8.52 * Update sentry 0.41.0 -> 0.42.0 and sentry-actix 0.41.0 -> 0.42.0 * Update serde_json 1.0.140 -> 1.0.142 * Update serde_with 3.13.0 -> 3.14.0 * Update spdx 0.10.8 -> 0.10.9 * Update sysinfo 0.35.2 -> 0.36.1 * Update tauri suite * Fix build by updating mappings * Update tokio 1.45.1 -> 1.47.1 and tokio-util 0.7.15 -> 0.7.16 * Update tracing-actix-web 0.7.18 -> 0.7.19 * Update zip 4.2.0 -> 4.3.0 * Misc Cargo.lock updates * Update Dockerfiles
298 lines
7.5 KiB
Rust
298 lines
7.5 KiB
Rust
use crate::database;
|
|
use crate::database::models::generate_pat_id;
|
|
|
|
use crate::auth::get_user_from_headers;
|
|
use crate::routes::ApiError;
|
|
|
|
use crate::database::redis::RedisPool;
|
|
use actix_web::web::{self, Data};
|
|
use actix_web::{HttpRequest, HttpResponse, delete, get, patch, post};
|
|
use chrono::{DateTime, Utc};
|
|
use rand::Rng;
|
|
use rand::distributions::Alphanumeric;
|
|
use rand_chacha::ChaCha20Rng;
|
|
use rand_chacha::rand_core::SeedableRng;
|
|
|
|
use crate::models::pats::{PersonalAccessToken, Scopes};
|
|
use crate::queue::session::AuthQueue;
|
|
use crate::util::validate::validation_errors_to_string;
|
|
use serde::Deserialize;
|
|
use sqlx::postgres::PgPool;
|
|
use validator::Validate;
|
|
|
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
|
cfg.service(get_pats);
|
|
cfg.service(create_pat);
|
|
cfg.service(edit_pat);
|
|
cfg.service(delete_pat);
|
|
}
|
|
|
|
#[get("pat")]
|
|
pub async fn get_pats(
|
|
req: HttpRequest,
|
|
pool: Data<PgPool>,
|
|
redis: Data<RedisPool>,
|
|
session_queue: Data<AuthQueue>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let user = get_user_from_headers(
|
|
&req,
|
|
&**pool,
|
|
&redis,
|
|
&session_queue,
|
|
Scopes::PAT_READ,
|
|
)
|
|
.await?
|
|
.1;
|
|
|
|
let pat_ids =
|
|
database::models::pat_item::DBPersonalAccessToken::get_user_pats(
|
|
user.id.into(),
|
|
&**pool,
|
|
&redis,
|
|
)
|
|
.await?;
|
|
let pats = database::models::pat_item::DBPersonalAccessToken::get_many_ids(
|
|
&pat_ids, &**pool, &redis,
|
|
)
|
|
.await?;
|
|
|
|
Ok(HttpResponse::Ok().json(
|
|
pats.into_iter()
|
|
.map(|x| PersonalAccessToken::from(x, false))
|
|
.collect::<Vec<_>>(),
|
|
))
|
|
}
|
|
|
|
#[derive(Deserialize, Validate)]
|
|
pub struct NewPersonalAccessToken {
|
|
pub scopes: Scopes,
|
|
#[validate(length(min = 3, max = 255))]
|
|
pub name: String,
|
|
pub expires: DateTime<Utc>,
|
|
}
|
|
|
|
#[post("pat")]
|
|
pub async fn create_pat(
|
|
req: HttpRequest,
|
|
info: web::Json<NewPersonalAccessToken>,
|
|
pool: Data<PgPool>,
|
|
redis: Data<RedisPool>,
|
|
session_queue: Data<AuthQueue>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
info.0.validate().map_err(|err| {
|
|
ApiError::InvalidInput(validation_errors_to_string(err, None))
|
|
})?;
|
|
|
|
if info.scopes.is_restricted() {
|
|
return Err(ApiError::InvalidInput(
|
|
"Invalid scopes requested!".to_string(),
|
|
));
|
|
}
|
|
if info.expires < Utc::now() {
|
|
return Err(ApiError::InvalidInput(
|
|
"Expire date must be in the future!".to_string(),
|
|
));
|
|
}
|
|
|
|
let user = get_user_from_headers(
|
|
&req,
|
|
&**pool,
|
|
&redis,
|
|
&session_queue,
|
|
Scopes::PAT_CREATE,
|
|
)
|
|
.await?
|
|
.1;
|
|
|
|
let mut transaction = pool.begin().await?;
|
|
|
|
let id = generate_pat_id(&mut transaction).await?;
|
|
|
|
let token = ChaCha20Rng::from_entropy()
|
|
.sample_iter(&Alphanumeric)
|
|
.take(60)
|
|
.map(char::from)
|
|
.collect::<String>();
|
|
let token = format!("mrp_{token}");
|
|
|
|
let name = info.name.clone();
|
|
database::models::pat_item::DBPersonalAccessToken {
|
|
id,
|
|
name: name.clone(),
|
|
access_token: token.clone(),
|
|
scopes: info.scopes,
|
|
user_id: user.id.into(),
|
|
created: Utc::now(),
|
|
expires: info.expires,
|
|
last_used: None,
|
|
}
|
|
.insert(&mut transaction)
|
|
.await?;
|
|
|
|
transaction.commit().await?;
|
|
database::models::pat_item::DBPersonalAccessToken::clear_cache(
|
|
vec![(None, None, Some(user.id.into()))],
|
|
&redis,
|
|
)
|
|
.await?;
|
|
|
|
Ok(HttpResponse::Ok().json(PersonalAccessToken {
|
|
id: id.into(),
|
|
name,
|
|
access_token: Some(token),
|
|
scopes: info.scopes,
|
|
user_id: user.id,
|
|
created: Utc::now(),
|
|
expires: info.expires,
|
|
last_used: None,
|
|
}))
|
|
}
|
|
|
|
#[derive(Deserialize, Validate)]
|
|
pub struct ModifyPersonalAccessToken {
|
|
pub scopes: Option<Scopes>,
|
|
#[validate(length(min = 3, max = 255))]
|
|
pub name: Option<String>,
|
|
pub expires: Option<DateTime<Utc>>,
|
|
}
|
|
|
|
#[patch("pat/{id}")]
|
|
pub async fn edit_pat(
|
|
req: HttpRequest,
|
|
id: web::Path<(String,)>,
|
|
info: web::Json<ModifyPersonalAccessToken>,
|
|
pool: Data<PgPool>,
|
|
redis: Data<RedisPool>,
|
|
session_queue: Data<AuthQueue>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
info.0.validate().map_err(|err| {
|
|
ApiError::InvalidInput(validation_errors_to_string(err, None))
|
|
})?;
|
|
|
|
let user = get_user_from_headers(
|
|
&req,
|
|
&**pool,
|
|
&redis,
|
|
&session_queue,
|
|
Scopes::PAT_WRITE,
|
|
)
|
|
.await?
|
|
.1;
|
|
|
|
let id = id.into_inner().0;
|
|
let pat = database::models::pat_item::DBPersonalAccessToken::get(
|
|
&id, &**pool, &redis,
|
|
)
|
|
.await?;
|
|
|
|
if let Some(pat) = pat
|
|
&& pat.user_id == user.id.into()
|
|
{
|
|
let mut transaction = pool.begin().await?;
|
|
|
|
if let Some(scopes) = &info.scopes {
|
|
if scopes.is_restricted() {
|
|
return Err(ApiError::InvalidInput(
|
|
"Invalid scopes requested!".to_string(),
|
|
));
|
|
}
|
|
|
|
sqlx::query!(
|
|
"
|
|
UPDATE pats
|
|
SET scopes = $1
|
|
WHERE id = $2
|
|
",
|
|
scopes.bits() as i64,
|
|
pat.id.0
|
|
)
|
|
.execute(&mut *transaction)
|
|
.await?;
|
|
}
|
|
if let Some(name) = &info.name {
|
|
sqlx::query!(
|
|
"
|
|
UPDATE pats
|
|
SET name = $1
|
|
WHERE id = $2
|
|
",
|
|
name,
|
|
pat.id.0
|
|
)
|
|
.execute(&mut *transaction)
|
|
.await?;
|
|
}
|
|
if let Some(expires) = &info.expires {
|
|
if expires < &Utc::now() {
|
|
return Err(ApiError::InvalidInput(
|
|
"Expire date must be in the future!".to_string(),
|
|
));
|
|
}
|
|
|
|
sqlx::query!(
|
|
"
|
|
UPDATE pats
|
|
SET expires = $1
|
|
WHERE id = $2
|
|
",
|
|
expires,
|
|
pat.id.0
|
|
)
|
|
.execute(&mut *transaction)
|
|
.await?;
|
|
}
|
|
|
|
transaction.commit().await?;
|
|
database::models::pat_item::DBPersonalAccessToken::clear_cache(
|
|
vec![(Some(pat.id), Some(pat.access_token), Some(pat.user_id))],
|
|
&redis,
|
|
)
|
|
.await?;
|
|
}
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
}
|
|
|
|
#[delete("pat/{id}")]
|
|
pub async fn delete_pat(
|
|
req: HttpRequest,
|
|
id: web::Path<(String,)>,
|
|
pool: Data<PgPool>,
|
|
redis: Data<RedisPool>,
|
|
session_queue: Data<AuthQueue>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let user = get_user_from_headers(
|
|
&req,
|
|
&**pool,
|
|
&redis,
|
|
&session_queue,
|
|
Scopes::PAT_DELETE,
|
|
)
|
|
.await?
|
|
.1;
|
|
let id = id.into_inner().0;
|
|
let pat = database::models::pat_item::DBPersonalAccessToken::get(
|
|
&id, &**pool, &redis,
|
|
)
|
|
.await?;
|
|
|
|
if let Some(pat) = pat
|
|
&& pat.user_id == user.id.into()
|
|
{
|
|
let mut transaction = pool.begin().await?;
|
|
database::models::pat_item::DBPersonalAccessToken::remove(
|
|
pat.id,
|
|
&mut transaction,
|
|
)
|
|
.await?;
|
|
transaction.commit().await?;
|
|
database::models::pat_item::DBPersonalAccessToken::clear_cache(
|
|
vec![(Some(pat.id), Some(pat.access_token), Some(pat.user_id))],
|
|
&redis,
|
|
)
|
|
.await?;
|
|
}
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
}
|