Integrated the new authentication into Actix

This commit is contained in:
Imbus 2023-12-22 22:00:49 +01:00
parent a7a0ea60d5
commit b34a06387f
5 changed files with 28 additions and 57 deletions

View file

@ -1,5 +1,5 @@
use crate::db::{db_get_comments, db_new_comment};
use crate::jwt::validate_token;
use crate::jwt::Authentication;
use crate::types::{CommentQueryParams, NewComment};
use crate::ServerState;
@ -31,8 +31,9 @@ pub async fn get_comments(
pub async fn new_comment(
data: Json<NewComment>,
state: Data<ServerState>,
auth: Data<Authentication>,
) -> Result<impl Responder> {
let user_claims = validate_token(&data.user_token);
let user_claims = auth.decode(&data.user_token);
// Bail if the token is invalid
if let Err(e) = user_claims {

View file

@ -1,5 +1,5 @@
use crate::db::{db_get_latest_posts, db_get_post, db_new_post};
use crate::jwt::validate_token;
use crate::jwt::Authentication;
use crate::types::{NewPost, PostQueryParams};
use crate::ServerState;
@ -23,8 +23,12 @@ pub async fn get_posts(
/// Creates a new post, requires a token in release mode
#[post("/posts")]
pub async fn new_post(new_post: Json<NewPost>, state: Data<ServerState>) -> Result<impl Responder> {
let user_claims = validate_token(&new_post.token);
pub async fn new_post(
new_post: Json<NewPost>,
state: Data<ServerState>,
auth: Data<Authentication>,
) -> Result<impl Responder> {
let user_claims = auth.decode(&new_post.token);
if let Err(e) = user_claims {
info!("Error validating token: {}", e);

View file

@ -1,5 +1,6 @@
use crate::db::{db_new_user, db_user_login};
use crate::jwt::token_factory;
use crate::jwt::Authentication;
// use crate::jwt::token_factory;
use crate::state::CaptchaState;
use crate::types::{AuthResponse, LoginData, RegisterData};
use crate::ServerState;
@ -14,6 +15,7 @@ pub async fn register(
data: Json<RegisterData>,
state: Data<ServerState>,
captcha_state: Data<CaptchaState>,
auth: Data<Authentication>,
) -> Result<impl Responder> {
if !captcha_state
.capthca_db
@ -30,7 +32,7 @@ pub async fn register(
info!("User: {} registered", &user.username);
Ok(HttpResponse::Ok().json(AuthResponse {
username: user.username.clone(),
token: token_factory(&user.username).unwrap(),
token: auth.encode(&user.username).unwrap(),
}))
}
None => {
@ -41,14 +43,18 @@ pub async fn register(
}
#[post("/login")]
pub async fn login(data: Json<LoginData>, state: Data<ServerState>) -> Result<impl Responder> {
pub async fn login(
data: Json<LoginData>,
state: Data<ServerState>,
auth: Data<Authentication>,
) -> Result<impl Responder> {
let result = db_user_login(data.username.clone(), data.password.clone(), &state.pool).await;
match result {
Some(_) => {
return Ok(HttpResponse::Ok().json(AuthResponse {
username: data.username.clone(),
token: token_factory(&data.username).unwrap(),
token: auth.encode(&data.username).unwrap(),
}));
}
None => {