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