Base protocol refactor complete
parent: tbd commit: 079d544
1 | use Arc; |
2 | |
3 | use Error; |
4 | |
5 | use ; |
6 | use ; |
7 | use ; |
8 | use |
9 | |
10 | , | UserAuthenticationToken
11 | , | Instance
12 | , |
13 | , |
14 | , |
15 | , |
16 | , | AnyValue
17 | ; |
18 | use |
19 | , |
20 | , | OsRng
21 | RsaPrivateKey, RsaPublicKey, |
22 | ; |
23 | |
24 | use ExposeSecret; |
25 | use Value; |
26 | use PgPool; |
27 | use Mutex; |
28 | |
29 | use crate AuthenticationTokenGranter; |
30 | |
31 | use ; |
32 | |
33 | |
34 | pub pg_pool: PgPool, |
35 | pub this_instance: Instance, |
36 | pub auth_granter: , |
37 | pub settings_provider: , |
38 | |
39 | |
40 | |
41 | |
42 | pool: PgPool, |
43 | this_instance: &Instance, |
44 | granter: , |
45 | settings_provider: , |
46 | |
47 | Self |
48 | pg_pool: pool, |
49 | this_instance: this_instance.clone, |
50 | auth_granter: granter, |
51 | settings_provider, |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | async |
59 | Ok |
60 | "display_name" => unsafe |
61 | from_raw |
62 | , |
63 | "bio" => unsafe |
64 | from_raw |
65 | , |
66 | _ => |
67 | return Err; |
68 | |
69 | |
70 | |
71 | async |
72 | let mut provider = self.settings_provider.lock .await; |
73 | |
74 | Ok |
75 | |
76 | |
77 | async |
78 | &mut self, |
79 | user: &User, |
80 | name: &str, |
81 | setting: &Value, |
82 | |
83 | let mut provider = self.settings_provider.lock .await; |
84 | |
85 | provider |
86 | .user_write |
87 | .await |
88 | |
89 | |
90 | async |
91 | Ok |
92 | UserRow, |
93 | r#"SELECT * FROM users WHERE username = $1"#, |
94 | user.username |
95 | |
96 | .fetch_one |
97 | .await |
98 | .is_ok |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | async |
105 | &mut self, |
106 | request: RegisterAccountRequest, |
107 | |
108 | const BITS: usize = 2048; |
109 | |
110 | let private_key = new .unwrap; |
111 | let public_key = from; |
112 | |
113 | let key = |
114 | let mut target: = ; |
115 | |
116 | let mut index = 0; |
117 | let mut iterator = request.password.expose_secret .0.as_bytes .iter; |
118 | while index < 32 |
119 | if let Some = iterator.next |
120 | target= *next; |
121 | index += 1; |
122 | else |
123 | iterator = request.password.expose_secret .0.as_bytes .iter; |
124 | |
125 | |
126 | |
127 | target |
128 | ; |
129 | |
130 | let key: & = &key.into; |
131 | let cipher = new; |
132 | let nonce = generate_nonce; |
133 | let ciphertext = cipher |
134 | .encrypt |
135 | .unwrap; |
136 | |
137 | let private_key_enc = format!; |
138 | |
139 | let salt = generate; |
140 | |
141 | let argon2 = default; |
142 | |
143 | let password_hash = argon2 |
144 | .hash_password |
145 | .unwrap |
146 | .to_string; |
147 | |
148 | let user = match query_as! |
149 | UserRow, |
150 | r#"INSERT INTO users VALUES ($1, $2, $3, $4, $5) returning *"#, |
151 | request.username, |
152 | "example.com", |
153 | password_hash, |
154 | public_key |
155 | .to_public_key_pem |
156 | .unwrap, |
157 | private_key_enc |
158 | |
159 | .fetch_one |
160 | .await |
161 | |
162 | Ok => user, |
163 | Err => |
164 | error!; |
165 | |
166 | return Err; |
167 | |
168 | ; |
169 | |
170 | let mut granter = self.auth_granter.lock .await; |
171 | let token = granter |
172 | .create_token_for |
173 | &User |
174 | username: user.username, |
175 | instance: self.this_instance.clone, |
176 | , |
177 | &self.this_instance, |
178 | |
179 | .await; |
180 | |
181 | Ok |
182 | |
183 | |
184 | async |
185 | &mut self, |
186 | source: &Instance, |
187 | request: AuthenticationTokenRequest, |
188 | |
189 | info!; |
190 | let user = query_as! |
191 | UserRow, |
192 | r#"SELECT * FROM users WHERE username = $1"#, |
193 | request.username |
194 | |
195 | .fetch_one |
196 | .await?; |
197 | |
198 | let hash = new .unwrap; |
199 | |
200 | if default |
201 | .verify_password |
202 | .is_err |
203 | |
204 | info!; |
205 | return Err; |
206 | |
207 | |
208 | let mut granter = self.auth_granter.lock .await; |
209 | let token = granter |
210 | .create_token_for |
211 | &User |
212 | username: user.username, |
213 | instance: self.this_instance.clone, |
214 | , |
215 | &source, |
216 | |
217 | .await; |
218 | |
219 | Ok |
220 | |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | pub username: String, |
227 | pub email: , |
228 | pub password: String, |
229 | pub public_key: String, |
230 | pub enc_private_key: , |
231 | |
232 | |
233 | |
234 | |
235 | pub username: String, |
236 | pub name: String, |
237 | pub value: String, |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | InvalidPassword, |
244 | |
245 |