|
| 1 | +/*! |
| 2 | +Reset email |
| 3 | +*/ |
| 4 | + |
| 5 | +use crate::{endpoints::Endpoint, errors::AtomicResult, urls, Db, Resource}; |
| 6 | + |
| 7 | +pub fn request_email_pubkey_reset() -> Endpoint { |
| 8 | + Endpoint { |
| 9 | + path: urls::PATH_RESET_PUBKEY.to_string(), |
| 10 | + params: [urls::TOKEN.to_string(), urls::INVITE_PUBKEY.to_string()].into(), |
| 11 | + description: "Requests an email to set a new PublicKey to an Agent.".to_string(), |
| 12 | + shortname: "request-pubkey-reset".to_string(), |
| 13 | + handle: Some(construct_reset_pubkey), |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +pub fn confirm_pubkey_reset() -> Endpoint { |
| 18 | + Endpoint { |
| 19 | + path: urls::PATH_CONFIRM_RESET.to_string(), |
| 20 | + params: [urls::TOKEN.to_string(), urls::INVITE_PUBKEY.to_string()].into(), |
| 21 | + description: "Requests an email to set a new PublicKey to an Agent.".to_string(), |
| 22 | + shortname: "request-pubkey-reset".to_string(), |
| 23 | + handle: Some(construct_confirm_reset_pubkey), |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +#[tracing::instrument(skip(store))] |
| 28 | +pub fn construct_confirm_reset_pubkey( |
| 29 | + url: url::Url, |
| 30 | + store: &Db, |
| 31 | + for_agent: Option<&str>, |
| 32 | +) -> AtomicResult<Resource> { |
| 33 | + let mut token_opt: Option<String> = None; |
| 34 | + let mut pubkey_option = None; |
| 35 | + |
| 36 | + println!("url: {:?}", url); |
| 37 | + for (k, v) in url.query_pairs() { |
| 38 | + match k.as_ref() { |
| 39 | + "token" | urls::TOKEN => token_opt = Some(v.to_string()), |
| 40 | + "public-key" | urls::INVITE_PUBKEY => pubkey_option = Some(v.to_string()), |
| 41 | + _ => {} |
| 42 | + } |
| 43 | + } |
| 44 | + let Some(token) = token_opt else { |
| 45 | + return confirm_pubkey_reset().to_resource(store); |
| 46 | + }; |
| 47 | + let pubkey = pubkey_option.ok_or("No public-key provided")?; |
| 48 | + |
| 49 | + // Parse and verify the JWT token |
| 50 | + let confirmation = crate::token::verify_claim::<MailConfirmation>(store, &token)?.custom; |
| 51 | + |
| 52 | + // Add the drive to the Agent's list of drives |
| 53 | + let mut agent = store.get_resource(&drive_creator_agent)?; |
| 54 | + agent.push_propval( |
| 55 | + urls::USED_PUBKEYS.into(), |
| 56 | + SubResource::Subject(drive.get_subject().into()), |
| 57 | + true, |
| 58 | + )?; |
| 59 | + agent.save_locally(store)?; |
| 60 | + |
| 61 | + // Construct the Redirect Resource, which might provide the Client with a Subject for his Agent. |
| 62 | + let mut redirect = Resource::new_instance(urls::REDIRECT, store)?; |
| 63 | + redirect.set_propval_string(urls::DESTINATION.into(), drive.get_subject(), store)?; |
| 64 | + redirect.set_propval( |
| 65 | + urls::REDIRECT_AGENT.into(), |
| 66 | + crate::Value::AtomicUrl(drive_creator_agent), |
| 67 | + store, |
| 68 | + )?; |
| 69 | + Ok(redirect) |
| 70 | +} |
0 commit comments