Add Instance Authentication
parent: tbd commit: 5a042fd
1 | use ; |
2 | |
3 | use ; |
4 | use ; |
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 | user: User, |
22 | generated_for: Instance, |
23 | exp: u64, |
24 | |
25 | |
26 | |
27 | pub config: Table, |
28 | |
29 | |
30 | |
31 | pub async |
32 | &mut self, |
33 | raw_request: , |
34 | |
35 | let request = raw_request.inner .await; |
36 | |
37 | info!; |
38 | raw_request |
39 | .validate |
40 | url: String from, |
41 | |
42 | .await |
43 | .unwrap; |
44 | |
45 | let secret_key = self.config |
46 | .as_str |
47 | .unwrap; |
48 | let private_key = |
49 | let mut file = open |
50 | .await |
51 | .unwrap; |
52 | |
53 | let mut key = vec!; |
54 | file.read_to_end .await.unwrap; |
55 | |
56 | key |
57 | ; |
58 | |
59 | if request.secret_key != secret_key |
60 | error!; |
61 | |
62 | panic! |
63 | |
64 | |
65 | let encoding_key = from_rsa_pem .unwrap; |
66 | |
67 | let claims = UserTokenMetadata |
68 | user: User |
69 | username: String from, |
70 | instance: Instance |
71 | url: String from, |
72 | , |
73 | , |
74 | generated_for: Instance |
75 | url: String from, |
76 | , |
77 | exp: |
78 | + from_secs |
79 | .as_secs, |
80 | ; |
81 | |
82 | let token = encode |
83 | & new, |
84 | &claims, |
85 | &encoding_key, |
86 | |
87 | .unwrap; |
88 | |
89 | Ok |
90 | |
91 | |
92 | pub async |
93 | &mut self, |
94 | raw_request: , |
95 | |
96 | let request = raw_request.inner .await; |
97 | |
98 | // let server_public_key = { |
99 | // let mut file = File::open(self.config["keys"]["public"].as_str().unwrap()) |
100 | // .await |
101 | // .unwrap(); |
102 | |
103 | // let mut key = String::default(); |
104 | // file.read_to_string(&mut key).await.unwrap(); |
105 | |
106 | // key |
107 | // }; |
108 | |
109 | let server_public_key = public_key |
110 | url: String from, |
111 | |
112 | .await |
113 | .unwrap; |
114 | |
115 | println!; |
116 | |
117 | let verification_key = from_rsa_pem .unwrap; |
118 | |
119 | let data: = decode |
120 | &request.token, |
121 | &verification_key, |
122 | & new, |
123 | |
124 | .unwrap; |
125 | |
126 | info!; |
127 | |
128 | let secret_key = self.config |
129 | .as_str |
130 | .unwrap; |
131 | |
132 | if request.secret_key != secret_key |
133 | error!; |
134 | |
135 | panic! |
136 | |
137 | // Validate request |
138 | raw_request |
139 | .validate |
140 | .await |
141 | .unwrap; |
142 | info!; |
143 | |
144 | let private_key = |
145 | let mut file = open |
146 | .await |
147 | .unwrap; |
148 | |
149 | let mut key = vec!; |
150 | file.read_to_end .await.unwrap; |
151 | |
152 | key |
153 | ; |
154 | |
155 | let encoding_key = from_rsa_pem .unwrap; |
156 | |
157 | let claims = UserTokenMetadata |
158 | // TODO: Probably exploitable |
159 | user: data.claims.user, |
160 | generated_for: data.claims.generated_for, |
161 | exp: |
162 | + from_secs |
163 | .as_secs, |
164 | ; |
165 | |
166 | let token = encode |
167 | & new, |
168 | &claims, |
169 | &encoding_key, |
170 | |
171 | .unwrap; |
172 | |
173 | Ok |
174 | new_token: Some, |
175 | |
176 | |
177 | |
178 | |
179 | async |
180 | let key = get |
181 | .await? |
182 | .text |
183 | .await?; |
184 | |
185 | Ok |
186 | |
187 |