feat: guard get form submissions route with bearer authentication. Only
ci/woodpecker/push/woodpecker Pipeline failed Details

dashboard will have access
This commit is contained in:
Aravinth Manivannan 2022-12-29 01:38:51 +05:30
parent 647ec8f715
commit 4416027253
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
4 changed files with 48 additions and 4 deletions

16
Cargo.lock generated
View File

@ -193,6 +193,21 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "actix-web-httpauth"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dda62cf04bc3a9ad2ea8f314f721951cfdb4cdacec4e984d20e77c7bb170991"
dependencies = [
"actix-utils",
"actix-web",
"base64",
"futures-core",
"futures-util",
"log",
"pin-project-lite",
]
[[package]] [[package]]
name = "adler" name = "adler"
version = "1.0.2" version = "1.0.2"
@ -738,6 +753,7 @@ dependencies = [
"actix-rt", "actix-rt",
"actix-web", "actix-web",
"actix-web-codegen-const-routes", "actix-web-codegen-const-routes",
"actix-web-httpauth",
"argon2-creds", "argon2-creds",
"base64", "base64",
"clap", "clap",

View File

@ -12,6 +12,7 @@ build = "build.rs"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-httpauth = "0.8.0"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] } futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.17" log = "0.4.17"

View File

@ -17,6 +17,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{web, HttpResponse, Responder}; use actix_web::{web, HttpResponse, Responder};
use actix_web_httpauth::middleware::HttpAuthentication;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::db::FormSubmission; use crate::db::FormSubmission;
@ -24,6 +25,8 @@ use crate::errors::*;
use crate::AppCtx; use crate::AppCtx;
use crate::*; use crate::*;
use super::bearerauth;
pub mod routes { pub mod routes {
use super::*; use super::*;
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)] #[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
@ -95,7 +98,10 @@ impl From<FormSubmission> for FormSubmissionResp {
} }
} }
#[actix_web_codegen_const_routes::post(path = "API_V1_ROUTES.forms.get_all")] #[actix_web_codegen_const_routes::post(
path = "API_V1_ROUTES.forms.get_all",
wrap = "HttpAuthentication::bearer(bearerauth)"
)]
async fn list_all( async fn list_all(
ctx: AppCtx, ctx: AppCtx,
payload: web::Json<Table>, payload: web::Json<Table>,
@ -175,6 +181,8 @@ pub mod tests {
let upload_path = API_V1_ROUTES.forms.get_submit(host, path); let upload_path = API_V1_ROUTES.forms.get_submit(host, path);
println!("{upload_path}"); println!("{upload_path}");
let _ = ctx.db.delete_site(host).await;
let foo = Foo { let foo = Foo {
foo: "Foo".into(), foo: "Foo".into(),
num: 2.33, num: 2.33,
@ -226,6 +234,10 @@ pub mod tests {
test::TestRequest::post() test::TestRequest::post()
.set_json(&payload) .set_json(&payload)
.uri(&get_sub_route) .uri(&get_sub_route)
.insert_header((
header::AUTHORIZATION,
format!("Bearer {}", ctx.settings.dash.api_key),
))
.to_request(), .to_request(),
) )
.await; .await;

View File

@ -14,16 +14,17 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use crate::errors::ServiceError;
use crate::AppCtx;
use actix_web::web; use actix_web::web;
use actix_web::{dev::ServiceRequest, Error};
use actix_web_httpauth::extractors::bearer::BearerAuth;
pub mod forms; pub mod forms;
pub mod meta; pub mod meta;
pub const API_V1_ROUTES: routes::Routes = routes::Routes::new(); pub const API_V1_ROUTES: routes::Routes = routes::Routes::new();
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SignedInUser(String);
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
meta::services(cfg); meta::services(cfg);
forms::services(cfg); forms::services(cfg);
@ -47,3 +48,17 @@ pub mod routes {
} }
} }
} }
pub async fn bearerauth(
req: ServiceRequest,
credentials: BearerAuth,
) -> Result<ServiceRequest, (Error, ServiceRequest)> {
let ctx: &AppCtx = req.app_data().unwrap();
let creds = credentials.token();
if ctx.settings.dash.api_key == creds {
Ok(req)
} else {
let e = Error::from(ServiceError::Unauthorized);
Err((e, req))
}
}