feat: migrate v1 api mod to use actix_auth_middleware

This commit is contained in:
realaravinth 2022-05-07 12:29:37 +05:30
parent b057e48d72
commit a668fafa62
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
17 changed files with 65 additions and 19 deletions

View file

@ -24,7 +24,7 @@ use crate::AppData;
#[my_codegen::post(
path = "crate::V1_API_ROUTES.account.delete",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn delete_account(
id: Identity,

View file

@ -55,7 +55,7 @@ pub async fn email_exists(
/// update email
#[my_codegen::post(
path = "crate::V1_API_ROUTES.account.update_email",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn set_email(
id: Identity,

View file

@ -70,7 +70,7 @@ async fn update_password_runner(
#[my_codegen::post(
path = "crate::V1_API_ROUTES.account.update_password",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn update_user_password(
id: Identity,

View file

@ -31,7 +31,7 @@ pub struct Secret {
#[my_codegen::get(
path = "crate::V1_API_ROUTES.account.get_secret",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn get_secret(id: Identity, data: AppData) -> ServiceResult<impl Responder> {
let username = id.identity().unwrap();
@ -49,7 +49,7 @@ async fn get_secret(id: Identity, data: AppData) -> ServiceResult<impl Responder
#[my_codegen::post(
path = "crate::V1_API_ROUTES.account.update_secret",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn update_user_secret(
id: Identity,

View file

@ -67,7 +67,7 @@ pub struct Username {
/// update username
#[my_codegen::post(
path = "crate::V1_API_ROUTES.account.update_username",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn set_username(
id: Identity,

View file

@ -25,6 +25,8 @@ use crate::errors::*;
use crate::AppData;
pub mod routes {
use actix_auth_middleware::GetLoginRoute;
pub struct Auth {
pub logout: &'static str,
pub login: &'static str,
@ -43,6 +45,20 @@ pub mod routes {
}
}
}
impl GetLoginRoute for Auth {
fn get_login_route(&self, src: Option<&str>) -> String {
if let Some(redirect_to) = src {
format!(
"{}?redirect_to={}",
self.login,
urlencoding::encode(redirect_to)
)
} else {
self.login.to_string()
}
}
}
}
pub mod runners {
@ -214,14 +230,26 @@ async fn register(
async fn login(
id: Identity,
payload: web::Json<runners::Login>,
path: web::Path<super::RedirectQuery>,
data: AppData,
) -> ServiceResult<impl Responder> {
let username = runners::login_runner(payload.into_inner(), &data).await?;
id.remember(username);
Ok(HttpResponse::Ok())
// Ok(HttpResponse::Ok())
if let Some(redirect_to) = &path.redirect_to {
Ok(HttpResponse::Found()
.insert_header((header::LOCATION, redirect_to))
.finish())
} else {
Ok(HttpResponse::Ok().finish())
}
}
#[my_codegen::get(path = "crate::V1_API_ROUTES.auth.logout", wrap = "crate::CheckLogin")]
#[my_codegen::get(
path = "crate::V1_API_ROUTES.auth.logout",
wrap = "crate::api::v1::get_middleware()"
)]
async fn signout(id: Identity) -> impl Responder {
if id.identity().is_some() {
id.forget();

View file

@ -42,7 +42,7 @@ pub struct MCaptchaDetails {
// so that the whole thing can be added/udpaed in a single stroke
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.create",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn create(
payload: web::Json<CreateCaptcha>,

View file

@ -30,7 +30,7 @@ pub struct DeleteCaptcha {
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.delete",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn delete(
payload: web::Json<DeleteCaptcha>,

View file

@ -96,7 +96,7 @@ impl TrafficPattern {
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.easy.create",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn create(
payload: web::Json<TrafficPattern>,
@ -149,7 +149,7 @@ pub struct UpdateTrafficPattern {
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.easy.update",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
async fn update(
payload: web::Json<UpdateTrafficPattern>,

View file

@ -25,7 +25,7 @@ use crate::AppData;
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.get",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn get_captcha(
payload: web::Json<MCaptchaDetails>,

View file

@ -42,7 +42,7 @@ pub struct StatsPayload {
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.stats.get",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn get(
payload: web::Json<StatsPayload>,

View file

@ -29,7 +29,7 @@ use crate::AppData;
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.update_key",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn update_key(
payload: web::Json<MCaptchaDetails>,
@ -79,7 +79,7 @@ pub struct UpdateCaptcha {
#[my_codegen::post(
path = "crate::V1_API_ROUTES.captcha.update",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn update_captcha(
payload: web::Json<UpdateCaptcha>,

View file

@ -15,7 +15,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use actix_auth_middleware::Authentication;
use actix_web::web::ServiceConfig;
use serde::Deserialize;
pub mod account;
pub mod auth;
@ -36,5 +38,14 @@ pub fn services(cfg: &mut ServiceConfig) {
notifications::services(cfg);
}
#[derive(Deserialize)]
pub struct RedirectQuery {
pub redirect_to: Option<String>,
}
pub fn get_middleware() -> Authentication<routes::Routes> {
Authentication::with_identity(ROUTES)
}
#[cfg(test)]
mod tests;

View file

@ -32,7 +32,7 @@ pub struct AddNotification {
/// route handler that adds a notification message
#[my_codegen::post(
path = "crate::V1_API_ROUTES.notifications.add",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn add_notification(
payload: web::Json<AddNotification>,

View file

@ -55,7 +55,7 @@ impl From<Notification> for NotificationResp {
/// route handler that gets all unread notifications
#[my_codegen::get(
path = "crate::V1_API_ROUTES.notifications.get",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn get_notification(
data: AppData,

View file

@ -39,7 +39,7 @@ pub struct NotificationResp {
/// route handler that marks a notification read
#[my_codegen::post(
path = "crate::V1_API_ROUTES.notifications.mark_read",
wrap = "crate::CheckLogin"
wrap = "crate::api::v1::get_middleware()"
)]
pub async fn mark_read(
data: AppData,

View file

@ -14,6 +14,7 @@
* 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/>.
*/
use actix_auth_middleware::GetLoginRoute;
use super::account::routes::Account;
use super::auth::routes::Auth;
@ -45,3 +46,9 @@ impl Routes {
}
}
}
impl GetLoginRoute for Routes {
fn get_login_route(&self, src: Option<&str>) -> String {
self.auth.get_login_route(src)
}
}