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