@@ -27,26 +27,19 @@ pub struct CreateForm {
2727 password : String ,
2828}
2929
30- pub async fn create_account (
31- pool : & db_auth:: Pool ,
32- create_form : web:: Json < CreateForm > ,
33- ) -> impl Responder {
30+ pub async fn create_account ( pool : & db_auth:: Pool , create_form : web:: Json < CreateForm > ) -> impl Responder {
3431 // check password length is between 8 and 32, inclusive
3532 if create_form. password . len ( ) >= 8 && create_form. password . len ( ) <= 64 {
3633 // check if user is a sketchy motherfucker
3734 let regex = Regex :: new ( r"^[a-z0-9A-Z- ~!@#$%^&*()=+/\_[_]{}|?.,]{3,64}$" ) . unwrap ( ) ;
38- if !regex. is_match ( & create_form. username )
39- || !regex. is_match ( & create_form. password )
40- || !regex. is_match ( & create_form. full_name )
41- {
35+ if !regex. is_match ( & create_form. username ) || !regex. is_match ( & create_form. password ) || !regex. is_match ( & create_form. full_name ) {
4236 return HttpResponse :: BadRequest ( )
4337 . status ( StatusCode :: from_u16 ( 400 ) . unwrap ( ) )
4438 . insert_header ( ( "Cache-Control" , "no-cache" ) )
4539 . body ( "{\" status\" : \" you_sketchy_motherfucker\" }" ) ;
4640 }
4741 // check if username is taken
48- let target_user_temp: Result < db_auth:: User , actix_web:: Error > =
49- db_auth:: get_user_username ( pool, create_form. username . clone ( ) ) . await ;
42+ let target_user_temp: Result < db_auth:: User , actix_web:: Error > = db_auth:: get_user_username ( pool, create_form. username . clone ( ) ) . await ;
5043 if target_user_temp. is_ok ( ) {
5144 return HttpResponse :: BadRequest ( )
5245 . status ( StatusCode :: from_u16 ( 409 ) . unwrap ( ) )
@@ -57,12 +50,7 @@ pub async fn create_account(
5750 // check access key validity
5851 if create_form. access != "00000" {
5952 let access_key_temp: Result < Vec < db_auth:: AccessKey > , actix_web:: Error > =
60- db_auth:: get_access_key (
61- pool,
62- create_form. access . clone ( ) ,
63- db_auth:: AccessKeyQuery :: ById ,
64- )
65- . await ;
53+ db_auth:: get_access_key ( pool, create_form. access . clone ( ) , db_auth:: AccessKeyQuery :: ById ) . await ;
6654 if access_key_temp. is_err ( ) {
6755 return HttpResponse :: BadRequest ( )
6856 . status ( StatusCode :: from_u16 ( 403 ) . unwrap ( ) )
@@ -72,15 +60,14 @@ pub async fn create_account(
7260 // insert into database
7361 let access_key = access_key_temp. unwrap ( ) . first ( ) . cloned ( ) ;
7462 if let Some ( valid_key) = access_key {
75- let user_temp: Result < db_auth:: User , actix_web:: Error > =
76- db_auth:: create_user (
77- pool,
78- valid_key. team ,
79- html_escape:: encode_text ( & create_form. full_name ) . to_string ( ) ,
80- html_escape:: encode_text ( & create_form. username ) . to_string ( ) ,
81- html_escape:: encode_text ( & create_form. password ) . to_string ( ) ,
82- )
83- . await ;
63+ let user_temp: Result < db_auth:: User , actix_web:: Error > = db_auth:: create_user (
64+ pool,
65+ valid_key. team ,
66+ html_escape:: encode_text ( & create_form. full_name ) . to_string ( ) ,
67+ html_escape:: encode_text ( & create_form. username ) . to_string ( ) ,
68+ html_escape:: encode_text ( & create_form. password ) . to_string ( ) ,
69+ )
70+ . await ;
8471 // send final success/failure for creation
8572 if user_temp. is_err ( ) {
8673 return HttpResponse :: BadRequest ( )
@@ -139,8 +126,7 @@ pub async fn login(
139126 login_form : web:: Json < LoginForm > ,
140127) -> impl Responder {
141128 // try to get target user from database
142- let target_user_temp: Result < db_auth:: User , actix_web:: Error > =
143- db_auth:: get_user_username ( pool, login_form. username . clone ( ) ) . await ;
129+ let target_user_temp: Result < db_auth:: User , actix_web:: Error > = db_auth:: get_user_username ( pool, login_form. username . clone ( ) ) . await ;
144130 if target_user_temp. is_err ( ) {
145131 // query error, send failure response
146132 return HttpResponse :: BadRequest ( )
@@ -171,10 +157,11 @@ pub async fn login(
171157 // save the username to the identity
172158 identity. remember ( login_form. username . clone ( ) ) ;
173159 // write the user object to the session
174- session. write ( ) . unwrap ( ) . user_map . insert (
175- target_user. clone ( ) . username . to_string ( ) ,
176- target_user. clone ( ) ,
177- ) ;
160+ session
161+ . write ( )
162+ . unwrap ( )
163+ . user_map
164+ . insert ( target_user. clone ( ) . username . to_string ( ) , target_user. clone ( ) ) ;
178165 // if admin, send admin success response
179166 if target_user. admin == "true" {
180167 // user is admin, send admin response for client cookie
@@ -214,12 +201,8 @@ pub async fn login(
214201 }
215202}
216203
217- pub async fn delete_account (
218- pool : & db_auth:: Pool ,
219- login_form : web:: Json < LoginForm > ,
220- ) -> Result < HttpResponse , actix_web:: Error > {
221- let target_user_temp: Result < db_auth:: User , actix_web:: Error > =
222- db_auth:: get_user_username ( pool, login_form. username . clone ( ) ) . await ;
204+ pub async fn delete_account ( pool : & db_auth:: Pool , login_form : web:: Json < LoginForm > ) -> Result < HttpResponse , actix_web:: Error > {
205+ let target_user_temp: Result < db_auth:: User , actix_web:: Error > = db_auth:: get_user_username ( pool, login_form. username . clone ( ) ) . await ;
223206 if target_user_temp. is_err ( ) {
224207 return Ok ( HttpResponse :: BadRequest ( )
225208 . status ( StatusCode :: from_u16 ( 400 ) . unwrap ( ) )
@@ -240,12 +223,7 @@ pub async fn delete_account(
240223 . is_ok ( )
241224 {
242225 Ok ( HttpResponse :: Ok ( ) . json (
243- db_auth:: execute_manage_user (
244- & pool,
245- db_auth:: UserManageAction :: DeleteUser ,
246- [ target_user. id . to_string ( ) , "" . to_string ( ) ] ,
247- )
248- . await ?,
226+ db_auth:: execute_manage_user ( & pool, db_auth:: UserManageAction :: DeleteUser , [ target_user. id . to_string ( ) , "" . to_string ( ) ] ) . await ?,
249227 ) )
250228 } else {
251229 Ok ( HttpResponse :: BadRequest ( )
@@ -261,10 +239,7 @@ pub async fn delete_account(
261239 }
262240}
263241
264- pub async fn logout (
265- session : web:: Data < RwLock < crate :: Sessions > > ,
266- identity : Identity ,
267- ) -> HttpResponse {
242+ pub async fn logout ( session : web:: Data < RwLock < crate :: Sessions > > , identity : Identity ) -> HttpResponse {
268243 // if session exists, proceed
269244 if let Some ( id) = identity. identity ( ) {
270245 // forget identity
0 commit comments