Initial API setup

Right now we are just listing files in a given subdirectory with not
authentication.
This commit is contained in:
Cameron Cordes
2020-07-07 19:53:12 -04:00
commit 36f7351627
6 changed files with 1907 additions and 0 deletions

70
src/data/mod.rs Normal file
View File

@@ -0,0 +1,70 @@
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 std::str::FromStr;
#[derive(Deserialize)]
pub struct Claims {
pub sub: String,
pub exp: u32,
}
impl FromStr for Claims {
type Err = jsonwebtoken::errors::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
println!("Parsing token: {}", s);
let claims = match decode::<Claims>(
s,
&DecodingKey::from_secret("secret_token".as_ref()),
&Validation::new(Algorithm::HS256),
) {
Ok(data) => Ok(data.claims),
Err(other) => Err(other),
};
return claims;
}
}
impl FromRequest for Claims {
type Error = Error;
type Future = Ready<Result<Self, Self::Error>>;
type Config = ();
fn from_request(req: &HttpRequest, _payload: &mut dev::Payload) -> Self::Future {
let claims = match req.headers().get(header::AUTHORIZATION) {
Some(header) => Claims::from_str(header.to_str().unwrap_or_else(|_| "")),
None => Err(jsonwebtoken::errors::Error::from(
jsonwebtoken::errors::ErrorKind::InvalidToken,
)),
};
if let Ok(claims) = claims {
ok(claims)
} else {
err(ErrorUnauthorized("Bad token"))
}
}
}
#[derive(Deserialize)]
pub struct ThumbnailRequest {
pub path: String,
}
#[derive(Deserialize)]
pub struct LoginRequest {
pub username: String,
pub password: String,
}
#[derive(Deserialize)]
pub struct CreateAccountRequest {
pub username: String,
pub password: String,
pub confirmation: String,
}