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

View File

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