feat: #11 impl JWT + add associated tests
This commit is contained in:
+16
-5
@@ -11,6 +11,7 @@ pub enum HTTPStatusCode {
|
||||
Http400,
|
||||
Http403,
|
||||
Http404,
|
||||
Http500,
|
||||
}
|
||||
|
||||
impl Into<String> for HTTPStatusCode {
|
||||
@@ -20,6 +21,7 @@ impl Into<String> for HTTPStatusCode {
|
||||
Self::Http400 => "400".to_string(),
|
||||
Self::Http404 => "404".to_string(),
|
||||
Self::Http403 => "403".to_string(),
|
||||
Self::Http500 => "500".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +91,17 @@ impl Into<String> for HTTPResponse {
|
||||
}
|
||||
|
||||
impl HTTPResponse {
|
||||
pub fn as_500() -> Self {
|
||||
let mut response = Self::default();
|
||||
|
||||
response
|
||||
.status_line
|
||||
.set_status_code(HTTPStatusCode::Http500);
|
||||
response.body = json::parse(r#"{"error": "unexpected error occurred"}"#).unwrap();
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
pub fn as_404() -> Self {
|
||||
let mut response = Self::default();
|
||||
|
||||
@@ -117,16 +130,14 @@ impl HTTPResponse {
|
||||
}
|
||||
|
||||
// TODO: need to be adjust to accept `json::JsonValue`
|
||||
pub fn as_200() -> Self {
|
||||
pub fn as_200(token: String) -> Self {
|
||||
let mut response = Self::default();
|
||||
|
||||
response
|
||||
.status_line
|
||||
.set_status_code(HTTPStatusCode::Http200);
|
||||
response.body = json::parse(
|
||||
r#"{"token": "header.payload.signature", "refresh": "header.payload.signature"}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
response.body = json::parse(format!(r#"{{"token": "{}"}}"#, token).as_str()).unwrap();
|
||||
|
||||
response
|
||||
}
|
||||
|
||||
+16
-2
@@ -4,11 +4,15 @@
|
||||
use super::{HTTPRequest, HTTPResponse, HTTPStatusCode};
|
||||
use crate::stores::FileStore;
|
||||
use crate::stores::Store;
|
||||
use jwt_simple::prelude::*;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashMap;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
const JWT_EXPIRATION_TIME: u64 = 2;
|
||||
const ISSUER: &'static str = "thegux.fr";
|
||||
|
||||
type FuturePinned<HTTPResponse> = Pin<Box<dyn Future<Output = HTTPResponse>>>;
|
||||
type Handler = fn(HTTPRequest) -> FuturePinned<HTTPResponse>;
|
||||
|
||||
@@ -22,7 +26,15 @@ fn handle_get(request: HTTPRequest) -> FuturePinned<HTTPResponse> {
|
||||
if !is_auth {
|
||||
return HTTPResponse::as_403();
|
||||
}
|
||||
HTTPResponse::as_200()
|
||||
|
||||
let mut claims = Claims::create(Duration::from_hours(JWT_EXPIRATION_TIME));
|
||||
claims.issuer = Some(ISSUER.to_string());
|
||||
|
||||
match JWT_KEY.authenticate(claims) {
|
||||
Ok(token) => HTTPResponse::as_200(token),
|
||||
// TODO: set the error in the message body
|
||||
Err(_e) => HTTPResponse::as_500(),
|
||||
}
|
||||
}
|
||||
None => HTTPResponse::as_400(),
|
||||
}
|
||||
@@ -36,7 +48,7 @@ fn handle_validate(request: HTTPRequest) -> FuturePinned<HTTPResponse> {
|
||||
match &request.body {
|
||||
Some(ref _b) => {
|
||||
// TODO: impl the JWT validation
|
||||
HTTPResponse::as_200()
|
||||
HTTPResponse::as_200("header.payload.signature".to_string())
|
||||
}
|
||||
None => HTTPResponse::as_400(),
|
||||
}
|
||||
@@ -54,6 +66,8 @@ lazy_static! {
|
||||
("/validate/", handle_validate as Handler)
|
||||
]
|
||||
);
|
||||
|
||||
static ref JWT_KEY: jwt_simple::algorithms::HS256Key = HS256Key::generate();
|
||||
}
|
||||
|
||||
pub struct Router;
|
||||
|
||||
Reference in New Issue
Block a user