Skip to content

Commit 79782dd

Browse files
committed
Public key reset endpoint WIP
1 parent e82201f commit 79782dd

20 files changed

+271
-169
lines changed

lib/src/agents.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use base64::{engine::general_purpose, Engine};
66
use serde_json::from_slice;
77

8-
use crate::{errors::AtomicResult, urls, Resource, Storelike, Value};
8+
use crate::{errors::AtomicResult, urls, Query, Resource, Storelike, Value};
99

1010
/// None represents no right checks will be performed, effectively SUDO mode.
1111
#[derive(Clone, Debug, PartialEq)]
@@ -81,6 +81,41 @@ impl Agent {
8181
Ok(resource)
8282
}
8383

84+
pub fn from_email(email: &str, store: &impl Storelike) -> AtomicResult<Self> {
85+
let mut query = Query::new();
86+
query.property = Some(urls::EMAIL.into());
87+
query.value = Some(Value::String(email.to_string()));
88+
let response = store.query(&query)?;
89+
if response.resources.is_empty() {
90+
return Err(format!("Agent with Email {} not found", email).into());
91+
}
92+
if response.resources.len() > 1 {
93+
return Err(format!(
94+
"Email {} is not unique, {} agents have this email",
95+
email, response.count
96+
)
97+
.into());
98+
}
99+
let resource = response.resources.first().unwrap();
100+
Agent::from_resource(resource.clone())
101+
}
102+
103+
pub fn from_resource(resource: Resource) -> AtomicResult<Self> {
104+
let name = if let Ok(name) = resource.get(urls::NAME) {
105+
Some(name.to_string())
106+
} else {
107+
None
108+
};
109+
110+
return Ok(Self {
111+
created_at: resource.get(urls::CREATED_AT)?.to_int()?,
112+
name,
113+
public_key: resource.get(urls::PUBLIC_KEY)?.to_string(),
114+
private_key: None,
115+
subject: resource.get_subject().into(),
116+
});
117+
}
118+
84119
/// Creates a new Agent, generates a new Keypair.
85120
pub fn new(name: Option<&str>, store: &impl Storelike) -> AtomicResult<Agent> {
86121
let keypair = generate_keypair()?;

lib/src/collections.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
//! Collections are dynamic resources that refer to multiple resources.
22
//! They are constructed using a [Query]
33
use crate::{
4-
agents::ForAgent,
5-
errors::AtomicResult,
6-
storelike::{Query, ResourceCollection},
7-
urls, Resource, Storelike, Value,
4+
agents::ForAgent, errors::AtomicResult, storelike::ResourceCollection, urls, Query, Resource,
5+
Storelike, Value,
86
};
97

108
const DEFAULT_PAGE_SIZE: usize = 30;

lib/src/db.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ use crate::{
2727
email::{self, MailMessage},
2828
endpoints::{default_endpoints, Endpoint, HandleGetContext},
2929
errors::{AtomicError, AtomicResult},
30+
query::QueryResult,
3031
resources::PropVals,
31-
storelike::{Query, QueryResult, Storelike},
32+
storelike::Storelike,
3233
values::SortableValue,
33-
Atom, Resource,
34+
Atom, Query, Resource,
3435
};
3536

3637
use self::{
@@ -254,10 +255,6 @@ impl Db {
254255
}
255256

256257
fn query_basic(&self, q: &Query) -> AtomicResult<QueryResult> {
257-
let self_url = self
258-
.get_self_url()
259-
.ok_or("No self_url set, required for Queries")?;
260-
261258
let mut subjects: Vec<String> = vec![];
262259
let mut resources: Vec<Resource> = vec![];
263260
let mut total_count = 0;

lib/src/db/query_index.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! It relies on lexicographic ordering of keys, which Sled utilizes using `scan_prefix` queries.
33
44
use crate::{
5-
agents::ForAgent, atoms::IndexAtom, errors::AtomicResult, storelike::Query,
6-
values::SortableValue, Atom, Db, Resource, Storelike, Value,
5+
agents::ForAgent, atoms::IndexAtom, errors::AtomicResult, values::SortableValue, Atom, Db,
6+
Query, Resource, Storelike, Value,
77
};
88
use serde::{Deserialize, Serialize};
99

@@ -99,10 +99,6 @@ pub fn query_sorted_indexed(
9999
let mut resources: Vec<Resource> = vec![];
100100
let mut count = 0;
101101

102-
let self_url = store
103-
.get_self_url()
104-
.ok_or("No self_url set, required for Queries")?;
105-
106102
let limit = q.limit.unwrap_or(std::usize::MAX);
107103

108104
for (i, kv) in iter.enumerate() {

lib/src/email.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! [EmailAddress] with validation, [MailMessage] with sending, and [get_smtp_client] for setting up mail.
22
3-
use crate::{errors::AtomicResult, storelike::Query, urls, Storelike};
3+
use crate::{errors::AtomicResult, urls, Query, Storelike};
44
use mail_send::{mail_builder::MessageBuilder, Connected, Transport};
55
use serde::{Deserialize, Serialize};
66
use tracing::info;

lib/src/hierarchy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use core::fmt;
66

7-
use crate::{agents::ForAgent, errors::AtomicResult, storelike::Query, urls, Resource, Storelike};
7+
use crate::{agents::ForAgent, errors::AtomicResult, urls, Query, Resource, Storelike};
88

99
#[derive(Debug)]
1010
pub enum Right {

lib/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ See the [Atomic Data Docs](https://docs.atomicdata.dev) for more information.
1616
- [Value] converts Atomic Data to Rust native types
1717
- [Commit]s (transactions / delta's / changes / updates / versioning / history). Supports many checks, such as Schema, Authorization and more.
1818
- [collections] (pagination, sorting, filtering)
19-
- Queries (see [storelike::Query])
19+
- Queries (see [query::Query])
2020
- [hierarchy] for authorization
2121
- [crate::endpoints::Endpoint] for custom API endpoints
2222
- [config::Config] files. (requires `config` feature)
@@ -87,6 +87,7 @@ pub mod parse;
8787
#[cfg(feature = "db")]
8888
pub mod plugins;
8989
pub mod populate;
90+
pub mod query;
9091
pub mod resources;
9192
pub mod schema;
9293
pub mod serialize;
@@ -108,6 +109,7 @@ pub use commit::Commit;
108109
pub use db::Db;
109110
pub use errors::AtomicError;
110111
pub use errors::AtomicErrorType;
112+
pub use query::Query;
111113
pub use resources::Resource;
112114
pub use store::Store;
113115
pub use storelike::Storelike;

lib/src/plugins/chatroom.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use crate::{
88
agents::ForAgent,
99
commit::{CommitBuilder, CommitResponse},
1010
errors::AtomicResult,
11-
storelike::Query,
1211
urls::{self, PARENT},
13-
utils, Resource, Storelike, Value,
12+
utils, Query, Resource, Storelike, Value,
1413
};
1514

1615
// Find the messages for the ChatRoom

lib/src/plugins/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,6 @@ pub mod register;
4949
pub mod reset_pubkey;
5050
pub mod search;
5151
pub mod versioning;
52+
53+
// Utilities / helpers
54+
mod utils;

lib/src/plugins/prunetests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::{
22
endpoints::{Endpoint, HandleGetContext, HandlePostContext},
33
errors::AtomicResult,
4-
storelike::Query,
5-
urls, Resource, Storelike, Value,
4+
urls, Query, Resource, Storelike, Value,
65
};
76

87
pub fn prune_tests_endpoint() -> Endpoint {

lib/src/plugins/register.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{
1212
Resource, Storelike, Value,
1313
};
1414

15+
use super::utils::return_success;
16+
1517
pub fn register_endpoint() -> Endpoint {
1618
Endpoint {
1719
path: urls::PATH_REGISTER.to_string(),
@@ -21,8 +23,8 @@ pub fn register_endpoint() -> Endpoint {
2123
].into(),
2224
description: "Allows new users to easily, in one request, make both an Agent and a Drive. This drive will be created at the subdomain of `name`.".to_string(),
2325
shortname: "register".to_string(),
24-
handle: Some(construct_register_redirect),
2526
handle_post: None,
27+
handle: Some(handle_register_name_and_email),
2628
}
2729
}
2830

@@ -43,12 +45,17 @@ struct MailConfirmation {
4345
pub name: String,
4446
}
4547

46-
#[tracing::instrument()]
47-
pub fn construct_register_redirect(context: HandleGetContext) -> AtomicResult<Resource> {
48+
#[tracing::instrument]
49+
pub fn handle_register_name_and_email(context: HandleGetContext) -> AtomicResult<Resource> {
50+
let HandleGetContext {
51+
subject,
52+
store: _,
53+
for_agent: _,
54+
} = context;
4855
let mut name_option = None;
4956
let mut email_option: Option<EmailAddress> = None;
5057
let store = context.store;
51-
for (k, v) in context.subject.query_pairs() {
58+
for (k, v) in subject.query_pairs() {
5259
match k.as_ref() {
5360
"name" | urls::NAME => name_option = Some(v.to_string()),
5461
"email" => email_option = Some(EmailAddress::new(v.to_string())?),
@@ -93,14 +100,7 @@ pub fn construct_register_redirect(context: HandleGetContext) -> AtomicResult<Re
93100
.unwrap_or_else(|e| tracing::error!("Error sending email: {}", e));
94101
});
95102

96-
// Here we probably want to return some sort of SuccesMessage page.
97-
// Not sure what that should be.
98-
let mut resource = Resource::new_generate_subject(store);
99-
resource.set_string(urls::DESCRIPTION.into(), "success", store)?;
100-
101-
// resource.set_propval(urls::, value, store)
102-
103-
Ok(resource)
103+
return_success()
104104
}
105105

106106
#[tracing::instrument()]

0 commit comments

Comments
 (0)