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