remove token check, add username impl

This commit is contained in:
bitfl0wer 2023-04-17 22:38:21 +02:00
parent f12d5ed8f6
commit d4ea4bd096
1 changed files with 18 additions and 18 deletions

View File

@ -1,5 +1,3 @@
use regex::Regex;
use crate::api::schemas::schemas::InstancePoliciesSchema; use crate::api::schemas::schemas::InstancePoliciesSchema;
use crate::gateway::Gateway; use crate::gateway::Gateway;
use crate::limit::LimitedRequester; use crate::limit::LimitedRequester;
@ -13,12 +11,11 @@ use std::fmt;
The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server. The [`Instance`] what you will be using to perform all sorts of actions on the Spacebar server.
*/ */
pub struct Instance { pub struct Instance {
main_url: String,
urls: URLBundle, urls: URLBundle,
instance_info: InstancePoliciesSchema, instance_info: InstancePoliciesSchema,
requester: LimitedRequester, requester: LimitedRequester,
gateway: Gateway, gateway: Gateway,
users: HashMap<Token, String>, users: HashMap<Token, Username>,
} }
impl Instance { impl Instance {
@ -30,34 +27,37 @@ pub struct Token {
pub token: String, pub token: String,
} }
impl Token { #[derive(Debug, PartialEq, Eq)]
pub fn new(token: String) -> Result<Token, TokenFormatError> { pub struct Username {
let token_regex = Regex::new(r"/[\w-]{24}\.[\w-]{6}\.[\w-]{27}/").unwrap(); pub username: String,
let mfa_token_regex = Regex::new(r"/mfa\.[\w-]{84}/").unwrap(); }
if !token_regex.is_match(&token.as_str()) && !mfa_token_regex.is_match(&token.as_str()) {
return Err(TokenFormatError { impl Username {
message: "This does not seem to be a valid token.".to_string(), pub fn new(username: String) -> Result<Username, UsernameFormatError> {
}); if username.len() < 2 || username.len() > 32 {
return Err(UsernameFormatError::new(
"Username must be between 2 and 32 characters".to_string(),
));
} }
Ok(Token { token }) return Ok(Username { username });
} }
} }
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct TokenFormatError { pub struct UsernameFormatError {
pub message: String, pub message: String,
} }
impl TokenFormatError { impl UsernameFormatError {
fn new(message: String) -> Self { fn new(message: String) -> Self {
TokenFormatError { message } UsernameFormatError { message }
} }
} }
impl fmt::Display for TokenFormatError { impl fmt::Display for UsernameFormatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message) write!(f, "{}", self.message)
} }
} }
impl std::error::Error for TokenFormatError {} impl std::error::Error for UsernameFormatError {}