Add authentication #1

Merged
hannaeko merged 11 commits from feature/auth into master 2021-04-03 17:54:16 +02:00
2 changed files with 14 additions and 6 deletions
Showing only changes of commit 67e12239b3 - Show all commits

View File

@ -71,12 +71,21 @@ impl From<UserError> for ErrorResponse {
impl<S> Into<Outcome<S, ErrorResponse>> for ErrorResponse { impl<S> Into<Outcome<S, ErrorResponse>> for ErrorResponse {
fn into(self) -> Outcome<S, ErrorResponse> { fn into(self) -> Outcome<S, ErrorResponse> {
Outcome::Failure((self.status.clone(), self)) Outcome::Failure(self.into())
} }
} }
impl Into<(Status, ErrorResponse)> for ErrorResponse {
fn into(self) -> (Status, ErrorResponse) {
(self.status.clone(), self)
}
}
pub fn make_500<E: std::fmt::Debug>(e: E) -> ErrorResponse { pub fn make_500<E: std::fmt::Debug>(e: E) -> ErrorResponse {
println!("{:?}", e); println!("Making 500 for Error: {:?}", e);
ErrorResponse::new(Status::InternalServerError, "An unexpected error occured.".into()) ErrorResponse::new(Status::InternalServerError, "An unexpected error occured.".into())
} }
pub fn make_500_tuple<E: std::fmt::Debug>(e: E) -> (Status, ErrorResponse) {
make_500(e).into()
}

View File

@ -20,7 +20,7 @@ use jsonwebtoken::{
use crate::schema::*; use crate::schema::*;
use crate::DbConn; use crate::DbConn;
use crate::config::Config; use crate::config::Config;
use crate::models::errors::ErrorResponse; use crate::models::errors::{ErrorResponse, make_500_tuple};
const BEARER: &'static str = "Bearer "; const BEARER: &'static str = "Bearer ";
@ -109,9 +109,8 @@ impl<'r> FromRequest<'r> for UserInfo {
return ErrorResponse::from(UserError::MalformedHeader).into() return ErrorResponse::from(UserError::MalformedHeader).into()
}; };
// TODO: Better error handling let config = try_outcome!(request.guard::<State<Config>>().await.map_failure(make_500_tuple));
let config = request.guard::<State<Config>>().await.unwrap(); let conn = try_outcome!(request.guard::<DbConn>().await.map_failure(make_500_tuple));
let conn = request.guard::<DbConn>().await.unwrap();
let token_data = AuthClaims::decode( let token_data = AuthClaims::decode(
token, &config.web_app.secret token, &config.web_app.secret