diff --git a/json-api/src/auth.rs b/json-api/src/auth.rs index 6402914..a23e48b 100644 --- a/json-api/src/auth.rs +++ b/json-api/src/auth.rs @@ -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, 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, 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(¶ms); // 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 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);