|
| 1 | +//! Hosts VSS protocol compliant [`Authorizer`] implementations. |
| 2 | +//! |
| 3 | +//! VSS is an open-source project designed to offer a server-side cloud storage solution specifically |
| 4 | +//! tailored for noncustodial Lightning supporting mobile wallets. Its primary objective is to |
| 5 | +//! simplify the development process for Lightning wallets by providing a secure means to store |
| 6 | +//! and manage the essential state required for Lightning Network (LN) operations. |
| 7 | +//! |
| 8 | +//! [`Authorizer`]: api::auth::Authorizer |
| 9 | +
|
| 10 | +#![deny(rustdoc::broken_intra_doc_links)] |
| 11 | +#![deny(rustdoc::private_intra_doc_links)] |
| 12 | +#![deny(missing_docs)] |
| 13 | + |
| 14 | +use api::auth::{AuthResponse, Authorizer}; |
| 15 | +use api::error::VssError; |
| 16 | +use async_trait::async_trait; |
| 17 | +use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation}; |
| 18 | +use serde::{Deserialize, Serialize}; |
| 19 | +use std::collections::HashMap; |
| 20 | + |
| 21 | +/// A JWT based authorizer, only allows requests with verified 'JsonWebToken' signed by the given |
| 22 | +/// issuer key. |
| 23 | +/// |
| 24 | +/// Refer: https://datatracker.ietf.org/doc/html/rfc7519 |
| 25 | +pub struct JWTAuthorizer { |
| 26 | + jwt_issuer_key: DecodingKey, |
| 27 | +} |
| 28 | + |
| 29 | +/// A set of Claims claimed by 'JsonWebToken' |
| 30 | +/// |
| 31 | +/// Refer: https://datatracker.ietf.org/doc/html/rfc7519#section-4 |
| 32 | +#[derive(Serialize, Deserialize, Debug)] |
| 33 | +pub(crate) struct Claims { |
| 34 | + /// The "sub" (subject) claim identifies the principal that is the subject of the JWT. |
| 35 | + /// The claims in a JWT are statements about the subject. This can be used as user identifier. |
| 36 | + /// |
| 37 | + /// Refer: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 |
| 38 | + sub: String, |
| 39 | +} |
| 40 | + |
| 41 | +const BEARER_PREFIX: &str = "Bearer "; |
| 42 | + |
| 43 | +impl JWTAuthorizer { |
| 44 | + /// Create new instance of [`JWTAuthorizer`] |
| 45 | + pub async fn new(jwt_issuer_key: DecodingKey) -> JWTAuthorizer { |
| 46 | + JWTAuthorizer { jwt_issuer_key } |
| 47 | + } |
| 48 | + |
| 49 | + fn extract_token(auth_header: &str) -> Option<&str> { |
| 50 | + if auth_header.starts_with(BEARER_PREFIX) { |
| 51 | + Some(&auth_header[BEARER_PREFIX.len()..]) |
| 52 | + } else { |
| 53 | + None |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[async_trait] |
| 59 | +impl Authorizer for JWTAuthorizer { |
| 60 | + async fn verify( |
| 61 | + &self, headers_map: &HashMap<String, String>, |
| 62 | + ) -> Result<AuthResponse, VssError> { |
| 63 | + let auth_header = headers_map |
| 64 | + .get("Authorization") |
| 65 | + .ok_or(VssError::AuthError("Authorization header not found.".to_string()))?; |
| 66 | + |
| 67 | + let token = JWTAuthorizer::extract_token(auth_header) |
| 68 | + .ok_or(VssError::AuthError("Invalid token format.".to_string()))?; |
| 69 | + |
| 70 | + let claims = |
| 71 | + decode::<Claims>(token, &self.jwt_issuer_key, &Validation::new(Algorithm::RS256)) |
| 72 | + .map_err(|e| VssError::AuthError(format!("Authentication failure. {}", e)))? |
| 73 | + .claims; |
| 74 | + |
| 75 | + Ok(AuthResponse { user_token: claims.sub }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests { |
| 81 | + use crate::JWTAuthorizer; |
| 82 | + use api::auth::Authorizer; |
| 83 | + use api::error::VssError; |
| 84 | + use jsonwebtoken::{encode, Algorithm, DecodingKey, EncodingKey, Header}; |
| 85 | + use serde::{Deserialize, Serialize}; |
| 86 | + use std::collections::HashMap; |
| 87 | + use std::time::SystemTime; |
| 88 | + |
| 89 | + #[derive(Deserialize, Serialize)] |
| 90 | + struct TestClaims { |
| 91 | + sub: String, |
| 92 | + iat: i64, |
| 93 | + nbf: i64, |
| 94 | + exp: i64, |
| 95 | + } |
| 96 | + |
| 97 | + #[tokio::test] |
| 98 | + async fn test_valid_jwt_token() -> Result<(), VssError> { |
| 99 | + let now = |
| 100 | + SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as i64; |
| 101 | + let user_id = "valid_user_id"; |
| 102 | + let claims = TestClaims { |
| 103 | + sub: user_id.to_owned(), |
| 104 | + iat: now, |
| 105 | + nbf: now, |
| 106 | + exp: now + 30556889864403199, |
| 107 | + }; |
| 108 | + |
| 109 | + let valid_encoding_key = EncodingKey::from_rsa_pem( |
| 110 | + "-----BEGIN PRIVATE KEY-----\ |
| 111 | + MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDKwakpT4j2L1v5\ |
| 112 | + BlIA278TFoDrDiqJB0Vlpd5F6LPj2vWgN8AHAogVb2Ar+Q2eucv0fw/6lh+PuOpQ\ |
| 113 | + n+CWaCoyy8GyFtsPYWHHK1JLSaGxuHpDFSGVqfKY9xJRTIoEPq/tbQIZSFLmW4eW\ |
| 114 | + wIWfjKyUWTilq9wG0ZqnQNNRzzLPSP/GeZJBt2NaCbRrBsc3jy4i1E7dSEsA560b\ |
| 115 | + 4HOVYJHxixNrmmJXwqAmkb+vBhMZe67eVwKadbCOZt4OrXMUWsIMNWRogeQYmBG4\ |
| 116 | + UgM9dofJTDkfYe8qU/3jJJu9MMtdZmPpPLMcQcNuy2qzgOC+6sH9siGL91DvMrcQ\ |
| 117 | + hcvwpEGHAgMBAAECggEAZJZ5Fq6HkyLhrQRusFBUVeLnKDXJ8lsyGYCVafdNL3BU\ |
| 118 | + RR0DXjbqTkAH5SjUkfc48N4MjlPl6oZhcIgwgk3BCZw+RtzB5rp4KLgcRo+L8UBF\ |
| 119 | + H3yfQcGjQjHo235uRjbXTqGy1dokjnXAKZDvebzvbVVqHf7J1HQuFmW5sK9rVJvP\ |
| 120 | + CstC7HqJL15iYTshObnlskB+bnhhBc3LA+UpwyRmvOxPd60XOSxLJ8PMvwki5Qsx\ |
| 121 | + afFCOFpT17474199SxmZtnVpcan7xf9dET8AENTIg8iUAFzLIsl5YekyRAeXj0QW\ |
| 122 | + p9ln6Sl/TsWF+0yJPbeZ1kmvk52MMW7G56SqWt3bAQKBgQDy9mi9hRyfpfBMGrrk\ |
| 123 | + MFDAo1cUvkfuFfBLAfUE9HoEpnQYBqAVFRWCqy6vAa5WdNpVMCDhZkGrn1KDDd/n\ |
| 124 | + ZE/26WBTL95BzXQIO3Laiqmifnio01K2zvjvJt7aGMQOFUEJj8Ts8hUTbRMXfmXz\ |
| 125 | + wbueKeHmcvAUOXbZb5ylC/gkgQKBgQDVovBSib6FnJdv5Clxf1t4gyIbOYWTUPj3\ |
| 126 | + nmkFguBpTLwprzkYjyhyhrGuRaFbcqOVNopgt4KC6enpLtaAMffXwduge+TDKqsS\ |
| 127 | + X1o3OhSzpsya3TrWQMDXKszKTTlNogESOejHxj7LIzts4JmKJcRN4dEVEKhP/CxA\ |
| 128 | + 2b05YnJCBwKBgEiAuc7ceyc1GJlNXLodpOtnkuPwyHxG9bccdWauIf9jQL+usnS4\ |
| 129 | + HvwoYzz8Tm8kXccQHq/EmRJC8BeFu2xMpgQzrngEj9mpGtgeDW8j8+02uoD+1u8Q\ |
| 130 | + on6TZetFerQNKaRVz9k5gIqUgR8ArCHqjTdsninr4LLYVxwZz2/9O2aBAoGBAISQ\ |
| 131 | + ziW5ebL5P3NcFmdqSv1WCeTw5bVLSqKE9tBHrS9KQXxwUbKuqr+eW0UzyfOwCFf/\ |
| 132 | + 9xAa726C7fYXbV0xJIUKs1k7Z/G/WVZWOuoILW5pM49pdigbGE6sLVXfY46L17RS\ |
| 133 | + oOLOXoq4+xgNqtjxpIVbed1jb73qUh+PvX6NWy8jAoGBAOvE6mhHBig5YYdidAGG\ |
| 134 | + kF2oYp06+JG5ZpOu+MFT34ZDbgTwxx3+yuzfxPyBS68RHFfz+vG4BqX3P+pDOJQS\ |
| 135 | + FeGjkLHWEoW7ol5rh1D1ubhWf1MAVOd7O8vp9APnAwd11uraVky2xAVXvplgmSpT\ |
| 136 | + vHSUrqBuEFZ5mIWJxwkGElKN\ |
| 137 | + -----END PRIVATE KEY-----" |
| 138 | + .as_bytes(), |
| 139 | + ) |
| 140 | + .expect("Failed to create Encoding Key."); |
| 141 | + |
| 142 | + let decoding_key = DecodingKey::from_rsa_pem( |
| 143 | + "-----BEGIN PUBLIC KEY-----\ |
| 144 | + MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAysGpKU+I9i9b+QZSANu/\ |
| 145 | + ExaA6w4qiQdFZaXeReiz49r1oDfABwKIFW9gK/kNnrnL9H8P+pYfj7jqUJ/glmgq\ |
| 146 | + MsvBshbbD2FhxytSS0mhsbh6QxUhlanymPcSUUyKBD6v7W0CGUhS5luHlsCFn4ys\ |
| 147 | + lFk4pavcBtGap0DTUc8yz0j/xnmSQbdjWgm0awbHN48uItRO3UhLAOetG+BzlWCR\ |
| 148 | + 8YsTa5piV8KgJpG/rwYTGXuu3lcCmnWwjmbeDq1zFFrCDDVkaIHkGJgRuFIDPXaH\ |
| 149 | + yUw5H2HvKlP94ySbvTDLXWZj6TyzHEHDbstqs4DgvurB/bIhi/dQ7zK3EIXL8KRB\ |
| 150 | + hwIDAQAB\ |
| 151 | + -----END PUBLIC KEY-----" |
| 152 | + .as_bytes(), |
| 153 | + ) |
| 154 | + .expect("Failed to create Decoding Key."); |
| 155 | + |
| 156 | + let jwt_authorizer = JWTAuthorizer::new(decoding_key).await; |
| 157 | + |
| 158 | + let valid_jwt_token = |
| 159 | + encode(&Header::new(Algorithm::RS256), &claims, &valid_encoding_key).unwrap(); |
| 160 | + let mut headers_map: HashMap<String, String> = HashMap::new(); |
| 161 | + let header_value = format!("Bearer {}", valid_jwt_token); |
| 162 | + headers_map.insert("Authorization".to_string(), header_value.clone()); |
| 163 | + println!("headers_map: {:?}", headers_map); |
| 164 | + |
| 165 | + // JWT signed by valid key results in authenticated user. |
| 166 | + assert_eq!(jwt_authorizer.verify(&headers_map).await?.user_token, user_id); |
| 167 | + |
| 168 | + let invalid_encoding_key = EncodingKey::from_rsa_pem( |
| 169 | + "-----BEGIN PRIVATE KEY----- |
| 170 | + MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC77KWE/VUi7QTc\ |
| 171 | + odlj5yRaawPO4z+Ik4c2r2W1BaivIn2dkeTYKT9cQUEcU3sP/i4bQ/DnSuOWAmmG\ |
| 172 | + yaR4NvUvJyGxm6PSBf/kgzDbfvf/8sCi9OEpJEe/xYOhLFaPumtcJAB5mKrdaKsH\ |
| 173 | + XBKJaxJInJsiA6eB67d6SESXG/q1H8f00VLxIAKLK32z5Uahuzc9HQvl4dya+dAI\ |
| 174 | + Xcw0TJg+JoBIqv5ATlcoXKqguiAyQdG2nW5nRnArhvCl9blKjg26cjbhiJcVEZCf\ |
| 175 | + z8vv56IEPhvYEtA8OaiP6vEquqA+vwNipKxqhLzfsjgqYMf18PtrftHjn7nkIvlW\ |
| 176 | + RMnG4+IbAgMBAAECggEAXZf+171UKZDiWwBAxQDZmi6yNtf3TI4tSY8RmJa47IDB\ |
| 177 | + DzkaQI5KgCf/xZvOLqjpTasI0Cj8MDoDVJ4Yy8aTVmim304kyPUz/RtZufgCi/ba\ |
| 178 | + +k371gG7ukckx6DNe8fcsIc9tVHTx3HZvFCe6tHoyUE2AjrPsmUzfDOB9cB5nLrc\ |
| 179 | + JFyKVRUwByeG76AgDJaYMq6cK53+GZih3F9e2exxdnlBuk11R2yJMr638yOfgYbY\ |
| 180 | + 9vzq49OvleLEH1AdAxkcNYuUiPNC7KUeS84MAn+Ok65WvSlyJC3IjVS+swv4p/SB\ |
| 181 | + u0S38ljqisqr0qgfupEJJA/VQaXXo5NJDw48TDuEAQKBgQDuFt7sCoDyqm7XwzWf\ |
| 182 | + f9t9VFnPrLjJbNF7ll2zNlzfArzwk6cDrps2sXoNY0r37ObAdK+awWYRDyoCXJCe\ |
| 183 | + t1wP/leYMp8opn2axQVHSJCq8K2fZO3xRn98p6jy9Hub0l2r9EN6v3JGQmPffl03\ |
| 184 | + qrtYvU8as1ppUXj8Rgw4EGOWRQKBgQDKD7LJ5l/GXotYdOW93y/AXKmEzUjfi1gN\ |
| 185 | + QMxu4TxvK4Q5+CjALYtXb0swbOd7ThcYTU1vgD2Vf5t4z8L/0gSRssGxmMOw8UaS\ |
| 186 | + lay3ONFPRUhffzCMB4wkaomt1km3t9J1LJJ8h8131x2604MrIKmPMIAU6wnikdNN\ |
| 187 | + G5VXx6HM3wKBgQCBzqBdiuCA7WEfa8PJoTj23M1Wh7H7x8NyoSmW8tWxlNmURLwz\ |
| 188 | + KrhfGmYT9IXEJDouxa+ULUtLk7vwq60Bi7C6243AYiEaVaN3hWF6WtrdB/lxROLh\ |
| 189 | + v/Dz8qkPRTI7Y3dEsBk2TDiui7XN/SQvnHsmR5hgU1bAwvW2fS5eRrk1DQKBgQCf\ |
| 190 | + Dq55ukwoNiJQtmxnA3puXULgFEzKE8FzZU/H9KuDA2lpzIwfg3qNkEFK1F9/s+AA\ |
| 191 | + NFHBdNyFg1baSgnBIQyRuHo6l/trnPIlz4aPED3LvckTy2ZmxEYwIGFSoz2STjRw\ |
| 192 | + Im8JcklujbqMZ5V4bJSs78vTK5WzcYE40H7GA5K9VwKBgQCMNL9R7GUGxfQaOxiI\ |
| 193 | + 4mjwus2eQ0fEodIXfU5XFppScHgtKhPWNWNfbrSICyFkfvGBBgQDLCZgt/fO+GAK\ |
| 194 | + r0kIP0GD3KvsLVHsSTR6Fsnz+05HYUEwbc6ebjOegJu+ZO9C4MXnWIaiOzd6vxUz\ |
| 195 | + UIOZiBd7mcNJ6ccxdZ39YIPTew==\ |
| 196 | + -----END PRIVATE KEY-----" |
| 197 | + .as_bytes(), |
| 198 | + ) |
| 199 | + .expect("Failed to create Encoding Key."); |
| 200 | + |
| 201 | + let invalid_jwt_token = |
| 202 | + encode(&Header::new(Algorithm::RS256), &claims, &invalid_encoding_key).unwrap(); |
| 203 | + headers_map.insert("Authorization".to_string(), format!("Bearer {}", invalid_jwt_token)); |
| 204 | + |
| 205 | + // JWT signed by invalid key results in AuthError. |
| 206 | + assert!(matches!( |
| 207 | + jwt_authorizer.verify(&headers_map).await.unwrap_err(), |
| 208 | + VssError::AuthError(_) |
| 209 | + )); |
| 210 | + Ok(()) |
| 211 | + } |
| 212 | +} |
0 commit comments