use crate::error::Error; use crate::schema::{Db, Transaction}; use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; use serde::{Deserialize, Serialize}; use warp::header::headers_cloned; use warp::http::header::{HeaderMap, HeaderValue, AUTHORIZATION}; use warp::{reject, Filter, Rejection}; use thiserror::Error; use anyhow::*; const BEARER: &str = "Bearer "; const PUBLIC_KEY_PEM: &str = "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4nU0G4WjkmcQUx0hq6LQ uV5Q+ACmUFL/OjoYMDwC/O/6pCd1UZgCfgHN2xEffDPznzcTn8OiFRxr4oWyBiny rUpnY4mhy0SQUwoeCw7YkcHAyhCjNT74aR/ohX0MCj0qRRdbt5ZQXM/GC3HJuXE1 ptSuhFgQxziItamn8maoJ6JUSVEXVO1NOrrjoM3r7Q+BK2B+sX4/bLZ+VG5g1q2n EbFdTHS6pHqtZNHQndTmEKwRfh0RYtzEzOXuO6e1gQY42Tujkof40dhGCIU7TeIG GHwdFxy1niLkXwtHNjV7lnIOkTbx6+sSPamRfQAlZqUWM2Lf5o+7h3qWP3ENB138 sQIDAQAB -----END PUBLIC KEY-----"; // const private_key_pem: &str = "-----BEGIN RSA PRIVATE KEY----- // MIIEpAIBAAKCAQEA4nU0G4WjkmcQUx0hq6LQuV5Q+ACmUFL/OjoYMDwC/O/6pCd1 // UZgCfgHN2xEffDPznzcTn8OiFRxr4oWyBinyrUpnY4mhy0SQUwoeCw7YkcHAyhCj // NT74aR/ohX0MCj0qRRdbt5ZQXM/GC3HJuXE1ptSuhFgQxziItamn8maoJ6JUSVEX // VO1NOrrjoM3r7Q+BK2B+sX4/bLZ+VG5g1q2nEbFdTHS6pHqtZNHQndTmEKwRfh0R // YtzEzOXuO6e1gQY42Tujkof40dhGCIU7TeIGGHwdFxy1niLkXwtHNjV7lnIOkTbx // 6+sSPamRfQAlZqUWM2Lf5o+7h3qWP3ENB138sQIDAQABAoIBAD23nYTmrganag6M //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fTFZ4y5EGOBoKe/dE+IcVF3Vnh6DZVbCAbBL // 5EdFWZSrCnDjA4xiKW55mwp95Ud9EZsZAb13L8V9t82eK+UDBoWlb7VRNYpda/x1 // 5/i4qQJ28x2UNJDStpYFpnp4Ba1lvXjKngIbDPkjU+hbBJ+BNGAIeg== // -----END RSA PRIVATE KEY-----"; /// sub: Subject, user identifier /// exp: Expiration date, Unix Time, epoch /// puk: Public Key? Not 100% on this #[derive(Debug, Serialize, Deserialize)] struct Claims { sub: String, exp: usize, puk: String, } // #[derive(Error, Debug)] // pub enum Nope { // #[error("Invalid header")] // InvalidHeader { // expected: String, // found: String, // }, // } pub fn with_auth( db: Db, t: Transaction, ) -> impl Filter + Clone { headers_cloned() .map(move |headers: HeaderMap| (db.clone(), headers)) .and_then(authorize) } impl warp::reject::Reject for Nope {} async fn authorize((db, headers): (Db, HeaderMap)) -> Result { match jwt_from_header(&headers) { Ok(jwt) => { let decoded = decode::( &jwt, // TODO: what key are we using here? pass db/pw store here to get the claimant's // public key <10-04-21, yigit> // &DecodingKey::from_rsa_pem(PUBLIC_KEY_PEM.as_bytes()).unwrap(), &Validation::new(Algorithm::HS512), ) .map_err(|_| reject::custom(Error::JWTTokenError)) .unwrap(); Ok(decoded.claims.puk) } Err(e) => return Err(anyhow!("missing!")); } } fn jwt_from_header(headers: &HeaderMap) -> Result { let header = match headers.get(AUTHORIZATION) { Some(v) => v, None => return Err(Error::NoAuthHeaderError), }; let auth_header = match std::str::from_utf8(header.as_bytes()) { Ok(v) => v, Err(_) => return Err(Error::NoAuthHeaderError), }; if !auth_header.starts_with(BEARER) { return Err(Error::InvalidAuthHeaderError); } Ok(auth_header.trim_start_matches(BEARER).to_owned()) }