remove client side validation

This commit is contained in:
Vincent Junge 2023-06-25 11:33:50 +02:00
parent ad4c96217c
commit 8b0f41fad3
8 changed files with 52 additions and 354 deletions

View File

@ -26,16 +26,12 @@ impl Instance {
self,
&mut cloned_limits,
)
.await;
if response.is_err() {
return Err(ChorusLibError::NoResponse);
}
.await?;
let response_unwrap = response.unwrap();
let status = response_unwrap.status();
let response_text_string = response_unwrap.text().await.unwrap();
let status = response.status();
let response_text = response.text().await.unwrap();
if status.is_client_error() {
let json: ErrorResponse = serde_json::from_str(&response_text_string).unwrap();
let json: ErrorResponse = serde_json::from_str(&response_text).unwrap();
let error_type = json.errors.errors.iter().next().unwrap().0.to_owned();
let mut error = "".to_string();
for (_, value) in json.errors.errors.iter() {
@ -47,11 +43,8 @@ impl Instance {
}
let cloned_limits = self.limits.clone();
let login_result: LoginResult = from_str(&response_text_string).unwrap();
let object = self
.get_user(login_result.token.clone(), None)
.await
.unwrap();
let login_result: LoginResult = from_str(&response_text).unwrap();
let object = self.get_user(login_result.token.clone(), None).await?;
let user = UserMeta::new(
Rc::new(RefCell::new(self.clone())),
login_result.token,

View File

@ -39,15 +39,11 @@ impl Instance {
self,
&mut cloned_limits,
)
.await;
if response.is_err() {
return Err(ChorusLibError::NoResponse);
}
.await?;
let response_unwrap = response.unwrap();
let status = response_unwrap.status();
let response_unwrap_text = response_unwrap.text().await.unwrap();
let token = from_str::<Token>(&response_unwrap_text).unwrap();
let status = response.status();
let response_text = response.text().await.unwrap();
let token = from_str::<Token>(&response_text).unwrap();
let token = token.token;
if status.is_client_error() {
let json: ErrorResponse = serde_json::from_str(&token).unwrap();
@ -61,9 +57,7 @@ impl Instance {
return Err(ChorusLibError::InvalidFormBodyError { error_type, error });
}
let user_object = self.get_user(token.clone(), None).await.unwrap();
let settings = UserMeta::get_settings(&token, &self.urls.api.clone(), self)
.await
.unwrap();
let settings = UserMeta::get_settings(&token, &self.urls.api.clone(), self).await?;
let user = UserMeta::new(
Rc::new(RefCell::new(self.clone())),
token.clone(),

View File

@ -1879,7 +1879,7 @@ mod example {
#[derive(Debug)]
struct Consumer {
name: String,
_name: String,
events_received: AtomicI32,
}
@ -1900,13 +1900,13 @@ mod example {
};
let consumer = Arc::new(Consumer {
name: "first".into(),
_name: "first".into(),
events_received: 0.into(),
});
event.subscribe(consumer.clone());
let second_consumer = Arc::new(Consumer {
name: "second".into(),
_name: "second".into(),
events_received: 0.into(),
});
event.subscribe(second_consumer.clone());

View File

@ -1,122 +1,8 @@
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::errors::FieldFormatError;
/**
A struct that represents a well-formed email address.
*/
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthEmail {
pub email: String,
}
impl AuthEmail {
/**
Returns a new [`Result<AuthEmail, FieldFormatError>`].
## Arguments
The email address you want to validate.
## Errors
You will receive a [`FieldFormatError`], if:
- The email address is not in a valid format.
*/
pub fn new(email: String) -> Result<AuthEmail, FieldFormatError> {
let regex = Regex::new(r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$").unwrap();
if !regex.is_match(email.as_str()) {
return Err(FieldFormatError::EmailError);
}
Ok(AuthEmail { email })
}
}
/**
A struct that represents a well-formed username.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
*/
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthUsername {
pub username: String,
}
impl AuthUsername {
/**
Returns a new [`Result<AuthUsername, FieldFormatError>`].
## Arguments
The username you want to validate.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
*/
pub fn new(username: String) -> Result<AuthUsername, FieldFormatError> {
if username.len() < 2 || username.len() > 32 {
Err(FieldFormatError::UsernameError)
} else {
Ok(AuthUsername { username })
}
}
}
/**
A struct that represents a well-formed password.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The password is not between 1 and 72 characters.
*/
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct AuthPassword {
pub password: String,
}
impl AuthPassword {
/**
Returns a new [`Result<AuthPassword, FieldFormatError>`].
## Arguments
The password you want to validate.
## Errors
You will receive a [`FieldFormatError`], if:
- The password is not between 1 and 72 characters.
*/
pub fn new(password: String) -> Result<AuthPassword, FieldFormatError> {
if password.is_empty() || password.len() > 72 {
Err(FieldFormatError::PasswordError)
} else {
Ok(AuthPassword { password })
}
}
}
/**
A struct that represents a well-formed register request.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
- The password is not between 1 and 72 characters.
*/
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct RegisterSchema {
username: String,
password: Option<String>,
consent: bool,
email: Option<String>,
fingerprint: Option<String>,
invite: Option<String>,
date_of_birth: Option<String>,
gift_code_sku_id: Option<String>,
captcha_key: Option<String>,
promotional_email_opt_in: Option<bool>,
}
pub struct RegisterSchemaOptions {
pub username: String,
pub password: Option<String>,
pub consent: bool,
@ -129,83 +15,14 @@ pub struct RegisterSchemaOptions {
pub promotional_email_opt_in: Option<bool>,
}
impl RegisterSchema {
pub fn builder(username: impl Into<String>, consent: bool) -> RegisterSchemaOptions {
RegisterSchemaOptions {
username: username.into(),
password: None,
consent,
email: None,
fingerprint: None,
invite: None,
date_of_birth: None,
gift_code_sku_id: None,
captcha_key: None,
promotional_email_opt_in: None,
}
}
}
impl RegisterSchemaOptions {
/**
Create a new [`RegisterSchema`].
## Arguments
All but "String::username" and "bool::consent" are optional.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is less than 2 or more than 32 characters in length
- You supply a `password` which is less than 1 or more than 72 characters in length.
These constraints have been defined [in the Spacebar-API](https://docs.spacebar.chat/routes/)
*/
pub fn build(self) -> Result<RegisterSchema, FieldFormatError> {
let username = AuthUsername::new(self.username)?.username;
let email = if let Some(email) = self.email {
Some(AuthEmail::new(email)?.email)
} else {
None
};
let password = if let Some(password) = self.password {
Some(AuthPassword::new(password)?.password)
} else {
None
};
if !self.consent {
return Err(FieldFormatError::ConsentError);
}
Ok(RegisterSchema {
username,
password,
consent: self.consent,
email,
fingerprint: self.fingerprint,
invite: self.invite,
date_of_birth: self.date_of_birth,
gift_code_sku_id: self.gift_code_sku_id,
captcha_key: self.captcha_key,
promotional_email_opt_in: self.promotional_email_opt_in,
})
}
}
/**
A struct that represents a well-formed login request.
## Arguments
Please use new() to create a new instance of this struct.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is not between 2 and 32 characters.
- The password is not between 1 and 72 characters.
*/
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct LoginSchema {
/// For Discord, usernames must be between 2 and 32 characters,
/// but other servers may have different limits.
pub login: String,
/// For Discord, must be between 1 and 72 characters,
/// but other servers may have different limits.
pub password: Option<String>,
pub undelete: Option<bool>,
pub captcha_key: Option<String>,
@ -213,39 +30,6 @@ pub struct LoginSchema {
pub gift_code_sku_id: Option<String>,
}
impl LoginSchema {
/**
Returns a new [`Result<LoginSchema, FieldFormatError>`].
## Arguments
login: The username you want to login with.
password: The password you want to login with.
undelete: Honestly no idea what this is for.
captcha_key: The captcha key you want to login with.
login_source: The login source.
gift_code_sku_id: The gift code sku id.
## Errors
You will receive a [`FieldFormatError`], if:
- The username is less than 2 or more than 32 characters in length
*/
pub fn new(
login: String,
password: Option<String>,
undelete: Option<bool>,
captcha_key: Option<String>,
login_source: Option<String>,
gift_code_sku_id: Option<String>,
) -> Result<LoginSchema, FieldFormatError> {
Ok(LoginSchema {
login,
password,
undelete,
captcha_key,
login_source,
gift_code_sku_id,
})
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct TotpSchema {

View File

@ -15,76 +15,3 @@ mod message;
mod relationship;
mod role;
mod user;
#[cfg(test)]
mod schemas_tests {
use crate::errors::FieldFormatError;
use super::*;
#[test]
fn password_too_short() {
assert_eq!(
AuthPassword::new("".to_string()),
Err(FieldFormatError::PasswordError)
);
}
#[test]
fn password_too_long() {
let mut long_pw = String::new();
for _ in 0..73 {
long_pw += "a";
}
assert_eq!(
AuthPassword::new(long_pw),
Err(FieldFormatError::PasswordError)
);
}
#[test]
fn username_too_short() {
assert_eq!(
AuthUsername::new("T".to_string()),
Err(FieldFormatError::UsernameError)
);
}
#[test]
fn username_too_long() {
let mut long_un = String::new();
for _ in 0..33 {
long_un += "a";
}
assert_eq!(
AuthUsername::new(long_un),
Err(FieldFormatError::UsernameError)
);
}
#[test]
fn consent_false() {
assert_eq!(
RegisterSchema::builder("Test", false).build(),
Err(FieldFormatError::ConsentError)
);
}
#[test]
fn invalid_email() {
assert_eq!(
AuthEmail::new("p@p.p".to_string()),
Err(FieldFormatError::EmailError)
)
}
#[test]
fn valid_email() {
let reg = RegisterSchemaOptions {
email: Some("me@mail.de".to_string()),
..RegisterSchema::builder("Testy", true)
}
.build();
assert_ne!(reg, Err(FieldFormatError::EmailError));
}
}

View File

@ -1,16 +1,16 @@
use chorus::types::{RegisterSchema, RegisterSchemaOptions};
use chorus::types::RegisterSchema;
mod common;
#[tokio::test]
async fn test_registration() {
let mut bundle = common::setup().await;
let reg = RegisterSchemaOptions {
let reg = RegisterSchema {
username: "Hiiii".into(),
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("Hiiii", true)
}
.build()
.unwrap();
consent: true,
..Default::default()
};
bundle.instance.register_account(&reg).await.unwrap();
common::teardown(bundle).await;
}

View File

@ -2,7 +2,7 @@ use chorus::{
instance::{Instance, UserMeta},
types::{
Channel, ChannelCreateSchema, Guild, GuildCreateSchema, RegisterSchema,
RegisterSchemaOptions, RoleCreateModifySchema, RoleObject,
RoleCreateModifySchema, RoleObject,
},
UrlBundle,
};
@ -26,12 +26,12 @@ pub async fn setup() -> TestBundle {
);
let mut instance = Instance::new(urls.clone()).await.unwrap();
// Requires the existance of the below user.
let reg = RegisterSchemaOptions {
let reg = RegisterSchema {
username: "integrationtestuser".into(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser", true)
}
.build()
.unwrap();
..Default::default()
};
let guild_create_schema = GuildCreateSchema {
name: Some("Test-Guild!".to_string()),
region: None,

View File

@ -1,15 +1,15 @@
use chorus::types::{self, RegisterSchema, RegisterSchemaOptions, Relationship, RelationshipType};
use chorus::types::{self, RegisterSchema, Relationship, RelationshipType};
mod common;
#[tokio::test]
async fn test_get_mutual_relationships() {
let register_schema = RegisterSchemaOptions {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser2", true)
}
.build()
.unwrap();
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
@ -30,12 +30,12 @@ async fn test_get_mutual_relationships() {
#[tokio::test]
async fn test_get_relationships() {
let register_schema = RegisterSchemaOptions {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser2", true)
}
.build()
.unwrap();
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
@ -53,12 +53,12 @@ async fn test_get_relationships() {
#[tokio::test]
async fn test_modify_relationship_friends() {
let register_schema = RegisterSchemaOptions {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser2", true)
}
.build()
.unwrap();
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;
@ -101,12 +101,12 @@ async fn test_modify_relationship_friends() {
#[tokio::test]
async fn test_modify_relationship_block() {
let register_schema = RegisterSchemaOptions {
let register_schema = RegisterSchema {
username: "integrationtestuser2".to_string(),
consent: true,
date_of_birth: Some("2000-01-01".to_string()),
..RegisterSchema::builder("integrationtestuser2", true)
}
.build()
.unwrap();
..Default::default()
};
let mut bundle = common::setup().await;
let belongs_to = &mut bundle.instance;