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