Add docs
parent: tbd commit: 51aad53
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 | |
105 | |
106 | |
107 | async |
108 | &mut self, |
109 | request: RegisterAccountRequest, |
110 | |
111 | const BITS: usize = 2048; |
112 | |
113 | let private_key = new .unwrap; |
114 | let public_key = from; |
115 | |
116 | let key = |
117 | let mut target: = ; |
118 | |
119 | let mut index = 0; |
120 | let mut iterator = request.password.as_bytes .iter; |
121 | while index < 32 |
122 | if let Some = iterator.next |
123 | target= *next; |
124 | index += 1; |
125 | else |
126 | iterator = request.password.as_bytes .iter; |
127 | |
128 | |
129 | |
130 | target |
131 | ; |
132 | |
133 | let key: & = &key.into; |
134 | let cipher = new; |
135 | let nonce = generate_nonce; |
136 | let ciphertext = cipher |
137 | .encrypt |
138 | .unwrap; |
139 | |
140 | let private_key_enc = format!; |
141 | |
142 | let salt = generate; |
143 | |
144 | let argon2 = default; |
145 | |
146 | let password_hash = argon2 |
147 | .hash_password |
148 | .unwrap |
149 | .to_string; |
150 | |
151 | let user = match query_as! |
152 | UserRow, |
153 | r#"INSERT INTO users VALUES ($1, null, $2, null, null, $3, $4, $5) returning *"#, |
154 | request.username, |
155 | "example.com", |
156 | password_hash, |
157 | public_key |
158 | .to_public_key_pem |
159 | .unwrap, |
160 | private_key_enc |
161 | |
162 | .fetch_one |
163 | .await |
164 | |
165 | Ok => user, |
166 | Err => |
167 | error!; |
168 | |
169 | return Err; |
170 | |
171 | ; |
172 | |
173 | let mut granter = self.auth_granter.lock .await; |
174 | let token = granter |
175 | .create_token_for |
176 | &User |
177 | username: user.username, |
178 | instance: self.this_instance.clone, |
179 | , |
180 | &self.this_instance, |
181 | |
182 | .await; |
183 | |
184 | Ok |
185 | |
186 | |
187 | async |
188 | &mut self, |
189 | _request: AuthenticationTokenRequest, |
190 | |
191 | todo! |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | pub username: String, |
198 | pub image_url: , |
199 | pub display_name: , |
200 | pub bio: , |
201 | pub email: , |
202 | pub password: String, |
203 | pub public_key: String, |
204 | pub enc_private_key: , |
205 | |
206 |