    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121

    /*
     * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
     *
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the
     * License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU Affero General Public License for more details.
     *
     * 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 std::cell::RefCell;

    use actix_identity::Identity;
    use actix_web::http::header::ContentType;
    use tera::Context;

    use crate::api::v1::RedirectQuery;
    use crate::ctx::api::v1::auth::Login as LoginPayload;
    use crate::pages::errors::*;
    use crate::settings::Settings;
    use crate::AppCtx;

    pub use super::*;

    pub struct Login {
        ctx: RefCell<Context>,
    }

    pub const LOGIN: TemplateFile = TemplateFile::new("login", "pages/auth/login.html");

    impl CtxError for Login {
        fn with_error(&self, e: &ReadableError) -> String {
            self.ctx.borrow_mut().insert(ERROR_KEY, e);
            self.render()
        }
    }

    impl Login {
        pub fn new(settings: &Settings, payload: Option<&LoginPayload>) -> Self {
            let ctx = RefCell::new(context(settings));
            if let Some(payload) = payload {
                ctx.borrow_mut().insert(PAYLOAD_KEY, payload);
            }
            Self { ctx }
        }

        pub fn render(&self) -> String {
            TEMPLATES.render(LOGIN.name, &self.ctx.borrow()).unwrap()
        }

        pub fn page(s: &Settings) -> String {
            let p = Self::new(s, None);
            p.render()
        }
    }

    #[actix_web_codegen_const_routes::get(path = "PAGES.auth.login")]
    #[tracing::instrument(name = "Serve login page", skip(ctx))]
    pub async fn get_login(ctx: AppCtx) -> impl Responder {
        let login = Login::page(&ctx.settings);
        let html = ContentType::html();
        HttpResponse::Ok().content_type(html).body(login)
    }

    pub fn services(cfg: &mut web::ServiceConfig) {
        cfg.service(get_login);
        cfg.service(login_submit);
    }

    #[actix_web_codegen_const_routes::post(path = "PAGES.auth.login")]
    #[tracing::instrument(name = "Web UI Login", skip(id, payload, query, ctx))]
    pub async fn login_submit(
        id: Identity,
        payload: web::Form<LoginPayload>,
        query: web::Query<RedirectQuery>,
        ctx: AppCtx,
    ) -> PageResult<impl Responder, Login> {
        let username = ctx
            .login(&payload)
            .await
            .map_err(|e| PageError::new(Login::new(&ctx.settings, Some(&payload)), e))?;
        id.remember(username);
        let query = query.into_inner();
        if let Some(redirect_to) = query.redirect_to {
            Ok(HttpResponse::Found()
                .insert_header((http::header::LOCATION, redirect_to))
                .finish())
        } else {
            Ok(HttpResponse::Found()
                .insert_header((http::header::LOCATION, PAGES.dash.home))
                .finish())
        }
    }

    #[cfg(test)]
    mod tests {
        use super::Login;
        use super::LoginPayload;
        use crate::errors::*;
        use crate::pages::errors::*;
        use crate::settings::Settings;

        #[test]
        fn register_page_renders() {
            let settings = Settings::new().unwrap();
            Login::page(&settings);
            let payload = LoginPayload {
                login: "foo".into(),
                password: "foo".into(),
            };
            let page = Login::new(&settings, Some(&payload));
            page.with_error(&ReadableError::new(&ServiceError::WrongPassword));
            page.render();
        }
    }