mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2024-11-27 11:59:56 +03:00
update user secret
This commit is contained in:
parent
52ab947e3b
commit
08ec215709
4 changed files with 51 additions and 7 deletions
|
@ -142,6 +142,44 @@ pub async fn get_secret(id: Identity, data: web::Data<Data>) -> ServiceResult<im
|
|||
Ok(HttpResponse::Ok().json(secret))
|
||||
}
|
||||
|
||||
#[post("/api/v1/account/secret/")]
|
||||
pub async fn update_user_secret(
|
||||
id: Identity,
|
||||
data: web::Data<Data>,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
is_authenticated(&id)?;
|
||||
|
||||
let username = id.identity().unwrap();
|
||||
|
||||
let mut secret;
|
||||
|
||||
loop {
|
||||
secret = get_random(32);
|
||||
let res = sqlx::query!(
|
||||
"UPDATE mcaptcha_users set secret = $1
|
||||
WHERE name = $2",
|
||||
&secret,
|
||||
&username,
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await;
|
||||
if res.is_ok() {
|
||||
break;
|
||||
} else {
|
||||
if let Err(sqlx::Error::Database(err)) = res {
|
||||
if err.code() == Some(Cow::from("23505"))
|
||||
&& err.message().contains("mcaptcha_users_secret_key")
|
||||
{
|
||||
continue;
|
||||
} else {
|
||||
Err(sqlx::Error::Database(err))?;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[post("/api/v1/signout")]
|
||||
pub async fn signout(id: Identity) -> impl Responder {
|
||||
if let Some(_) = id.identity() {
|
||||
|
|
|
@ -34,6 +34,7 @@ pub fn services(cfg: &mut ServiceConfig) {
|
|||
cfg.service(auth::username_exists);
|
||||
cfg.service(auth::email_exists);
|
||||
cfg.service(auth::get_secret);
|
||||
cfg.service(auth::update_user_secret);
|
||||
|
||||
// mcaptcha
|
||||
cfg.service(mcaptcha::mcaptcha::add_mcaptcha);
|
||||
|
|
|
@ -44,6 +44,7 @@ async fn auth_works() {
|
|||
let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await;
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
|
||||
// chech if get user secret works
|
||||
let resp = test::call_service(
|
||||
&mut app,
|
||||
test::TestRequest::get()
|
||||
|
@ -54,6 +55,17 @@ async fn auth_works() {
|
|||
.await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
// check if update user secret works
|
||||
let resp = test::call_service(
|
||||
&mut app,
|
||||
test::TestRequest::post()
|
||||
.cookie(cookies.clone())
|
||||
.uri(GET_SECRET)
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
// 2. check if duplicate username is allowed
|
||||
let msg = Register {
|
||||
username: NAME.into(),
|
||||
|
|
|
@ -187,16 +187,11 @@ impl From<sqlx::Error> for ServiceError {
|
|||
fn from(e: sqlx::Error) -> Self {
|
||||
use sqlx::error::Error;
|
||||
use std::borrow::Cow;
|
||||
|
||||
println!("{:?}", &e);
|
||||
if let Error::Database(err) = e {
|
||||
if err.code() == Some(Cow::from("23505")) {
|
||||
return ServiceError::UsernameTaken;
|
||||
}
|
||||
|
||||
println!("{:?}", &err.code());
|
||||
}
|
||||
|
||||
ServiceError::InternalServerError
|
||||
}
|
||||
}
|
||||
|
@ -204,9 +199,7 @@ impl From<sqlx::Error> for ServiceError {
|
|||
pub fn dup_error(e: sqlx::Error, dup_error: ServiceError) -> ServiceError {
|
||||
use sqlx::error::Error;
|
||||
use std::borrow::Cow;
|
||||
// println!("sqlx:Error: {:#?}", &e);
|
||||
if let Error::Database(err) = e {
|
||||
// println!("Database Error: {:#?}", &err);
|
||||
if err.code() == Some(Cow::from("23505")) {
|
||||
dup_error
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue