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