Setting payload for jwt with http:set_json_body

- Removed visual clutter extracting uid from user params
This commit is contained in:
shockrah 2021-02-03 19:30:12 -08:00
parent e21c5c7624
commit 5737f9824d

View File

@ -191,15 +191,19 @@ pub async fn wall_entry<'path, 'pool, 'params>(
return AuthReason::NoKey;
}
pub async fn login_get_jwt(p: &Pool, response: &mut hyper::Response<hyper::Body>, params: serde_json::Value) {
// basically this route generates a jwt for the user and returns via the jwt key
// in the json response
pub async fn login_get_jwt(p: &Pool, response: &mut hyper::Response<hyper::Body>, params: HashMap<&str, &str>) {
// Login data has already been validated at this point
// Required data such as 'id' and 'secret' are there and validated
use jsonwebtoken::{
Header, Algorithm,
encode
};
use hyper::header::HeaderValue;
let id = params.get("id").unwrap().as_u64().unwrap(); // only route where we have the "id is there guarantee"
use crate::http::{self, extract_uid};
let id = extract_uid(&params); // only route where we have the "id is there guarantee"
let claim = Claim::new(id);
let header = Header::new(Algorithm::HS512);
let encoded = encode(
@ -212,10 +216,7 @@ pub async fn login_get_jwt(p: &Pool, response: &mut hyper::Response<hyper::Body>
response.headers_mut().insert("Content-Type",
HeaderValue::from_static("application/json"));
let payload = serde_json::json!({
"jwt": encoded
});
*response.body_mut() = hyper::Body::from(payload.to_string());
http::set_json_body(response, serde_json::json!({"jwt": encoded}));
},
Err(e) => {
eprintln!("{}", e);