feat(jwt): #16 add a route to get the public key

This commit is contained in:
landrigun 2022-11-19 14:48:22 +00:00
parent b1cb4dec23
commit 91e80cfbf4
4 changed files with 37 additions and 0 deletions

7
Cargo.lock generated
View File

@ -169,6 +169,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce" checksum = "349a06037c7bf932dd7e7d1f653678b2038b9ad46a74102f1fc7bd7872678cce"
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]] [[package]]
name = "base64ct" name = "base64ct"
version = "1.5.2" version = "1.5.2"
@ -1229,6 +1235,7 @@ version = "0.2.0"
dependencies = [ dependencies = [
"async-std", "async-std",
"async-trait", "async-trait",
"base64",
"clap", "clap",
"configparser", "configparser",
"json", "json",

View File

@ -14,6 +14,7 @@ async-trait = "0.1.57"
jwt-simple = "0.11.1" jwt-simple = "0.11.1"
simple_logger = "4.0.0" simple_logger = "4.0.0"
log = "0.4.17" log = "0.4.17"
base64 = "0.13.1"
# useful for tests (embedded files should be delete in release ?) # useful for tests (embedded files should be delete in release ?)
#rust-embed="6.4.1" #rust-embed="6.4.1"

View File

@ -1,6 +1,7 @@
//! router aims to handle correctly the request corresponding to the target //! router aims to handle correctly the request corresponding to the target
//! it implements all the logic to build an `HTTPResponse` //! it implements all the logic to build an `HTTPResponse`
use base64;
use json; use json;
use super::{HTTPMessage, HTTPRequest, HTTPResponse}; use super::{HTTPMessage, HTTPRequest, HTTPResponse};
@ -11,6 +12,7 @@ use crate::stores::{FileStore, Store};
// TODO: must be mapped with corresponding handler // TODO: must be mapped with corresponding handler
const GET_ROUTE: &'static str = "/get/"; const GET_ROUTE: &'static str = "/get/";
const VALIDATE_ROUTE: &'static str = "/validate/"; const VALIDATE_ROUTE: &'static str = "/validate/";
const PUBKEY_ROUTE: &'static str = "/pubkey/";
async fn handle_get(request: HTTPRequest, config: Config) -> HTTPResponse { async fn handle_get(request: HTTPRequest, config: Config) -> HTTPResponse {
let mut store = FileStore::new(config.filestore_path.clone()); 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)) 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; pub struct Router;
impl Router { impl Router {
@ -99,6 +123,7 @@ impl Router {
match target.as_str() { match target.as_str() {
GET_ROUTE => handle_get(request, config).await, GET_ROUTE => handle_get(request, config).await,
VALIDATE_ROUTE => handle_validate(request, config).await, VALIDATE_ROUTE => handle_validate(request, config).await,
PUBKEY_ROUTE => handle_public_key(request, config).await,
_ => HTTPResponse::as_404(), _ => HTTPResponse::as_404(),
} }
} }

View File

@ -90,6 +90,10 @@ impl JWTSigner {
)), )),
} }
} }
pub fn get_public_key(&self) -> String {
self.public_key.clone()
}
} }
#[tokio::test] #[tokio::test]