    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
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192

    /*
     * 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 actix_identity::Identity;
    use actix_web::{web, HttpResponse, Responder};
    use argon2_creds::Config;
    use db_core::Login;
    use serde::{Deserialize, Serialize};

    use crate::errors::*;
    use crate::*;

    #[derive(Clone, Debug, Deserialize, Serialize)]
    pub struct ChangePasswordReqest {
        pub password: String,
        pub new_password: String,
        pub confirm_new_password: String,
    }

    pub struct UpdatePassword {
        pub new_password: String,
        pub confirm_new_password: String,
    }

    impl From<ChangePasswordReqest> for UpdatePassword {
        fn from(s: ChangePasswordReqest) -> Self {
            UpdatePassword {
                new_password: s.new_password,
                confirm_new_password: s.confirm_new_password,
            }
        }
    }

    async fn update_password_runner(
        user: &str,
        update: UpdatePassword,
        data: &Data,
    ) -> ServiceResult<()> {
        if update.new_password != update.confirm_new_password {
            return Err(ServiceError::PasswordsDontMatch);
        }

        let new_hash = data.creds.password(&update.new_password)?;

        let p = db_core::NameHash {
            username: user.to_owned(),
            hash: new_hash,
        };

        data.db.update_password(&p).await?;
        Ok(())
    }

    #[my_codegen::post(
        path = "crate::V1_API_ROUTES.account.update_password",
        wrap = "crate::api::v1::get_middleware()"
    )]
    async fn update_user_password(
        id: Identity,
        data: AppData,
        payload: web::Json<ChangePasswordReqest>,
    ) -> ServiceResult<impl Responder> {
        if payload.new_password != payload.confirm_new_password {
            return Err(ServiceError::PasswordsDontMatch);
        }

        let username = id.identity().unwrap();

        // TODO: verify behavior when account is not found
        let res = data.db.get_password(&Login::Username(&username)).await?;

        if Config::verify(&res.hash, &payload.password)? {
            let update: UpdatePassword = payload.into_inner().into();
            update_password_runner(&username, update, &data).await?;
            Ok(HttpResponse::Ok())
        } else {
            Err(ServiceError::WrongPassword)
        }
    }

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

    #[cfg(test)]
    pub mod tests {
        use super::*;

        use actix_web::http::StatusCode;
        use actix_web::test;

        use crate::api::v1::ROUTES;
        use crate::tests::*;

        #[actix_rt::test]
        pub async fn update_password_works() {
            const NAME: &str = "updatepassuser";
            const PASSWORD: &str = "longpassword2";
            const EMAIL: &str = "updatepassuser@a.com";

            let data = get_data().await;
            let data = &data;

            delete_user(data, NAME).await;

            let (_, signin_resp) = register_and_signin(data, NAME, EMAIL, PASSWORD).await;
            let cookies = get_cookie!(signin_resp);
            let app = get_app!(data).await;

            let new_password = "newpassword";

            let update_password = ChangePasswordReqest {
                password: PASSWORD.into(),
                new_password: new_password.into(),
                confirm_new_password: PASSWORD.into(),
            };

            let res = update_password_runner(NAME, update_password.into(), data).await;
            assert!(res.is_err());
            assert_eq!(res, Err(ServiceError::PasswordsDontMatch));

            let update_password = ChangePasswordReqest {
                password: PASSWORD.into(),
                new_password: new_password.into(),
                confirm_new_password: new_password.into(),
            };

            assert!(update_password_runner(NAME, update_password.into(), data)
                .await
                .is_ok());

            let update_password = ChangePasswordReqest {
                password: new_password.into(),
                new_password: new_password.into(),
                confirm_new_password: PASSWORD.into(),
            };

            bad_post_req_test(
                data,
                NAME,
                new_password,
                ROUTES.account.update_password,
                &update_password,
                ServiceError::PasswordsDontMatch,
            )
            .await;

            let update_password = ChangePasswordReqest {
                password: PASSWORD.into(),
                new_password: PASSWORD.into(),
                confirm_new_password: PASSWORD.into(),
            };

            bad_post_req_test(
                data,
                NAME,
                new_password,
                ROUTES.account.update_password,
                &update_password,
                ServiceError::WrongPassword,
            )
            .await;

            let update_password = ChangePasswordReqest {
                password: new_password.into(),
                new_password: PASSWORD.into(),
                confirm_new_password: PASSWORD.into(),
            };

            let update_password_resp = test::call_service(
                &app,
                post_request!(&update_password, ROUTES.account.update_password)
                    .cookie(cookies)
                    .to_request(),
            )
            .await;
            assert_eq!(update_password_resp.status(), StatusCode::OK);
        }
    }