|
| 1 | +/*! |
| 2 | +Sends users a link to add a new public key to their account. |
| 3 | +Useful when a users loses their private key. |
| 4 | +*/ |
| 5 | + |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + agents::Agent, |
| 10 | + email::{EmailAddress, MailAction, MailMessage}, |
| 11 | + endpoints::{Endpoint, HandleGetContext}, |
| 12 | + errors::AtomicResult, |
| 13 | + plugins::utils::return_success, |
| 14 | + urls, Resource, Storelike, |
| 15 | +}; |
| 16 | + |
| 17 | +pub fn request_email_add_pubkey() -> Endpoint { |
| 18 | + Endpoint { |
| 19 | + path: urls::PATH_ADD_PUBKEY.to_string(), |
| 20 | + params: [urls::TOKEN.to_string(), urls::INVITE_PUBKEY.to_string()].into(), |
| 21 | + description: "Requests an email to add a new PublicKey to an Agent.".to_string(), |
| 22 | + shortname: "request-pubkey-reset".to_string(), |
| 23 | + handle: Some(handle_request_email_pubkey), |
| 24 | + handle_post: None, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +pub fn confirm_add_pubkey() -> Endpoint { |
| 29 | + Endpoint { |
| 30 | + path: urls::PATH_CONFIRM_PUBKEY.to_string(), |
| 31 | + params: [urls::TOKEN.to_string(), urls::INVITE_PUBKEY.to_string()].into(), |
| 32 | + description: "Confirms a token to add a new Public Key.".to_string(), |
| 33 | + shortname: "request-pubkey-reset".to_string(), |
| 34 | + handle: Some(handle_confirm_add_pubkey), |
| 35 | + handle_post: None, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Debug, Serialize, Deserialize)] |
| 40 | +struct AddPubkeyToken { |
| 41 | + agent: String, |
| 42 | +} |
| 43 | + |
| 44 | +pub fn handle_request_email_pubkey(context: HandleGetContext) -> AtomicResult<Resource> { |
| 45 | + let HandleGetContext { |
| 46 | + subject, |
| 47 | + store, |
| 48 | + for_agent: _, |
| 49 | + } = context; |
| 50 | + let mut email_option: Option<EmailAddress> = None; |
| 51 | + for (k, v) in subject.query_pairs() { |
| 52 | + if let "email" = k.as_ref() { |
| 53 | + email_option = Some(EmailAddress::new(v.to_string())?) |
| 54 | + } |
| 55 | + } |
| 56 | + // by default just return the Endpoint |
| 57 | + let Some(email) = email_option else { |
| 58 | + return request_email_add_pubkey().to_resource(store); |
| 59 | + }; |
| 60 | + |
| 61 | + // Find the agent by their email |
| 62 | + let agent = match Agent::from_email(&email.to_string(), store) { |
| 63 | + Ok(a) => a, |
| 64 | + // If we can't find the agent, we should still return a `success` response, |
| 65 | + // in order to prevent users to know that the email exists. |
| 66 | + Err(_) => return return_success(), |
| 67 | + }; |
| 68 | + |
| 69 | + // send the user an e-mail to confirm sign up |
| 70 | + let store_clone = store.clone(); |
| 71 | + let confirmation_token_struct = AddPubkeyToken { |
| 72 | + agent: agent.subject, |
| 73 | + }; |
| 74 | + let token = crate::token::sign_claim(store, confirmation_token_struct)?; |
| 75 | + let mut confirm_url = store |
| 76 | + .get_server_url() |
| 77 | + .clone() |
| 78 | + .set_path(urls::PATH_CONFIRM_PUBKEY) |
| 79 | + .url(); |
| 80 | + confirm_url.set_query(Some(&format!("token={}", token))); |
| 81 | + let message = MailMessage { |
| 82 | + to: email, |
| 83 | + subject: "Add a new Passphrase to your account".to_string(), |
| 84 | + body: "You've requested adding a new Passphrase. Click the link below to do so!" |
| 85 | + .to_string(), |
| 86 | + action: Some(MailAction { |
| 87 | + name: "Add new Passphrase to account".to_string(), |
| 88 | + url: confirm_url.into(), |
| 89 | + }), |
| 90 | + }; |
| 91 | + // async, because mails are slow |
| 92 | + tokio::spawn(async move { |
| 93 | + store_clone |
| 94 | + .send_email(message) |
| 95 | + .await |
| 96 | + .unwrap_or_else(|e| tracing::error!("Error sending email: {}", e)); |
| 97 | + }); |
| 98 | + |
| 99 | + return_success() |
| 100 | +} |
| 101 | + |
| 102 | +pub fn handle_confirm_add_pubkey(context: HandleGetContext) -> AtomicResult<Resource> { |
| 103 | + let HandleGetContext { |
| 104 | + subject, |
| 105 | + store, |
| 106 | + for_agent: _, |
| 107 | + } = context; |
| 108 | + |
| 109 | + let mut token_opt: Option<String> = None; |
| 110 | + let mut pubkey_option = None; |
| 111 | + |
| 112 | + for (k, v) in subject.query_pairs() { |
| 113 | + match k.as_ref() { |
| 114 | + "token" | urls::TOKEN => token_opt = Some(v.to_string()), |
| 115 | + "public-key" | urls::INVITE_PUBKEY => pubkey_option = Some(v.to_string()), |
| 116 | + _ => {} |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + let Some(token) = token_opt else { |
| 121 | + return confirm_add_pubkey().to_resource(store); |
| 122 | + }; |
| 123 | + |
| 124 | + let pubkey = pubkey_option.ok_or("No public-key provided")?; |
| 125 | + |
| 126 | + // Parse and verify the JWT token |
| 127 | + let confirmation = crate::token::verify_claim::<AddPubkeyToken>(store, &token)?.custom; |
| 128 | + |
| 129 | + // Add the key to the agent |
| 130 | + let mut agent = store.get_resource(&confirmation.agent)?; |
| 131 | + agent.push_propval(urls::ACTIVE_KEYS, pubkey.into(), true)?; |
| 132 | + agent.save(store)?; |
| 133 | + |
| 134 | + return_success() |
| 135 | +} |
0 commit comments