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