Skip to content

Commit

Permalink
Merge pull request #256 from Ugo-X/testnet
Browse files Browse the repository at this point in the history
Replace admin auth macro by middleware
  • Loading branch information
Marchand-Nicolas authored Sep 5, 2024
2 parents 7b07258 + c3bbd47 commit 6b262c3
Show file tree
Hide file tree
Showing 36 changed files with 249 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "c974e5c
starknet-id = { git = "https://github.com/starknet-id/starknet-id.rs.git", rev = "2b30c2453b96789a628c86d2edebb1023fa2e77d" }
serde_derive = "1.0.183"
env_logger = "0.10.0"
axum_auto_routes = { git = "https://github.com/Th0rgal/axum_auto_routes.git", rev = "f9e1d2083e887cd264642359c4aa851938da6f09" }
axum_auto_routes = { git = "https://github.com/Th0rgal/axum_auto_routes.git", rev = "4bcae49628a657ed4bdc1749dfd4f1221ffaffe7" }
axum = "0.6.17"
toml = "0.5.10"
serde = { version = "1.0.152", features = ["derive"] }
Expand Down
5 changes: 5 additions & 0 deletions mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod create_quest;
pub(crate) mod get_quest;
pub mod get_quests;
pub mod get_tasks;
pub mod update_quest;
2 changes: 1 addition & 1 deletion src/endpoints/achievements/verify_seniority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ pub async fn handler(
}
Err(e) => get_error(e),
}
}
}
18 changes: 8 additions & 10 deletions src/endpoints/admin/balance/create_balance.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestDocument, QuestTaskDocument};
use crate::models::{QuestDocument, QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{Extension, State},
http::StatusCode,
response::{IntoResponse, Json},
response::{IntoResponse, Json}
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
Expand All @@ -26,13 +25,12 @@ pub_struct!(Deserialize; CreateBalance {
cta: String,
});

#[route(post, "/admin/tasks/balance/create")]
#[route(post, "/admin/tasks/balance/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateBalance>,
Extension(sub): Extension<String>,
Json(body): Json<CreateBalance>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");
// Get the last id in increasing order
let last_id_filter = doc! {};
Expand All @@ -41,7 +39,7 @@ pub async fn handler(

let quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(user, &quests_collection, &(body.quest_id as i64)).await;
let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await;
if !res {
return get_error("Error creating task".to_string());
};
Expand Down
19 changes: 9 additions & 10 deletions src/endpoints/admin/balance/update_balance.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::models::{JWTClaims, QuestTaskDocument};
use crate::utils::verify_task_auth;
use crate::models::QuestTaskDocument;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use crate::utils::verify_task_auth;

use axum::{
extract::State,
extract::{Extension, State},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
Expand All @@ -30,16 +30,15 @@ fn field_element_to_bson(fe: &FieldElement) -> mongodb::bson::Bson {
mongodb::bson::Bson::String(fe.to_string())
}

#[route(post, "/admin/tasks/balance/update")]
#[route(post, "/admin/tasks/balance/update", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateBalance>,
Extension(sub): Extension<String>,
Json(body): Json<CreateBalance>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");

let res = verify_task_auth(user, &collection, &(body.id as i32)).await;
let res = verify_task_auth(sub, &collection, &(body.id as i32)).await;
if !res {
return get_error("Error updating tasks".to_string());
}
Expand Down
18 changes: 8 additions & 10 deletions src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestDocument, QuestTaskDocument};
use crate::models::{QuestDocument, QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{State, Extension},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
Expand All @@ -24,13 +23,12 @@ pub_struct!(Deserialize; CreateCustom {
api: String,
});

#[route(post, "/admin/tasks/custom/create")]
#[route(post, "/admin/tasks/custom/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateCustom>,
Extension(sub): Extension<String>,
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");
// Get the last id in increasing order
let last_id_filter = doc! {};
Expand All @@ -39,11 +37,11 @@ pub async fn handler(

let quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(user, &quests_collection, &(body.quest_id as i64)).await;
let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await;
if !res {
return get_error("Error creating task".to_string());
};

let mut next_id = 1;
if let Some(doc) = last_doc {
let last_id = doc.id;
Expand Down
20 changes: 9 additions & 11 deletions src/endpoints/admin/custom/update_custom.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestTaskDocument};
use crate::models::QuestTaskDocument;
use crate::utils::verify_task_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{State, Extension},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
Expand All @@ -25,20 +24,19 @@ pub_struct!(Deserialize; CreateCustom {
href: Option<String>,
});

#[route(post, "/admin/tasks/custom/update")]
#[route(post, "/admin/tasks/custom/update", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateCustom>,
Extension(sub): Extension<String>,
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");

let res = verify_task_auth(user, &collection, &(body.id as i32)).await;
let res = verify_task_auth(sub, &collection, &(body.id as i32)).await;
if !res {
return get_error("Error updating tasks".to_string());
}

// filter to get existing quest
let filter = doc! {
"id": &body.id,
Expand Down
14 changes: 6 additions & 8 deletions src/endpoints/admin/delete_task.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestTaskDocument};
use crate::models::QuestTaskDocument;
use crate::utils::verify_task_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{Extension, State},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
Expand All @@ -18,15 +17,14 @@ pub_struct!(Deserialize; DeleteTask {
id: i32,
});

#[route(post, "/admin/tasks/remove_task")]
#[route(post, "/admin/tasks/remove_task", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
Extension(sub): Extension<String>,
body: Json<DeleteTask>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");
let res = verify_task_auth(user, &collection, &body.id).await;
let res = verify_task_auth(sub, &collection, &body.id).await;
if !res {
return get_error("Error updating tasks".to_string());
}
Expand Down
16 changes: 7 additions & 9 deletions src/endpoints/admin/discord/create_discord.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestDocument, QuestTaskDocument};
use crate::models::{QuestDocument,QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{State, Extension},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
Expand All @@ -23,13 +22,12 @@ pub_struct!(Deserialize; CreateCustom {
guild_id: String,
});

#[route(post, "/admin/tasks/discord/create")]
#[route(post, "/admin/tasks/discord/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateCustom>,
Extension(sub): Extension<String>,
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");
// Get the last id in increasing order
let last_id_filter = doc! {};
Expand All @@ -38,7 +36,7 @@ pub async fn handler(

let quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(user, &quests_collection, &(body.quest_id as i64)).await;
let res = verify_quest_auth(sub, &quests_collection, &(body.quest_id as i64)).await;
if !res {
return get_error("Error creating task".to_string());
};
Expand Down
18 changes: 8 additions & 10 deletions src/endpoints/admin/discord/update_discord.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestTaskDocument};
use crate::models::QuestTaskDocument;
use crate::utils::verify_task_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{State, Extension},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use serde::Deserialize;
use serde_json::json;
Expand All @@ -22,16 +21,15 @@ pub_struct!(Deserialize; CreateCustom {
guild_id: Option<String>,
});

#[route(post, "/admin/tasks/discord/update")]
#[route(post, "/admin/tasks/discord/update", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateCustom>,
Extension(sub): Extension<String>,
Json(body): Json<CreateCustom>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");

let res = verify_task_auth(user, &collection, &(body.id as i32)).await;
let res = verify_task_auth(sub, &collection, &(body.id as i32)).await;
if !res {
return get_error("Error updating tasks".to_string());
}
Expand Down
16 changes: 7 additions & 9 deletions src/endpoints/admin/domain/create_domain.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::models::{JWTClaims, QuestDocument, QuestTaskDocument};
use crate::models::{QuestDocument, QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use axum::http::HeaderMap;
use crate::middleware::auth::auth_middleware;
use axum::{
extract::State,
extract::{State, Extension},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
Expand All @@ -21,17 +20,16 @@ pub_struct!(Deserialize; CreateTwitterFw {
quest_id: i64,
});

#[route(post, "/admin/tasks/domain/create")]
#[route(post, "/admin/tasks/domain/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
headers: HeaderMap,
body: Json<CreateTwitterFw>,
Extension(sub): Extension<String>,
Json(body): Json<CreateTwitterFw>,
) -> impl IntoResponse {
let user = check_authorization!(headers, &state.conf.auth.secret_key.as_ref()) as String;
let collection = state.db.collection::<QuestTaskDocument>("tasks");
let quests_collection = state.db.collection::<QuestDocument>("quests");

let res = verify_quest_auth(user, &quests_collection, &body.quest_id).await;
let res = verify_quest_auth(sub, &quests_collection, &body.quest_id).await;
if !res {
return get_error("Error creating task".to_string());
};
Expand Down
Loading

0 comments on commit 6b262c3

Please sign in to comment.