Fix token parsing and require Auth for list files

This commit is contained in:
Cameron Cordes
2020-07-07 22:37:20 -04:00
parent 2aa1b61429
commit e3bb607d95
6 changed files with 148 additions and 24 deletions

View File

@@ -2,31 +2,42 @@ use actix_web::error::ErrorUnauthorized;
use actix_web::{dev, http::header, Error, FromRequest, HttpRequest};
use futures::future::{err, ok, Ready};
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Deserialize)]
#[derive(Serialize)]
pub struct Token<'a> {
pub token: &'a str,
}
#[derive(Deserialize, Serialize)]
pub struct Claims {
pub sub: String,
pub exp: u32,
pub exp: i64,
}
impl FromStr for Claims {
type Err = jsonwebtoken::errors::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
println!("Parsing token: {}", s);
let token = *(s
.split("Bearer ")
.collect::<Vec<_>>()
.last()
.unwrap_or(&""));
println!("Parsing token: '{}'", token);
let claims = match decode::<Claims>(
s,
match decode::<Claims>(
&token,
&DecodingKey::from_secret("secret_token".as_ref()),
&Validation::new(Algorithm::HS256),
) {
Ok(data) => Ok(data.claims),
Err(other) => Err(other),
};
return claims;
Err(other) => {
println!("DecodeError: {}", other);
Err(other)
}
}
}
}