great victory
This commit is contained in:
parent
c44cd44933
commit
4db44e0b59
@ -76,7 +76,7 @@ pub fn from_token(db: &Db, token: String) -> Result<Account, Error> {
|
|||||||
Ok(Account { id, name, balance, subscribed })
|
Ok(Account { id, name, balance, subscribed })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result<Account, Error> {
|
pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result<Account, MnmlHttpError> {
|
||||||
let query = "
|
let query = "
|
||||||
SELECT id, password, name, balance, subscribed
|
SELECT id, password, name, balance, subscribed
|
||||||
FROM accounts
|
FROM accounts
|
||||||
@ -97,7 +97,7 @@ pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result<A
|
|||||||
|
|
||||||
// verify garbage to prevent timing attacks
|
// verify garbage to prevent timing attacks
|
||||||
verify(garbage.clone(), &garbage).ok();
|
verify(garbage.clone(), &garbage).ok();
|
||||||
return Err(err_msg("account not found"));
|
return Err(MnmlHttpError::AccountNameNotFound);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -108,7 +108,7 @@ pub fn login(tx: &mut Transaction, name: &String, password: &String) -> Result<A
|
|||||||
let subscribed: bool = row.get(4);
|
let subscribed: bool = row.get(4);
|
||||||
|
|
||||||
if !verify(password, &hash)? {
|
if !verify(password, &hash)? {
|
||||||
return Err(err_msg("password does not match"));
|
return Err(MnmlHttpError::PasswordNotMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
let balance = u32::try_from(db_balance)
|
let balance = u32::try_from(db_balance)
|
||||||
|
|||||||
@ -34,9 +34,15 @@ pub enum MnmlHttpError {
|
|||||||
#[fail(display="account name taken or invalid")]
|
#[fail(display="account name taken or invalid")]
|
||||||
AccountNameNotProvided,
|
AccountNameNotProvided,
|
||||||
#[fail(display="account name not provided")]
|
#[fail(display="account name not provided")]
|
||||||
|
AccountNameNotFound,
|
||||||
|
#[fail(display="account name not found")]
|
||||||
AccountNameTaken,
|
AccountNameTaken,
|
||||||
|
#[fail(display="incorrect password")]
|
||||||
|
PasswordNotMatch,
|
||||||
#[fail(display="password unacceptable. must be > 11 characters")]
|
#[fail(display="password unacceptable. must be > 11 characters")]
|
||||||
PasswordUnacceptable,
|
PasswordUnacceptable,
|
||||||
|
#[fail(display="incorrect token. refresh or logout of existing sessions")]
|
||||||
|
TokenDoesNotMatch,
|
||||||
#[fail(display="invalid code. https://discord.gg/YJJgurM")]
|
#[fail(display="invalid code. https://discord.gg/YJJgurM")]
|
||||||
InvalidCode,
|
InvalidCode,
|
||||||
}
|
}
|
||||||
@ -89,14 +95,19 @@ fn iron_response (status: status::Status, message: String) -> Response {
|
|||||||
impl From<MnmlHttpError> for IronError {
|
impl From<MnmlHttpError> for IronError {
|
||||||
fn from(m_err: MnmlHttpError) -> Self {
|
fn from(m_err: MnmlHttpError) -> Self {
|
||||||
let (err, res) = match m_err {
|
let (err, res) = match m_err {
|
||||||
MnmlHttpError::ServerError => (m_err.compat(), status::InternalServerError),
|
MnmlHttpError::ServerError |
|
||||||
MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError),
|
MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError),
|
||||||
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
|
|
||||||
MnmlHttpError::BadRequest => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::AccountNameNotProvided |
|
||||||
MnmlHttpError::AccountNameNotProvided => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::AccountNameTaken |
|
||||||
MnmlHttpError::AccountNameTaken => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::AccountNameNotFound |
|
||||||
|
MnmlHttpError::BadRequest |
|
||||||
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
|
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
|
||||||
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
|
|
||||||
|
MnmlHttpError::PasswordNotMatch |
|
||||||
|
MnmlHttpError::InvalidCode |
|
||||||
|
MnmlHttpError::TokenDoesNotMatch |
|
||||||
|
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
|
||||||
};
|
};
|
||||||
IronError { error: Box::new(err), response: iron_response(res, m_err.to_string()) }
|
IronError { error: Box::new(err), response: iron_response(res, m_err.to_string()) }
|
||||||
}
|
}
|
||||||
@ -119,7 +130,7 @@ impl BeforeMiddleware for AuthMiddleware {
|
|||||||
if cookie.name() == TOKEN_HEADER {
|
if cookie.name() == TOKEN_HEADER {
|
||||||
match account::from_token(&db, cookie.value().to_string()) {
|
match account::from_token(&db, cookie.value().to_string()) {
|
||||||
Ok(a) => req.extensions.insert::<account::Account>(a),
|
Ok(a) => req.extensions.insert::<account::Account>(a),
|
||||||
Err(_) => return Err(IronError::from(MnmlHttpError::Unauthorized)),
|
Err(_) => return Err(IronError::from(MnmlHttpError::TokenDoesNotMatch)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +230,7 @@ fn login(req: &mut Request) -> IronResult<Response> {
|
|||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("{:?}", e);
|
warn!("{:?}", e);
|
||||||
Err(IronError::from(MnmlHttpError::Unauthorized))
|
Err(IronError::from(e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user