new to rust here. I am trying to build a simple server that provides and decodes JWT tokens and I am missing a big part. Here is the code:
pub struct Server {
pub host: String,
pub port: String,
pub public_key: String,
pub private_key: String
}
impl Server {
pub async fn start(&self) {
let routes = Router::new()
.route("/", get(check))
.route("/auth", post(auth));
let mut hostport = String::from(&self.host);
hostport.push_str(":");
hostport.push_str(&self.port);
println!("{}", hostport);
let addr : SocketAddr = hostport.parse().expect("invalid host:port pair");
axum::Server::bind(
&addr
).serve(routes.into_make_service()).await.unwrap();
}
}
async fn auth(Json(payload): Json<LoginInput>) -> impl IntoResponse {
let claims = Claims::create(Duration::from_hours(1));
RS384PublicKey::from_pem("id_like_to_put_Server::public_key_here").sign(claims)?;
let lo = LoginOutput{
token: payload.username
};
(StatusCode::OK, Json(lo))
}
As you can see Server
holds routing logic and applies configuration. Among configuration there is a public key I'd like to use in order to sign the JWT token (I am using jwt_simple
to achieve that). Since public key is a Server
's attribute, I want to pass that value to the auth
handler but I can't figure out how to do that. How can I pass a parameter to an Axum
handler and sign the token is generated inside?