mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-30 21:23:29 +03:00
set email
This commit is contained in:
parent
420ff75817
commit
06815469b7
6 changed files with 90 additions and 28 deletions
|
@ -205,6 +205,47 @@ pub async fn update_user_secret(
|
||||||
Ok(HttpResponse::Ok())
|
Ok(HttpResponse::Ok())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Email {
|
||||||
|
pub email: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/api/v1/account/email/")]
|
||||||
|
pub async fn set_email(
|
||||||
|
id: Identity,
|
||||||
|
|
||||||
|
payload: web::Json<Email>,
|
||||||
|
|
||||||
|
data: web::Data<Data>,
|
||||||
|
) -> ServiceResult<impl Responder> {
|
||||||
|
is_authenticated(&id)?;
|
||||||
|
|
||||||
|
let username = id.identity().unwrap();
|
||||||
|
|
||||||
|
data.creds.email(Some(&payload.email))?;
|
||||||
|
|
||||||
|
let res = sqlx::query!(
|
||||||
|
"UPDATE mcaptcha_users set email = $1
|
||||||
|
WHERE name = $2",
|
||||||
|
&payload.email,
|
||||||
|
&username,
|
||||||
|
)
|
||||||
|
.execute(&data.db)
|
||||||
|
.await;
|
||||||
|
if !res.is_ok() {
|
||||||
|
if let Err(sqlx::Error::Database(err)) = res {
|
||||||
|
if err.code() == Some(Cow::from("23505"))
|
||||||
|
&& err.message().contains("mcaptcha_users_email_key")
|
||||||
|
{
|
||||||
|
Err(ServiceError::EmailTaken)?
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::Database(err))?
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Ok(HttpResponse::Ok())
|
||||||
|
}
|
||||||
|
|
||||||
#[post("/api/v1/signout")]
|
#[post("/api/v1/signout")]
|
||||||
pub async fn signout(id: Identity) -> impl Responder {
|
pub async fn signout(id: Identity) -> impl Responder {
|
||||||
if let Some(_) = id.identity() {
|
if let Some(_) = id.identity() {
|
||||||
|
|
|
@ -36,6 +36,7 @@ pub fn services(cfg: &mut ServiceConfig) {
|
||||||
cfg.service(auth::email_exists);
|
cfg.service(auth::email_exists);
|
||||||
cfg.service(auth::get_secret);
|
cfg.service(auth::get_secret);
|
||||||
cfg.service(auth::update_user_secret);
|
cfg.service(auth::update_user_secret);
|
||||||
|
cfg.service(auth::set_email);
|
||||||
|
|
||||||
// mcaptcha
|
// mcaptcha
|
||||||
cfg.service(mcaptcha::mcaptcha::add_mcaptcha);
|
cfg.service(mcaptcha::mcaptcha::add_mcaptcha);
|
||||||
|
|
|
@ -134,10 +134,12 @@ async fn auth_works() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn del_userworks() {
|
async fn email_udpate_and_del_userworks() {
|
||||||
const NAME: &str = "testuser2";
|
const NAME: &str = "testuser2";
|
||||||
const PASSWORD: &str = "longpassword2";
|
const PASSWORD: &str = "longpassword2";
|
||||||
const EMAIL: &str = "testuser1@a.com2";
|
const EMAIL: &str = "testuser1@a.com2";
|
||||||
|
const DEL_URL: &str = "/api/v1/account/delete";
|
||||||
|
const EMAIL_UPDATE: &str = "/api/v1/account/email/";
|
||||||
|
|
||||||
{
|
{
|
||||||
let data = Data::new().await;
|
let data = Data::new().await;
|
||||||
|
@ -148,13 +150,26 @@ async fn del_userworks() {
|
||||||
let cookies = get_cookie!(signin_resp);
|
let cookies = get_cookie!(signin_resp);
|
||||||
let mut app = get_app!(data).await;
|
let mut app = get_app!(data).await;
|
||||||
|
|
||||||
|
let email_payload = Email {
|
||||||
|
email: EMAIL.into(),
|
||||||
|
};
|
||||||
|
let email_update_resp = test::call_service(
|
||||||
|
&mut app,
|
||||||
|
post_request!(&email_payload, EMAIL_UPDATE)
|
||||||
|
.cookie(cookies.clone())
|
||||||
|
.to_request(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert_eq!(email_update_resp.status(), StatusCode::OK);
|
||||||
|
|
||||||
let payload = Password {
|
let payload = Password {
|
||||||
password: creds.password,
|
password: creds.password,
|
||||||
};
|
};
|
||||||
|
|
||||||
let delete_user_resp = test::call_service(
|
let delete_user_resp = test::call_service(
|
||||||
&mut app,
|
&mut app,
|
||||||
post_request!(&payload, "/api/v1/account/delete")
|
post_request!(&payload, DEL_URL)
|
||||||
.cookie(cookies)
|
.cookie(cookies)
|
||||||
.to_request(),
|
.to_request(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -77,6 +77,11 @@ pub enum ServiceError {
|
||||||
/// when the a username is already taken
|
/// when the a username is already taken
|
||||||
#[display(fmt = "Username not available")]
|
#[display(fmt = "Username not available")]
|
||||||
UsernameTaken,
|
UsernameTaken,
|
||||||
|
|
||||||
|
/// email is already taken
|
||||||
|
#[display(fmt = "Email not available")]
|
||||||
|
EmailTaken,
|
||||||
|
|
||||||
/// when the a token name is already taken
|
/// when the a token name is already taken
|
||||||
/// token not found
|
/// token not found
|
||||||
#[display(fmt = "Token not found. Is token registered?")]
|
#[display(fmt = "Token not found. Is token registered?")]
|
||||||
|
@ -122,6 +127,7 @@ impl ResponseError for ServiceError {
|
||||||
ServiceError::PasswordTooLong => StatusCode::BAD_REQUEST,
|
ServiceError::PasswordTooLong => StatusCode::BAD_REQUEST,
|
||||||
|
|
||||||
ServiceError::UsernameTaken => StatusCode::BAD_REQUEST,
|
ServiceError::UsernameTaken => StatusCode::BAD_REQUEST,
|
||||||
|
ServiceError::EmailTaken => StatusCode::BAD_REQUEST,
|
||||||
|
|
||||||
ServiceError::TokenNotFound => StatusCode::NOT_FOUND,
|
ServiceError::TokenNotFound => StatusCode::NOT_FOUND,
|
||||||
ServiceError::CaptchaError(e) => match e {
|
ServiceError::CaptchaError(e) => match e {
|
||||||
|
|
|
@ -16,11 +16,10 @@
|
||||||
*/
|
*/
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use actix_cors::Cors;
|
|
||||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
client::Client, error::InternalError, http::StatusCode, middleware, web::scope,
|
client::Client, error::InternalError, http::StatusCode, middleware, web::JsonConfig, App,
|
||||||
web::JsonConfig, App, HttpServer,
|
HttpServer,
|
||||||
};
|
};
|
||||||
//use awc::Client;
|
//use awc::Client;
|
||||||
use cache_buster::Files as FileMap;
|
use cache_buster::Files as FileMap;
|
||||||
|
|
|
@ -44,14 +44,14 @@ impl Server {
|
||||||
format!("{}:{}", self.ip, self.port)
|
format!("{}:{}", self.ip, self.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_url_prefix(&mut self) {
|
//fn check_url_prefix(&mut self) {
|
||||||
if let Some(prefix) = self.url_prefix.clone() {
|
// if let Some(prefix) = self.url_prefix.clone() {
|
||||||
self.url_prefix = Some(prefix.trim().into());
|
// self.url_prefix = Some(prefix.trim().into());
|
||||||
if prefix.trim().is_empty() {
|
// if prefix.trim().is_empty() {
|
||||||
self.url_prefix = None;
|
// self.url_prefix = None;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
@ -173,18 +173,18 @@ fn set_database_url(s: &mut Config) {
|
||||||
.expect("Couldn't set databse url");
|
.expect("Couldn't set databse url");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
//#[cfg(test)]
|
||||||
mod tests {
|
//mod tests {
|
||||||
use super::*;
|
// use super::*;
|
||||||
|
//
|
||||||
#[test]
|
// #[test]
|
||||||
fn url_prefix_test() {
|
// fn url_prefix_test() {
|
||||||
let mut settings = Settings::new().unwrap();
|
// let mut settings = Settings::new().unwrap();
|
||||||
assert!(settings.server.url_prefix.is_none());
|
// assert!(settings.server.url_prefix.is_none());
|
||||||
settings.server.url_prefix = Some("test".into());
|
// settings.server.url_prefix = Some("test".into());
|
||||||
settings.server.check_url_prefix();
|
// settings.server.check_url_prefix();
|
||||||
settings.server.url_prefix = Some(" ".into());
|
// settings.server.url_prefix = Some(" ".into());
|
||||||
settings.server.check_url_prefix();
|
// settings.server.check_url_prefix();
|
||||||
assert!(settings.server.url_prefix.is_none());
|
// assert!(settings.server.url_prefix.is_none());
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
Loading…
Add table
Reference in a new issue