feat(jwt): #16 add a route to get the public key
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
//! router aims to handle correctly the request corresponding to the target
|
||||
//! it implements all the logic to build an `HTTPResponse`
|
||||
|
||||
use base64;
|
||||
use json;
|
||||
|
||||
use super::{HTTPMessage, HTTPRequest, HTTPResponse};
|
||||
@@ -11,6 +12,7 @@ use crate::stores::{FileStore, Store};
|
||||
// TODO: must be mapped with corresponding handler
|
||||
const GET_ROUTE: &'static str = "/get/";
|
||||
const VALIDATE_ROUTE: &'static str = "/validate/";
|
||||
const PUBKEY_ROUTE: &'static str = "/pubkey/";
|
||||
|
||||
async fn handle_get(request: HTTPRequest, config: Config) -> HTTPResponse {
|
||||
let mut store = FileStore::new(config.filestore_path.clone());
|
||||
@@ -87,6 +89,28 @@ async fn handle_validate(request: HTTPRequest, config: Config) -> HTTPResponse {
|
||||
HTTPResponse::as_200(Some(json))
|
||||
}
|
||||
|
||||
/// returns the JWT public key in base64 encoded
|
||||
async fn handle_public_key(_request: HTTPRequest, config: Config) -> HTTPResponse {
|
||||
let jwt_signer = {
|
||||
match JWTSigner::new(config).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
let message = HTTPMessage::error(&e);
|
||||
let json = message.try_into().unwrap();
|
||||
return HTTPResponse::as_500(Some(json));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let public_key = jwt_signer.get_public_key();
|
||||
|
||||
let mut message = HTTPMessage::default();
|
||||
message.put("pubkey", &base64::encode(public_key));
|
||||
|
||||
let json = message.try_into().unwrap();
|
||||
HTTPResponse::as_200(Some(json))
|
||||
}
|
||||
|
||||
pub struct Router;
|
||||
|
||||
impl Router {
|
||||
@@ -99,6 +123,7 @@ impl Router {
|
||||
match target.as_str() {
|
||||
GET_ROUTE => handle_get(request, config).await,
|
||||
VALIDATE_ROUTE => handle_validate(request, config).await,
|
||||
PUBKEY_ROUTE => handle_public_key(request, config).await,
|
||||
_ => HTTPResponse::as_404(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,10 @@ impl JWTSigner {
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_public_key(&self) -> String {
|
||||
self.public_key.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user