Skip to content

Commit

Permalink
Remove legacy endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 17, 2024
1 parent ab0d20f commit df6d927
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 273 deletions.
138 changes: 43 additions & 95 deletions src/admin/service.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use super::Admin;
use crate::Result;
use crate::{discord, Error};
use actix_web::{http::header::HeaderMap, HttpRequest};
use deadpool_sqlite::Pool;
use rusqlite::Connection;
use tracing::warn;

#[cfg(test)]
pub async fn mock_admin(password: &str, pool: &Pool) -> Admin {
let password = password.to_string();
pool.get()
.await
.unwrap()
.interact(move |conn| Admin::insert("test", &password, conn))
.await
.unwrap()
.unwrap()
.unwrap()
}
//#[cfg(test)]
//pub async fn mock_admin(password: &str, pool: &Pool) -> Admin {
// let password = password.to_string();
// pool.get()
// .await
// .unwrap()
// .interact(move |conn| Admin::insert("test", &password, conn))
// .await
// .unwrap()
// .unwrap()
// .unwrap()
//}

pub async fn check_rpc(password: &str, action: &str, pool: &Pool) -> Result<Admin> {
let password = password.to_string();
Expand All @@ -42,97 +39,48 @@ pub async fn check_rpc(password: &str, action: &str, pool: &Pool) -> Result<Admi
Ok(admin)
}

pub async fn check(req: &HttpRequest, pool: &Pool) -> Result<Admin> {
let headers = req.headers().clone();
let guard = pool.get().await.unwrap();
let conn = guard.lock().unwrap();
get_admin(&conn, &headers).await
}

pub async fn get_admin(db: &Connection, headers: &HeaderMap) -> Result<Admin> {
let auth_header = headers
.get("Authorization")
.map(|it| it.to_str().unwrap_or(""))
.unwrap_or("");
if auth_header.len() == 0 {
Err(Error::HttpUnauthorized(
"Authorization header is missing".into(),
))?
}
let auth_header_parts: Vec<&str> = auth_header.split(" ").collect();
if auth_header_parts.len() != 2 {
Err(Error::HttpUnauthorized(
"Authorization header is invalid".into(),
))?
}
let password = auth_header_parts[1];
let admin = Admin::select_by_password(password, db)?;
match admin {
Some(admin) => {
return Ok(admin);
}
None => {
let log_message = "Someone tried and failed to access admin API";
warn!(log_message);
discord::send_message_to_channel(log_message, discord::CHANNEL_API).await;
Err(Error::HttpUnauthorized("Invalid bearer token".into()))?
}
}
}

#[cfg(test)]
mod tests {
use crate::test::mock_state;
use crate::{Error, Result};
use actix_web::test::{self, TestRequest};
use actix_web::HttpRequest;
use actix_web::{
dev::Response,
get,
web::{scope, Data},
App, Responder,
};
use deadpool_sqlite::Pool;
use std::sync::Arc;
use crate::Result;

#[actix_web::test]
async fn no_header() -> Result<()> {
let state = mock_state().await;
super::mock_admin("test", &state.pool).await;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
.service(scope("/").service(get)),
)
.await;
let req = TestRequest::get().uri("/").to_request();
let res = test::call_service(&app, req).await;
assert_eq!(401, res.status().as_u16());
//let state = mock_state().await;
//super::mock_admin("test", &state.pool).await;
//let app = test::init_service(
// App::new()
// .app_data(Data::new(state.pool))
// .service(scope("/").service(get)),
//)
//.await;
//let req = TestRequest::get().uri("/").to_request();
//let res = test::call_service(&app, req).await;
//assert_eq!(401, res.status().as_u16());
Ok(())
}

#[actix_web::test]
async fn valid_token() -> Result<()> {
let state = mock_state().await;
super::mock_admin("test", &state.pool).await;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
.service(scope("/").service(get)),
)
.await;
let req = TestRequest::get()
.uri("/")
.append_header(("Authorization", "Bearer test"))
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(200, res.status().as_u16());
//let state = mock_state().await;
//super::mock_admin("test", &state.pool).await;
//let app = test::init_service(
// App::new()
// .app_data(Data::new(state.pool))
// .service(scope("/").service(get)),
//)
//.await;
//let req = TestRequest::get()
// .uri("/")
// .append_header(("Authorization", "Bearer test"))
// .to_request();
//let res = test::call_service(&app, req).await;
//assert_eq!(200, res.status().as_u16());
Ok(())
}

#[get("")]
async fn get(req: HttpRequest, pool: Data<Arc<Pool>>) -> Result<impl Responder, Error> {
super::check(&req, &pool).await?;
Ok(Response::ok())
}
//#[get("")]
//async fn get(req: HttpRequest, pool: Data<Arc<Pool>>) -> Result<impl Responder, Error> {
// super::check(&req, &pool).await?;
// Ok(Response::ok())
//}
}
176 changes: 0 additions & 176 deletions src/element/admin.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/element/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod model;
pub use model::Element;
pub mod admin;
pub mod service;
pub mod v2;
pub mod v3;
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ async fn main() -> Result<()> {
.wrap(from_fn(ban::check_if_banned))
.service(
scope("elements")
.service(element::admin::patch)
.service(element::v2::get)
.service(element::v2::get_by_id),
)
Expand Down

0 comments on commit df6d927

Please sign in to comment.