Add more aggressive key caching
parent: tbd commit: 5bc92ad
1 | use Error; |
2 | use |
3 | , |
4 | |
5 | , |
6 | , | Instance
7 | , | User
8 | , |
9 | ; |
10 | use ; |
11 | use ; |
12 | use ; |
13 | use Table; |
14 | |
15 | use crate PublicKeyCache; |
16 | |
17 | |
18 | pub config: Table, |
19 | pub instance: Instance, |
20 | |
21 | |
22 | |
23 | async |
24 | let _secret_key = self.config |
25 | .as_str |
26 | .unwrap; |
27 | let mut file = open |
28 | self.config |
29 | .as_str |
30 | .unwrap, |
31 | |
32 | .await |
33 | .unwrap; |
34 | |
35 | let mut key = vec!; |
36 | file.read_to_end .await.unwrap; |
37 | |
38 | key |
39 | |
40 | |
41 | pub async |
42 | &mut self, |
43 | user: &User, |
44 | generated_for: &Instance, |
45 | |
46 | let private_key = self.private_key .await; |
47 | |
48 | let encoding_key = from_rsa_pem .unwrap; |
49 | |
50 | let claims = UserTokenMetadata |
51 | user: user.clone, |
52 | generated_for: generated_for.clone, |
53 | exp: |
54 | + from_secs |
55 | .as_secs, |
56 | ; |
57 | |
58 | encode |
59 | & new, |
60 | &claims, |
61 | &encoding_key, |
62 | |
63 | .unwrap |
64 | |
65 | |
66 | pub async |
67 | &mut self, |
68 | issued_for: impl , |
69 | username: String, |
70 | _password: String, |
71 | |
72 | let private_key = |
73 | let mut file = open |
74 | self.config |
75 | .as_str |
76 | .unwrap, |
77 | |
78 | .await |
79 | .unwrap; |
80 | |
81 | let mut key = vec!; |
82 | file.read_to_end .await.unwrap; |
83 | |
84 | key |
85 | ; |
86 | |
87 | let encoding_key = from_rsa_pem .unwrap; |
88 | |
89 | let claims = UserTokenMetadata |
90 | user: User |
91 | username, |
92 | instance: self.instance.clone, |
93 | , |
94 | generated_for: issued_for.to_owned, |
95 | exp: |
96 | + from_secs |
97 | .as_secs, |
98 | ; |
99 | |
100 | let token = encode |
101 | & new, |
102 | &claims, |
103 | &encoding_key, |
104 | |
105 | .unwrap; |
106 | |
107 | Ok |
108 | token: from, |
109 | |
110 | |
111 | |
112 | pub async |
113 | &mut self, |
114 | issued_for: &Instance, |
115 | key_cache: & , |
116 | token: UserAuthenticationToken, |
117 | |
118 | let mut key_cache = key_cache.lock .await; |
119 | let server_public_key = key_cache.get .await?; |
120 | drop; |
121 | |
122 | let verification_key = from_rsa_pem .unwrap; |
123 | |
124 | let data: = decode |
125 | token.as_ref, |
126 | &verification_key, |
127 | & new, |
128 | |
129 | .unwrap; |
130 | |
131 | if data.claims.generated_for != *issued_for |
132 | panic! |
133 | |
134 | |
135 | info!; |
136 | |
137 | let private_key = |
138 | let mut file = open |
139 | self.config |
140 | .as_str |
141 | .unwrap, |
142 | |
143 | .await |
144 | .unwrap; |
145 | |
146 | let mut key = vec!; |
147 | file.read_to_end .await.unwrap; |
148 | |
149 | key |
150 | ; |
151 | |
152 | let encoding_key = from_rsa_pem .unwrap; |
153 | |
154 | let claims = UserTokenMetadata |
155 | // TODO: Probably exploitable |
156 | user: data.claims.user, |
157 | generated_for: issued_for.clone, |
158 | exp: |
159 | + from_secs |
160 | .as_secs, |
161 | ; |
162 | |
163 | let token = encode |
164 | & new, |
165 | &claims, |
166 | &encoding_key, |
167 | |
168 | .unwrap; |
169 | |
170 | Ok |
171 | new_token: Some, |
172 | |
173 | |
174 | |
175 |