Read Token signing key from environment

This commit is contained in:
Cameron Cordes
2020-07-30 10:53:27 -04:00
parent 68bb9d9d5c
commit f99d574ba2
2 changed files with 17 additions and 5 deletions

View File

@@ -16,6 +16,10 @@ pub struct Claims {
pub exp: i64, pub exp: i64,
} }
pub fn secret_key() -> String {
dotenv::var("SECRET_KEY").unwrap()
}
impl FromStr for Claims { impl FromStr for Claims {
type Err = jsonwebtoken::errors::Error; type Err = jsonwebtoken::errors::Error;
@@ -24,7 +28,7 @@ impl FromStr for Claims {
match decode::<Claims>( match decode::<Claims>(
&token, &token,
&DecodingKey::from_secret("secret_token".as_ref()), &DecodingKey::from_secret(secret_key().as_bytes()),
&Validation::new(Algorithm::HS256), &Validation::new(Algorithm::HS256),
) { ) {
Ok(data) => Ok(data.claims), Ok(data) => Ok(data.claims),

View File

@@ -12,7 +12,7 @@ use rayon::prelude::*;
use serde::Serialize; use serde::Serialize;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::data::{Claims, CreateAccountRequest, Token}; use crate::data::{secret_key, Claims, CreateAccountRequest, Token};
use crate::database::{create_user, get_user, user_exists}; use crate::database::{create_user, get_user, user_exists};
use crate::files::{is_valid_path, list_files}; use crate::files::{is_valid_path, list_files};
use crate::video::*; use crate::video::*;
@@ -48,7 +48,7 @@ async fn login(creds: Json<LoginRequest>) -> impl Responder {
let token = encode( let token = encode(
&Header::default(), &Header::default(),
&claims, &claims,
&EncodingKey::from_secret("secret_token".as_ref()), &EncodingKey::from_secret(secret_key().as_bytes()),
) )
.unwrap(); .unwrap();
HttpResponse::Ok().json(Token { token: &token }) HttpResponse::Ok().json(Token { token: &token })
@@ -139,7 +139,11 @@ async fn generate_video(_claims: Claims, body: web::Json<ThumbnailRequest>) -> i
} }
#[get("/video/stream")] #[get("/video/stream")]
async fn stream_video(request: HttpRequest, _: Claims, path: web::Query<ThumbnailRequest>) -> impl Responder { async fn stream_video(
request: HttpRequest,
_: Claims,
path: web::Query<ThumbnailRequest>,
) -> impl Responder {
let playlist = &path.path; let playlist = &path.path;
println!("Playlist: {}", playlist); println!("Playlist: {}", playlist);
@@ -151,7 +155,11 @@ async fn stream_video(request: HttpRequest, _: Claims, path: web::Query<Thumbnai
} }
#[get("/video/{path}")] #[get("/video/{path}")]
async fn get_video_part(request: HttpRequest, _: Claims , path: web::Path<ThumbnailRequest>) -> impl Responder { async fn get_video_part(
request: HttpRequest,
_: Claims,
path: web::Path<ThumbnailRequest>,
) -> impl Responder {
let part = &path.path; let part = &path.path;
println!("Video part: {}", part); println!("Video part: {}", part);