Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 3 additions & 109 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ chrono = { version = "0.4.42", features = [
serde_json = { version = "1.0.145", features = ["preserve_order"] }
base64 = "0.22.1"
uuid = { version = "1.19.0", features = ["serde"] }
captcha = "1.0.0"
anyhow = { version = "1.0.100", features = ["backtrace"] }
diesel_ltree = "0.4.0"
serial_test = "3.2.0"
Expand Down
3 changes: 0 additions & 3 deletions crates/api/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ activitypub_federation = { workspace = true }
tracing = { workspace = true }
bcrypt = { workspace = true }
actix-web = { workspace = true }
base64 = { workspace = true }
captcha = { workspace = true }
anyhow = { workspace = true }
chrono = { workspace = true }
url = { workspace = true }
regex = { workspace = true }
hound = "3.5.1"
sitemap-rs = "0.4.0"
totp-rs = { version = "5.7.0", features = ["gen_secret", "otpauth"] }
diesel-async = { workspace = true, features = ["deadpool", "postgres"] }
Expand Down
43 changes: 0 additions & 43 deletions crates/api/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use base64::{Engine, engine::general_purpose::STANDARD_NO_PAD as base64};
use captcha::Captcha;
use lemmy_api_utils::{context::LemmyContext, utils::is_mod_or_admin_opt};
use lemmy_db_schema::newtypes::CommunityId;
use lemmy_db_views_local_user::LocalUserView;
Expand All @@ -8,7 +6,6 @@ use lemmy_utils::{
utils::slurs::check_slurs,
};
use regex::Regex;
use std::io::Cursor;
use totp_rs::{Secret, TOTP};

pub mod comment;
Expand All @@ -20,46 +17,6 @@ pub mod reports;
pub mod site;
pub mod sitemap;

/// Converts the captcha to a base64 encoded wav audio file
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> LemmyResult<String> {
let letters = captcha.as_wav();

// Decode each wav file, concatenate the samples
let mut concat_samples: Vec<i16> = Vec::new();
let mut any_header: Option<hound::WavSpec> = None;
for letter in letters {
let mut cursor = Cursor::new(letter.unwrap_or_default());
let reader = hound::WavReader::new(&mut cursor)?;
any_header = Some(reader.spec());
let samples16 = reader
.into_samples::<i16>()
.collect::<Result<Vec<_>, _>>()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
concat_samples.extend(samples16);
}

// Encode the concatenated result as a wav file
let mut output_buffer = Cursor::new(vec![]);
if let Some(header) = any_header {
let mut writer = hound::WavWriter::new(&mut output_buffer, header)
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
let mut writer16 = writer.get_i16_writer(concat_samples.len().try_into()?);
for sample in concat_samples {
writer16.write_sample(sample);
}
writer16
.flush()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;
writer
.finalize()
.with_lemmy_type(LemmyErrorType::CouldntCreateAudioCaptcha)?;

Ok(base64.encode(output_buffer.into_inner()))
} else {
Err(LemmyErrorType::CouldntCreateAudioCaptcha)?
}
}

/// Check size of report
pub(crate) fn check_report_reason(reason: &str, slur_regex: &Regex) -> LemmyResult<()> {
check_slurs(reason, slur_regex)?;
Expand Down
49 changes: 10 additions & 39 deletions crates/api/api/src/local_user/get_captcha.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,26 @@
use crate::captcha_as_wav_base64;
use actix_web::{
HttpResponse,
HttpResponseBuilder,
http::{
StatusCode,
header::{CacheControl, CacheDirective},
},
web::{Data, Json},
web::Json,
};
use captcha::{Difficulty, generate};
use lemmy_api_utils::context::LemmyContext;
use lemmy_db_schema::source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm};
use lemmy_db_views_site::{
SiteView,
api::{CaptchaResponse, GetCaptchaResponse},
};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use lemmy_api_utils::plugins::{is_captcha_plugin_loaded, plugin_get_captcha};
use lemmy_db_views_site::api::GetCaptchaResponse;
use lemmy_utils::error::LemmyResult;

pub async fn get_captcha(context: Data<LemmyContext>) -> LemmyResult<HttpResponse> {
let local_site = SiteView::read_local(&mut context.pool()).await?.local_site;
pub async fn get_captcha() -> LemmyResult<HttpResponse> {
let mut res = HttpResponseBuilder::new(StatusCode::OK);
res.insert_header(CacheControl(vec![CacheDirective::NoStore]));

if !local_site.captcha_enabled {
if !is_captcha_plugin_loaded() {
return Ok(res.json(Json(GetCaptchaResponse { ok: None })));
}

let captcha = generate(match local_site.captcha_difficulty.as_str() {
"easy" => Difficulty::Easy,
"hard" => Difficulty::Hard,
_ => Difficulty::Medium,
});

let answer = captcha.chars_as_string();

let png = captcha
.as_base64()
.ok_or(LemmyErrorType::CouldntCreateImageCaptcha)?;

let wav = captcha_as_wav_base64(&captcha)?;

let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer };
// Stores the captcha item in the db
let captcha = CaptchaAnswer::insert(&mut context.pool(), &captcha_form).await?;

let json = Json(GetCaptchaResponse {
ok: Some(CaptchaResponse {
png,
wav,
uuid: captcha.uuid.to_string(),
}),
});
Ok(res.json(json))
let captcha = GetCaptchaResponse {
ok: Some(plugin_get_captcha().await?),
};
Ok(res.json(Json(captcha)))
}
2 changes: 1 addition & 1 deletion crates/api/api_common/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub use lemmy_db_views_post_comment_combined::PostCommentCombinedView;
pub use lemmy_db_views_site::api::{DeleteAccount, MyUserInfo, SaveUserSettings};
pub mod auth {
pub use lemmy_db_schema::source::login_token::LoginToken;
pub use lemmy_db_views_registration_applications::api::Register;
pub use lemmy_db_views_registration_applications::api::{CaptchaAnswer, Register};
pub use lemmy_db_views_site::api::{
CaptchaResponse,
ChangePassword,
Expand Down
Loading