1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
pub(crate) mod sql_db;
use async_trait::async_trait;
use sqlx::{Postgres, Sqlite};
use std::sync::Arc;
use uuid::Uuid;
use crate::{
util, Address, AddressType, AddressValidation, Credential, CredentialId, CredentialType,
Identity, IdentityId, Session,
};
use self::sql_db::SqlClient;
#[derive(Debug, thiserror::Error, derive_more::Display)]
pub enum StoreError {
SqlClientError(#[from] sqlx::Error),
SerdeError(#[from] serde_json::Error),
ParseError(#[from] strum::ParseError),
StoreValueCannotBeParsedInvariant,
IdempotentCheckAlreadyExists,
}
#[async_trait]
pub trait Store: Send + Sync {
fn get_type(&self) -> StoreType;
}
pub enum StoreType {
Postgres { c: Arc<SqlClient<Postgres>> },
Sqlite { c: Arc<SqlClient<Sqlite>> },
}
#[async_trait]
pub(crate) trait Storable<'a> {
type Item;
type Lens;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError>;
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError>;
}
pub(crate) trait Lens {}
pub(crate) struct AddressLens<'a> {
pub id: Option<&'a Uuid>,
pub t: Option<&'a AddressType>,
}
impl<'a> Lens for AddressLens<'a> {}
pub(crate) struct AddressValidationLens<'a> {
pub id: Option<&'a Uuid>,
}
impl<'a> Lens for AddressValidationLens<'a> {}
pub(crate) struct IdentityLens<'a> {
pub id: Option<&'a Uuid>,
pub address_type: Option<&'a AddressType>,
pub validated_address: Option<bool>,
pub session_token_hash: Option<Vec<u8>>,
}
impl<'a> Lens for IdentityLens<'a> {}
pub(crate) struct SessionLens<'a> {
pub token_hash: Option<&'a Vec<u8>>,
pub identity_id: Option<&'a IdentityId>,
}
impl<'a> Lens for SessionLens<'a> {}
pub(crate) struct CredentialLens<'a> {
pub id: Option<CredentialId>,
pub identity_id: Option<IdentityId>,
pub t: Option<&'a CredentialType>,
pub restrict_by_key: Option<bool>,
}
impl<'a> Lens for CredentialLens<'a> {}
#[async_trait]
impl<'a> Storable<'a> for Address {
type Item = Address;
type Lens = AddressLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_address(self).await?,
StoreType::Sqlite { c } => c.write_address(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
let typ = lens.t.map(|at| at.to_string().clone());
let typ = typ.as_deref();
let val = lens.t.and_then(|at| at.get_value());
let val = val.as_deref();
Ok(match store.get_type() {
StoreType::Postgres { c } => c.find_address(lens.id, typ, val).await?,
StoreType::Sqlite { c } => c.find_address(lens.id, typ, val).await?,
})
}
}
#[async_trait]
impl<'a> Storable<'a> for AddressValidation {
type Item = AddressValidation;
type Lens = AddressValidationLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Sqlite { c } => c.write_address_validation(self).await?,
StoreType::Postgres { c } => c.write_address_validation(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
Ok(match store.get_type() {
StoreType::Postgres { c } => c.find_address_validation(lens.id).await?,
StoreType::Sqlite { c } => c.find_address_validation(lens.id).await?,
})
}
}
#[async_trait]
impl<'a> Storable<'a> for Identity {
type Item = Identity;
type Lens = IdentityLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_identity(self).await?,
StoreType::Sqlite { c } => c.write_identity(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
let val = lens.address_type.and_then(|at| at.get_value());
let val = val.as_deref();
Ok(match store.get_type() {
StoreType::Postgres { c } => {
c.find_identity(
lens.id,
val,
lens.validated_address,
&lens.session_token_hash,
)
.await?
}
StoreType::Sqlite { c } => {
c.find_identity(
lens.id,
val,
lens.validated_address,
&lens.session_token_hash,
)
.await?
}
})
}
}
#[async_trait]
impl<'a> Storable<'a> for Session {
type Item = Session;
type Lens = SessionLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
let token_hash = util::hash(&self.token);
match store.get_type() {
StoreType::Postgres { c } => c.write_session(self, &token_hash).await?,
StoreType::Sqlite { c } => c.write_session(self, &token_hash).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
let token = lens.token_hash.map(|t| t.clone()).unwrap_or(vec![]);
Ok(match store.get_type() {
StoreType::Postgres { c } => c.find_session(token, lens.identity_id).await?,
StoreType::Sqlite { c } => c.find_session(token, lens.identity_id).await?,
})
}
}
#[async_trait]
impl<'a> Storable<'a> for Credential {
type Item = Credential;
type Lens = CredentialLens<'a>;
async fn write(&self, store: Arc<dyn Store>) -> Result<(), StoreError> {
match store.get_type() {
StoreType::Postgres { c } => c.write_credential(self).await?,
StoreType::Sqlite { c } => c.write_credential(self).await?,
}
Ok(())
}
async fn find(
store: Arc<dyn Store>,
lens: &'a Self::Lens,
) -> Result<Vec<Self::Item>, StoreError> {
Ok(match store.get_type() {
StoreType::Postgres { c } => {
c.find_credential(
lens.id,
lens.identity_id,
lens.t,
if let Some(true) = lens.restrict_by_key {
true
} else {
false
},
)
.await?
}
StoreType::Sqlite { c } => {
c.find_credential(
lens.id,
lens.identity_id,
lens.t,
if let Some(true) = lens.restrict_by_key {
true
} else {
false
},
)
.await?
}
})
}
}
|