Fix authentication
parent: tbd commit: cef865c
1 | use Error; |
2 | use ; |
3 | use ; |
4 | use SystemTime; |
5 | use ; |
6 | use Table; |
7 | |
8 | use crate:: |
9 | |
10 | |
11 | AuthenticationTokenRequest, AuthenticationTokenResponse, TokenExtensionRequest, |
12 | TokenExtensionResponse, |
13 | , |
14 | InstanceAuthenticated, |
15 | , |
16 | , |
17 | ; |
18 | |
19 | |
20 | |
21 | pub user: User, |
22 | pub generated_for: Instance, |
23 | exp: u64, |
24 | |
25 | |
26 | |
27 | pub config: Table, |
28 | pub instance: Instance, |
29 | |
30 | |
31 | |
32 | async |
33 | let _secret_key = self.config |
34 | .as_str |
35 | .unwrap; |
36 | let mut file = open |
37 | self.config |
38 | .as_str |
39 | .unwrap, |
40 | |
41 | .await |
42 | .unwrap; |
43 | |
44 | let mut key = vec!; |
45 | file.read_to_end .await.unwrap; |
46 | |
47 | key |
48 | |
49 | |
50 | pub async |
51 | &mut self, |
52 | user: &User, |
53 | generated_for: &Instance, |
54 | |
55 | let private_key = self.private_key .await; |
56 | |
57 | let encoding_key = from_rsa_pem .unwrap; |
58 | |
59 | let claims = UserTokenMetadata |
60 | user: user.clone, |
61 | generated_for: generated_for.clone, |
62 | exp: |
63 | + from_secs |
64 | .as_secs, |
65 | ; |
66 | |
67 | encode |
68 | & new, |
69 | &claims, |
70 | &encoding_key, |
71 | |
72 | .unwrap |
73 | |
74 | |
75 | pub async |
76 | &mut self, |
77 | raw_request: , |
78 | |
79 | let request = raw_request.inner .await; |
80 | |
81 | info!; |
82 | raw_request |
83 | .validate |
84 | url: String from, |
85 | |
86 | .await |
87 | .unwrap; |
88 | |
89 | let secret_key = self.config |
90 | .as_str |
91 | .unwrap; |
92 | let private_key = |
93 | let mut file = open |
94 | self.config |
95 | .as_str |
96 | .unwrap, |
97 | |
98 | .await |
99 | .unwrap; |
100 | |
101 | let mut key = vec!; |
102 | file.read_to_end .await.unwrap; |
103 | |
104 | key |
105 | ; |
106 | |
107 | if request.secret_key != secret_key |
108 | error!; |
109 | |
110 | panic! |
111 | |
112 | |
113 | let encoding_key = from_rsa_pem .unwrap; |
114 | |
115 | let claims = UserTokenMetadata |
116 | user: User |
117 | username: request.username.clone, |
118 | instance: self.instance.clone, |
119 | , |
120 | generated_for: raw_request.instance.clone, |
121 | exp: |
122 | + from_secs |
123 | .as_secs, |
124 | ; |
125 | |
126 | let token = encode |
127 | & new, |
128 | &claims, |
129 | &encoding_key, |
130 | |
131 | .unwrap; |
132 | |
133 | Ok |
134 | |
135 | |
136 | pub async |
137 | &mut self, |
138 | raw_request: , |
139 | |
140 | let request = raw_request.inner .await; |
141 | |
142 | // let server_public_key = { |
143 | // let mut file = File::open(self.config["keys"]["public"].as_str().unwrap()) |
144 | // .await |
145 | // .unwrap(); |
146 | |
147 | // let mut key = String::default(); |
148 | // file.read_to_string(&mut key).await.unwrap(); |
149 | |
150 | // key |
151 | // }; |
152 | |
153 | let server_public_key = public_key |
154 | url: String from, |
155 | |
156 | .await |
157 | .unwrap; |
158 | |
159 | let verification_key = from_rsa_pem .unwrap; |
160 | |
161 | let data: = decode |
162 | &request.token, |
163 | &verification_key, |
164 | & new, |
165 | |
166 | .unwrap; |
167 | |
168 | info!; |
169 | |
170 | let secret_key = self.config |
171 | .as_str |
172 | .unwrap; |
173 | |
174 | if request.secret_key != secret_key |
175 | error!; |
176 | |
177 | panic! |
178 | |
179 | // Validate request |
180 | raw_request |
181 | .validate |
182 | .await |
183 | .unwrap; |
184 | info!; |
185 | |
186 | let private_key = |
187 | let mut file = open |
188 | self.config |
189 | .as_str |
190 | .unwrap, |
191 | |
192 | .await |
193 | .unwrap; |
194 | |
195 | let mut key = vec!; |
196 | file.read_to_end .await.unwrap; |
197 | |
198 | key |
199 | ; |
200 | |
201 | let encoding_key = from_rsa_pem .unwrap; |
202 | |
203 | let claims = UserTokenMetadata |
204 | // TODO: Probably exploitable |
205 | user: data.claims.user, |
206 | generated_for: data.claims.generated_for, |
207 | exp: |
208 | + from_secs |
209 | .as_secs, |
210 | ; |
211 | |
212 | let token = encode |
213 | & new, |
214 | &claims, |
215 | &encoding_key, |
216 | |
217 | .unwrap; |
218 | |
219 | Ok |
220 | new_token: Some, |
221 | |
222 | |
223 | |
224 | |
225 | async |
226 | let key = get |
227 | .await? |
228 | .text |
229 | .await?; |
230 | |
231 | Ok |
232 | |
233 |