(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
| 242 | type Output = Result<Response, Infallible>; |
| 243 | |
| 244 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 245 | let config = Config::get(); |
| 246 | loop { |
| 247 | match &mut self.state { |
| 248 | ProcessingState::ValidatingAuth => { |
| 249 | if self.endpoint.annotation.is_jwt_auth() { |
| 250 | self.jwt_claim = { |
| 251 | let future = self |
| 252 | .request |
| 253 | .extract_parts::<TypedHeader<Authorization<Bearer>>>(); |
| 254 | tokio::pin!(future); |
| 255 | match future.poll(cx) { |
| 256 | Poll::Pending => return Poll::Pending, |
| 257 | Poll::Ready(Ok(bearer)) => { |
| 258 | let key = |
| 259 | DecodingKey::from_secret(config.auth.jwt.secret.as_bytes()); |
| 260 | let validation = Validation::new(Algorithm::HS256); |
| 261 | // Decode token |
| 262 | let token_data: TokenData<Value> = |
| 263 | decode(bearer.token(), &key, &validation).unwrap(); |
| 264 | Some(token_data.claims) |
| 265 | } |
| 266 | Poll::Ready(Err(e)) => { |
| 267 | return Poll::Ready(Ok(ServerError::AuthenticationError { |
| 268 | message: e.to_string(), |
| 269 | } |
| 270 | .into_response())); |
| 271 | } |
| 272 | } |
| 273 | }; |
| 274 | } else { |
| 275 | // Baisc auth |
| 276 | // Extract the token from the authorization header |
| 277 | let future = self |
| 278 | .request |
| 279 | .extract_parts::<TypedHeader<Authorization<Basic>>>(); |
| 280 | tokio::pin!(future); |
| 281 | match future.poll(cx) { |
| 282 | Poll::Pending => return Poll::Pending, |
| 283 | Poll::Ready(Ok(basic)) => { |
| 284 | if let Some(b) = config.auth.basic.as_ref() { |
| 285 | if *b.username != basic.username() |
| 286 | || *b.password != basic.password() |
| 287 | { |
| 288 | return Poll::Ready(Ok(ServerError::AuthenticationError { |
| 289 | message: "Invalid username or password".to_string(), |
| 290 | } |
| 291 | .into_response())); |
| 292 | } |
| 293 | } else { |
| 294 | return Poll::Ready(Ok(ServerError::AuthenticationError { |
| 295 | message: "Basic auth is not configured".to_string(), |
| 296 | } |
| 297 | .into_response())); |
| 298 | } |
| 299 | } |
| 300 | Poll::Ready(Err(e)) => { |
| 301 | return Poll::Ready(Ok(ServerError::AuthenticationError { |
nothing calls this directly
no test coverage detected