|
| 1 | +// This file is Copyright its original authors, visible in version control history. |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 | +// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in |
| 6 | +// accordance with one or both of these licenses. |
| 7 | + |
| 8 | +use crate::logger::{log_debug, log_error, Logger}; |
| 9 | +use crate::types::KeysManager; |
| 10 | +use crate::Error; |
| 11 | + |
| 12 | +use bitcoin::hashes::{hex::FromHex, sha256, Hash, HashEngine, Hmac, HmacEngine}; |
| 13 | +use bitcoin::secp256k1::{Message, Secp256k1, SecretKey}; |
| 14 | +use lightning::util::logger::Logger as LdkLogger; |
| 15 | + |
| 16 | +use bitcoin::bech32; |
| 17 | +use reqwest::Client; |
| 18 | +use serde::{Deserialize, Serialize}; |
| 19 | +use std::sync::Arc; |
| 20 | + |
| 21 | +const LUD13_MESSAGE: &str = "DO NOT EVER SIGN THIS TEXT WITH YOUR PRIVATE KEYS! IT IS ONLY USED FOR DERIVATION OF LNURL-AUTH HASHING-KEY, DISCLOSING ITS SIGNATURE WILL COMPROMISE YOUR LNURL-AUTH IDENTITY AND MAY LEAD TO LOSS OF FUNDS!"; |
| 22 | + |
| 23 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 24 | +struct LnurlAuthResponse { |
| 25 | + status: String, |
| 26 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 27 | + reason: Option<String>, |
| 28 | +} |
| 29 | + |
| 30 | +/// An LNURL-auth handler providing authentication with LNURL-auth compatible services. |
| 31 | +/// |
| 32 | +/// LNURL-auth allows secure, privacy-preserving authentication using domain-specific keys |
| 33 | +/// derived from the node's master key. Each domain gets a unique key, ensuring privacy |
| 34 | +/// while allowing consistent authentication across sessions. |
| 35 | +#[derive(Clone)] |
| 36 | +pub struct LnurlAuth { |
| 37 | + hashing_key: SecretKey, |
| 38 | + client: Client, |
| 39 | + logger: Arc<Logger>, |
| 40 | +} |
| 41 | + |
| 42 | +impl LnurlAuth { |
| 43 | + pub(crate) fn new(hashing_key: SecretKey, logger: Arc<Logger>) -> Self { |
| 44 | + let client = Client::new(); |
| 45 | + Self { hashing_key, client, logger } |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) fn from_keys_manager(keys_manager: &KeysManager, logger: Arc<Logger>) -> Self { |
| 49 | + let hash = sha256::Hash::hash(LUD13_MESSAGE.as_bytes()); |
| 50 | + let sig = keys_manager.sign_message(hash.as_byte_array()); |
| 51 | + let hashed_sig = sha256::Hash::hash(sig.as_bytes()); |
| 52 | + let hashing_key = SecretKey::from_slice(hashed_sig.as_byte_array()) |
| 53 | + .expect("32 bytes, within curve order"); |
| 54 | + Self::new(hashing_key, logger) |
| 55 | + } |
| 56 | + |
| 57 | + /// Authenticates with an LNURL-auth compatible service using the provided URL. |
| 58 | + /// |
| 59 | + /// The authentication process involves: |
| 60 | + /// 1. Fetching the challenge from the service |
| 61 | + /// 2. Deriving a domain-specific linking key |
| 62 | + /// 3. Signing the challenge with the linking key |
| 63 | + /// 4. Submitting the signed response to complete authentication |
| 64 | + /// |
| 65 | + /// Returns `Ok(())` if authentication succeeds, or an error if the process fails. |
| 66 | + pub async fn authenticate(&self, lnurl: &str) -> Result<(), Error> { |
| 67 | + let (hrp, bytes) = bech32::decode(lnurl).map_err(|e| { |
| 68 | + log_error!(self.logger, "Failed to decode LNURL: {e}"); |
| 69 | + Error::InvalidLnurl |
| 70 | + })?; |
| 71 | + |
| 72 | + if hrp.to_lowercase() != "lnurl" { |
| 73 | + log_error!(self.logger, "Invalid LNURL prefix: {hrp}"); |
| 74 | + return Err(Error::InvalidLnurl); |
| 75 | + } |
| 76 | + |
| 77 | + let lnurl_auth_url = String::from_utf8(bytes).map_err(|e| { |
| 78 | + log_error!(self.logger, "Failed to convert LNURL bytes to string: {e}"); |
| 79 | + Error::InvalidLnurl |
| 80 | + })?; |
| 81 | + |
| 82 | + log_debug!(self.logger, "Starting LNURL-auth process for URL: {lnurl_auth_url}"); |
| 83 | + |
| 84 | + // Parse the URL to extract domain and parameters |
| 85 | + let url = reqwest::Url::parse(&lnurl_auth_url).map_err(|e| { |
| 86 | + log_error!(self.logger, "Invalid LNURL-auth URL: {e}"); |
| 87 | + Error::InvalidLnurl |
| 88 | + })?; |
| 89 | + |
| 90 | + let domain = url.host_str().ok_or_else(|| { |
| 91 | + log_error!(self.logger, "No domain found in LNURL-auth URL"); |
| 92 | + Error::InvalidLnurl |
| 93 | + })?; |
| 94 | + |
| 95 | + // get query parameters for k1 and tag |
| 96 | + let query_params: std::collections::HashMap<_, _> = |
| 97 | + url.query_pairs().into_owned().collect(); |
| 98 | + |
| 99 | + let tag = query_params.get("tag").ok_or_else(|| { |
| 100 | + log_error!(self.logger, "No tag parameter found in LNURL-auth URL"); |
| 101 | + Error::InvalidLnurl |
| 102 | + })?; |
| 103 | + |
| 104 | + if tag != "login" { |
| 105 | + log_error!(self.logger, "Invalid tag parameter in LNURL-auth URL: {tag}"); |
| 106 | + return Err(Error::InvalidLnurl); |
| 107 | + } |
| 108 | + |
| 109 | + let k1 = query_params.get("k1").ok_or_else(|| { |
| 110 | + log_error!(self.logger, "No k1 parameter found in LNURL-auth URL"); |
| 111 | + Error::InvalidLnurl |
| 112 | + })?; |
| 113 | + |
| 114 | + let k1_bytes: [u8; 32] = FromHex::from_hex(k1).map_err(|e| { |
| 115 | + log_error!(self.logger, "Invalid k1 hex in challenge: {e}"); |
| 116 | + Error::LnurlAuthFailed |
| 117 | + })?; |
| 118 | + |
| 119 | + // Derive domain-specific linking key |
| 120 | + let linking_secret_key = self.derive_linking_key(domain)?; |
| 121 | + let secp = Secp256k1::signing_only(); |
| 122 | + let linking_public_key = linking_secret_key.public_key(&secp); |
| 123 | + |
| 124 | + // Sign the challenge |
| 125 | + let message = Message::from_digest_slice(&k1_bytes).map_err(|e| { |
| 126 | + log_error!(self.logger, "Failed to create message from k1: {e}"); |
| 127 | + Error::LnurlAuthFailed |
| 128 | + })?; |
| 129 | + |
| 130 | + let signature = secp.sign_ecdsa(&message, &linking_secret_key); |
| 131 | + |
| 132 | + // Submit authentication response |
| 133 | + let auth_url = format!("{lnurl_auth_url}&sig={signature}&key={linking_public_key}"); |
| 134 | + |
| 135 | + log_debug!(self.logger, "Submitting LNURL-auth response"); |
| 136 | + let auth_response = self.client.get(&auth_url).send().await.map_err(|e| { |
| 137 | + log_error!(self.logger, "Failed to submit LNURL-auth response: {e}"); |
| 138 | + Error::LnurlAuthFailed |
| 139 | + })?; |
| 140 | + |
| 141 | + let response: LnurlAuthResponse = auth_response.json().await.map_err(|e| { |
| 142 | + log_error!(self.logger, "Failed to parse LNURL-auth response: {e}"); |
| 143 | + Error::LnurlAuthFailed |
| 144 | + })?; |
| 145 | + |
| 146 | + if response.status == "OK" { |
| 147 | + log_debug!(self.logger, "LNURL-auth authentication successful"); |
| 148 | + Ok(()) |
| 149 | + } else { |
| 150 | + let reason = response.reason.unwrap_or_else(|| "Unknown error".to_string()); |
| 151 | + log_error!(self.logger, "LNURL-auth authentication failed: {reason}"); |
| 152 | + Err(Error::LnurlAuthFailed) |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + fn derive_linking_key(&self, domain: &str) -> Result<SecretKey, Error> { |
| 157 | + // Get the master key from the KeysManager |
| 158 | + // Create HMAC-SHA256 of the domain using node secret as key |
| 159 | + let mut hmac_engine = HmacEngine::<sha256::Hash>::new(&self.hashing_key[..]); |
| 160 | + hmac_engine.input(domain.as_bytes()); |
| 161 | + let hmac_result = Hmac::from_engine(hmac_engine); |
| 162 | + |
| 163 | + // Use HMAC result as the linking private key |
| 164 | + SecretKey::from_slice(hmac_result.as_byte_array()).map_err(|e| { |
| 165 | + log_error!(self.logger, "Failed to derive linking key: {e}"); |
| 166 | + Error::LnurlAuthFailed |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#[cfg(test)] |
| 172 | +mod tests { |
| 173 | + use super::*; |
| 174 | + |
| 175 | + fn build_auth(hashing_key: [u8; 32]) -> LnurlAuth { |
| 176 | + let hashing_key = SecretKey::from_slice(&hashing_key).unwrap(); |
| 177 | + let logger = Arc::new(Logger::new_log_facade()); |
| 178 | + LnurlAuth::new(hashing_key, logger) |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_deterministic_key_derivation() { |
| 183 | + let auth = build_auth([42u8; 32]); |
| 184 | + let domain = "example.com"; |
| 185 | + |
| 186 | + // Keys should be identical for the same inputs |
| 187 | + let key1 = auth.derive_linking_key(domain).unwrap(); |
| 188 | + let key2 = auth.derive_linking_key(domain).unwrap(); |
| 189 | + assert_eq!(key1, key2); |
| 190 | + |
| 191 | + // Keys should be different for different domains |
| 192 | + let key3 = auth.derive_linking_key("different.com").unwrap(); |
| 193 | + assert_ne!(key1, key3); |
| 194 | + |
| 195 | + // Keys should be different for different master keys |
| 196 | + let different_master = build_auth([24u8; 32]); |
| 197 | + let key4 = different_master.derive_linking_key(domain).unwrap(); |
| 198 | + assert_ne!(key1, key4); |
| 199 | + } |
| 200 | + |
| 201 | + #[test] |
| 202 | + fn test_domain_isolation() { |
| 203 | + let auth = build_auth([42u8; 32]); |
| 204 | + let domains = ["example.com", "test.org", "service.net"]; |
| 205 | + let mut keys = Vec::with_capacity(domains.len()); |
| 206 | + |
| 207 | + // Generate keys for different domains |
| 208 | + for domain in &domains { |
| 209 | + keys.push(auth.derive_linking_key(domain).unwrap()); |
| 210 | + } |
| 211 | + |
| 212 | + // All keys should be different (domain isolation) |
| 213 | + for i in 0..keys.len() { |
| 214 | + for j in (i + 1)..keys.len() { |
| 215 | + assert_ne!( |
| 216 | + keys[i], keys[j], |
| 217 | + "Keys for {} and {} should be different", |
| 218 | + domains[i], domains[j] |
| 219 | + ); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments