From cd0589fb2e656b08971db01bc866cdcf9bb4b7ed Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 29 Dec 2022 17:29:07 +0530 Subject: [PATCH] feat: replace http auth with bearer auth --- src/api/v1/mod.rs | 11 +++++------ src/api/v1/webhook.rs | 9 +++------ src/settings.rs | 18 +++++++----------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/api/v1/mod.rs b/src/api/v1/mod.rs index 9ce1849..cf478ee 100644 --- a/src/api/v1/mod.rs +++ b/src/api/v1/mod.rs @@ -17,7 +17,7 @@ use actix_web::dev::ServiceRequest; use actix_web::web; use actix_web::Error; -use actix_web_httpauth::extractors::basic::BasicAuth; +use actix_web_httpauth::extractors::bearer::BearerAuth; use crate::errors::*; use crate::AppCtx; @@ -26,14 +26,13 @@ use crate::SETTINGS; pub mod meta; pub mod webhook; -pub async fn httpauth( +pub async fn bearerauth( req: ServiceRequest, - credentials: BasicAuth, + credentials: BearerAuth, ) -> Result { let _ctx: &AppCtx = req.app_data().unwrap(); - let username = credentials.user_id(); - let password = credentials.password().unwrap(); - if SETTINGS.authenticate(username, password) { + let token = credentials.token(); + if SETTINGS.authenticate(token) { Ok(req) } else { let e = Error::from(ServiceError::Unauthorized); diff --git a/src/api/v1/webhook.rs b/src/api/v1/webhook.rs index f1c8ffd..9969b08 100644 --- a/src/api/v1/webhook.rs +++ b/src/api/v1/webhook.rs @@ -24,7 +24,7 @@ use crate::errors::*; use crate::AppCtx; use crate::*; -use super::httpauth; +use super::bearerauth; pub mod routes { use super::*; @@ -47,7 +47,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { #[actix_web_codegen_const_routes::post( path = "API_V1_ROUTES.webhook.post_event", - wrap = "HttpAuthentication::basic(httpauth)" + wrap = "HttpAuthentication::bearer(bearerauth)" )] async fn post_event(ctx: AppCtx, payload: web::Json) -> ServiceResult { ctx.conductor.process(payload.into_inner()).await; @@ -71,10 +71,7 @@ pub mod tests { .await; let creds = settings.creds.clone(); - let auth = format!( - "Basic {}", - base64::encode(format!("{}:{}", creds.username.clone(), creds.password)) - ); + let auth = format!("Bearer {}", creds.token,); let msg = EventType::NewSite { hostname: "demo.librepages.org".into(), diff --git a/src/settings.rs b/src/settings.rs index b3cdd01..7f06ea8 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -54,8 +54,7 @@ pub enum ConductorType { #[derive(Debug, Clone, Deserialize)] pub struct Creds { - pub username: String, - pub password: String, + pub token: String, } #[derive(Debug, Clone, Deserialize)] @@ -69,8 +68,8 @@ pub struct Settings { #[cfg(not(tarpaulin_include))] impl Settings { - pub fn authenticate(&self, username: &str, password: &str) -> bool { - self.creds.username == username && self.creds.password == password + pub fn authenticate(&self, token: &str) -> bool { + self.creds.token == token } pub fn new() -> Result { @@ -148,16 +147,13 @@ mod tests { #[test] fn creds_works() { let settings = Settings::new().unwrap(); - let mut creds = settings.creds.clone(); + let creds = settings.creds.clone(); - assert!(settings.authenticate(&creds.username, &creds.password)); - - creds.username = "noexist".into(); - assert!(!settings.authenticate(&creds.username, &creds.password)); + assert!(settings.authenticate(&creds.token)); let mut creds = settings.creds.clone(); - creds.password = "noexist".into(); - assert!(!settings.authenticate(&creds.username, &creds.password)); + creds.token = "noexist".into(); + assert!(!settings.authenticate(&creds.token)) } }