Setting payload for jwt with http:set_json_body
- Removed visual clutter extracting uid from user params
This commit is contained in:
parent
e21c5c7624
commit
5737f9824d
@ -191,15 +191,19 @@ pub async fn wall_entry<'path, 'pool, 'params>(
|
|||||||
return AuthReason::NoKey;
|
return AuthReason::NoKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn login_get_jwt(p: &Pool, response: &mut hyper::Response<hyper::Body>, params: serde_json::Value) {
|
pub async fn login_get_jwt(p: &Pool, response: &mut hyper::Response<hyper::Body>, params: HashMap<&str, &str>) {
|
||||||
// basically this route generates a jwt for the user and returns via the jwt key
|
// Login data has already been validated at this point
|
||||||
// in the json response
|
// Required data such as 'id' and 'secret' are there and validated
|
||||||
use jsonwebtoken::{
|
use jsonwebtoken::{
|
||||||
Header, Algorithm,
|
Header, Algorithm,
|
||||||
encode
|
encode
|
||||||
};
|
};
|
||||||
use hyper::header::HeaderValue;
|
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 claim = Claim::new(id);
|
||||||
let header = Header::new(Algorithm::HS512);
|
let header = Header::new(Algorithm::HS512);
|
||||||
let encoded = encode(
|
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",
|
response.headers_mut().insert("Content-Type",
|
||||||
HeaderValue::from_static("application/json"));
|
HeaderValue::from_static("application/json"));
|
||||||
|
|
||||||
let payload = serde_json::json!({
|
http::set_json_body(response, serde_json::json!({"jwt": encoded}));
|
||||||
"jwt": encoded
|
|
||||||
});
|
|
||||||
*response.body_mut() = hyper::Body::from(payload.to_string());
|
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}", e);
|
eprintln!("{}", e);
|
||||||
|
Loading…
Reference in New Issue
Block a user