mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 18:15:39 +00:00
user secret
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
CREATE TABLE IF NOT EXISTS mcaptcha_users (
|
CREATE TABLE IF NOT EXISTS mcaptcha_users (
|
||||||
name VARCHAR(100) NOT NULL UNIQUE,
|
name VARCHAR(100) NOT NULL UNIQUE,
|
||||||
email VARCHAR(100) NOT NULL UNIQUE,
|
email VARCHAR(100) NOT NULL UNIQUE,
|
||||||
|
secret varchar(50) NOT NULL UNIQUE,
|
||||||
password TEXT NOT NULL,
|
password TEXT NOT NULL,
|
||||||
ID SERIAL PRIMARY KEY NOT NULL
|
ID SERIAL PRIMARY KEY NOT NULL
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -14,12 +14,14 @@
|
|||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{post, web, HttpResponse, Responder};
|
use actix_web::{post, web, HttpResponse, Responder};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use super::mcaptcha::get_random;
|
||||||
use crate::errors::*;
|
use crate::errors::*;
|
||||||
use crate::Data;
|
use crate::Data;
|
||||||
|
|
||||||
@@ -49,19 +51,53 @@ pub async fn signup(
|
|||||||
let username = data.creds.username(&payload.username)?;
|
let username = data.creds.username(&payload.username)?;
|
||||||
let hash = data.creds.password(&payload.password)?;
|
let hash = data.creds.password(&payload.password)?;
|
||||||
data.creds.email(Some(&payload.email))?;
|
data.creds.email(Some(&payload.email))?;
|
||||||
let res = sqlx::query!(
|
|
||||||
"INSERT INTO mcaptcha_users (name , password, email) VALUES ($1, $2, $3)",
|
let mut secret;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
secret = get_random(32);
|
||||||
|
let res = add_user_helper(&username, &hash, &payload.email, &secret, &data).await;
|
||||||
|
if res.is_ok() {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
if let Err(sqlx::Error::Database(err)) = res {
|
||||||
|
if err.code() == Some(Cow::from("23505")) {
|
||||||
|
let msg = err.message();
|
||||||
|
if msg.contains("mcaptcha_users_name_key") {
|
||||||
|
Err(ServiceError::UsernameTaken)?;
|
||||||
|
} else if msg.contains("mcaptcha_users_secret_key") {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
Err(ServiceError::InternalServerError)?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::Database(err))?;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(HttpResponse::Ok())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn add_user_helper(
|
||||||
|
username: &str,
|
||||||
|
hash: &str,
|
||||||
|
email: &str,
|
||||||
|
secret: &str,
|
||||||
|
data: &Data,
|
||||||
|
) -> Result<(), sqlx::Error> {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO mcaptcha_users
|
||||||
|
(name , password, email, secret) VALUES ($1, $2, $3, $4)",
|
||||||
username,
|
username,
|
||||||
hash,
|
hash,
|
||||||
&payload.email
|
email,
|
||||||
|
//get_random(32),
|
||||||
|
secret,
|
||||||
)
|
)
|
||||||
.execute(&data.db)
|
.execute(&data.db)
|
||||||
.await;
|
.await?;
|
||||||
|
Ok(())
|
||||||
match res {
|
|
||||||
Err(e) => Err(dup_error(e, ServiceError::UsernameTaken)),
|
|
||||||
Ok(_) => Ok(HttpResponse::Ok()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/api/v1/signin")]
|
#[post("/api/v1/signin")]
|
||||||
|
|||||||
@@ -204,7 +204,9 @@ impl From<sqlx::Error> for ServiceError {
|
|||||||
pub fn dup_error(e: sqlx::Error, dup_error: ServiceError) -> ServiceError {
|
pub fn dup_error(e: sqlx::Error, dup_error: ServiceError) -> ServiceError {
|
||||||
use sqlx::error::Error;
|
use sqlx::error::Error;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
// println!("sqlx:Error: {:#?}", &e);
|
||||||
if let Error::Database(err) = e {
|
if let Error::Database(err) = e {
|
||||||
|
// println!("Database Error: {:#?}", &err);
|
||||||
if err.code() == Some(Cow::from("23505")) {
|
if err.code() == Some(Cow::from("23505")) {
|
||||||
dup_error
|
dup_error
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user