1 use activitypub_federation::{config::Data, http_signatures::generate_actor_keypair};
2 use actix_web::web::Json;
3 use lemmy_api_common::{
5 person::{LoginResponse, Register},
8 generate_local_apub_endpoint,
9 generate_shared_inbox_url,
11 local_site_to_slur_regex,
12 password_length_check,
14 send_new_applicant_email_to_admins,
15 send_verification_email,
19 use lemmy_db_schema::{
20 aggregates::structs::PersonAggregates,
22 captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
23 local_user::{LocalUser, LocalUserInsertForm},
24 person::{Person, PersonInsertForm},
25 registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
30 use lemmy_db_views::structs::{LocalUserView, SiteView};
33 error::{LemmyError, LemmyErrorExt, LemmyErrorType},
35 slurs::{check_slurs, check_slurs_opt},
36 validation::is_valid_actor_name,
40 #[tracing::instrument(skip(context))]
41 pub async fn register(
43 context: Data<LemmyContext>,
44 ) -> Result<Json<LoginResponse>, LemmyError> {
45 let site_view = SiteView::read_local(&mut context.pool()).await?;
46 let local_site = site_view.local_site;
47 let require_registration_application =
48 local_site.registration_mode == RegistrationMode::RequireApplication;
50 if local_site.registration_mode == RegistrationMode::Closed {
51 return Err(LemmyErrorType::RegistrationClosed)?;
54 password_length_check(&data.password)?;
55 honeypot_check(&data.honeypot)?;
57 if local_site.require_email_verification && data.email.is_none() {
58 return Err(LemmyErrorType::EmailRequired)?;
61 if local_site.site_setup && require_registration_application && data.answer.is_none() {
62 return Err(LemmyErrorType::RegistrationApplicationAnswerRequired)?;
65 // Make sure passwords match
66 if data.password != data.password_verify {
67 return Err(LemmyErrorType::PasswordsDoNotMatch)?;
70 if local_site.site_setup && local_site.captcha_enabled {
71 if let Some(captcha_uuid) = &data.captcha_uuid {
72 let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
73 let check = CaptchaAnswer::check_captcha(
77 answer: data.captcha_answer.clone().unwrap_or_default(),
82 return Err(LemmyErrorType::CaptchaIncorrect)?;
85 return Err(LemmyErrorType::CaptchaIncorrect)?;
89 let slur_regex = local_site_to_slur_regex(&local_site);
90 check_slurs(&data.username, &slur_regex)?;
91 check_slurs_opt(&data.answer, &slur_regex)?;
92 let username = sanitize_html(&data.username);
94 let actor_keypair = generate_actor_keypair()?;
95 is_valid_actor_name(&data.username, local_site.actor_name_max_length as usize)?;
96 let actor_id = generate_local_apub_endpoint(
99 &context.settings().get_protocol_and_hostname(),
102 if let Some(email) = &data.email {
103 if LocalUser::is_email_taken(&mut context.pool(), email).await? {
104 return Err(LemmyErrorType::EmailAlreadyExists)?;
108 // We have to create both a person, and local_user
110 // Register the new person
111 let person_form = PersonInsertForm::builder()
113 .actor_id(Some(actor_id.clone()))
114 .private_key(Some(actor_keypair.private_key))
115 .public_key(actor_keypair.public_key)
116 .inbox_url(Some(generate_inbox_url(&actor_id)?))
117 .shared_inbox_url(Some(generate_shared_inbox_url(&actor_id)?))
118 // If its the initial site setup, they are an admin
119 .admin(Some(!local_site.site_setup))
120 .instance_id(site_view.site.instance_id)
124 let inserted_person = Person::create(&mut context.pool(), &person_form)
126 .with_lemmy_type(LemmyErrorType::UserAlreadyExists)?;
128 // Automatically set their application as accepted, if they created this with open registration.
129 // Also fixes a bug which allows users to log in when registrations are changed to closed.
130 let accepted_application = Some(!require_registration_application);
132 // Create the local user
133 let local_user_form = LocalUserInsertForm::builder()
134 .person_id(inserted_person.id)
135 .email(data.email.as_deref().map(str::to_lowercase))
136 .password_encrypted(data.password.to_string())
137 .show_nsfw(Some(data.show_nsfw))
138 .accepted_application(accepted_application)
139 .default_listing_type(Some(local_site.default_post_listing_type))
142 let inserted_local_user = LocalUser::create(&mut context.pool(), &local_user_form).await?;
144 if local_site.site_setup && require_registration_application {
145 // Create the registration application
146 let form = RegistrationApplicationInsertForm {
147 local_user_id: inserted_local_user.id,
148 // We already made sure answer was not null above
149 answer: data.answer.clone().expect("must have an answer"),
152 RegistrationApplication::create(&mut context.pool(), &form).await?;
156 if local_site.application_email_admins {
157 send_new_applicant_email_to_admins(&data.username, &mut context.pool(), context.settings())
161 let mut login_response = LoginResponse {
163 registration_created: false,
164 verify_email_sent: false,
167 // Log the user in directly if the site is not setup, or email verification and application aren't required
168 if !local_site.site_setup
169 || (!require_registration_application && !local_site.require_email_verification)
171 login_response.jwt = Some(
173 inserted_local_user.id.0,
174 &context.secret().jwt_secret,
175 &context.settings().hostname,
180 if local_site.require_email_verification {
181 let local_user_view = LocalUserView {
182 local_user: inserted_local_user,
183 person: inserted_person,
184 counts: PersonAggregates::default(),
186 // we check at the beginning of this method that email is set
187 let email = local_user_view
191 .expect("email was provided");
193 send_verification_email(
200 login_response.verify_email_sent = true;
203 if require_registration_application {
204 login_response.registration_created = true;
208 Ok(Json(login_response))