Create & list tokens
This commit is contained in:
36
matrixgw_backend/src/controllers/tokens_controller.rs
Normal file
36
matrixgw_backend/src/controllers/tokens_controller.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use crate::controllers::HttpResult;
|
||||
use crate::extractors::auth_extractor::{AuthExtractor, AuthenticatedMethod};
|
||||
use crate::users::{APIToken, BaseAPIToken};
|
||||
use actix_web::HttpResponse;
|
||||
|
||||
/// Create a new token
|
||||
pub async fn create(auth: AuthExtractor) -> HttpResult {
|
||||
if matches!(auth.method, AuthenticatedMethod::Token(_)) {
|
||||
return Ok(HttpResponse::Forbidden()
|
||||
.json("It is not allowed to create a token using another token!"));
|
||||
}
|
||||
|
||||
let base = auth.decode_json_body::<BaseAPIToken>()?;
|
||||
|
||||
if let Some(err) = base.check() {
|
||||
return Ok(HttpResponse::BadRequest().json(err));
|
||||
}
|
||||
|
||||
let token = APIToken::create(&auth.as_ref().email, base).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(token))
|
||||
}
|
||||
|
||||
/// Get the list of tokens of current user
|
||||
pub async fn get_list(auth: AuthExtractor) -> HttpResult {
|
||||
Ok(HttpResponse::Ok().json(
|
||||
APIToken::list_user(&auth.as_ref().email)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|mut t| {
|
||||
t.secret = String::new();
|
||||
t
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user