|
| 1 | +// Copyright (C) 2024 Quickwit, Inc. |
| 2 | +// |
| 3 | +// Quickwit is offered under the AGPL v3.0 and as commercial software. |
| 4 | +// For commercial licensing, contact us at [email protected]. |
| 5 | +// |
| 6 | +// AGPL: |
| 7 | +// This program is free software: you can redistribute it and/or modify |
| 8 | +// it under the terms of the GNU Affero General Public License as |
| 9 | +// published by the Free Software Foundation, either version 3 of the |
| 10 | +// License, or (at your option) any later version. |
| 11 | +// |
| 12 | +// This program is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +// GNU Affero General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU Affero General Public License |
| 18 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +use std::task::{Context, Poll}; |
| 21 | + |
| 22 | +use futures::future::Either; |
| 23 | +use http::Request; |
| 24 | +use tokio::task::futures::TaskLocalFuture; |
| 25 | +use tokio_inherit_task_local::TaskLocalInheritableTable; |
| 26 | +use tower::{Layer, Service}; |
| 27 | +use tracing::debug; |
| 28 | + |
| 29 | +use super::AuthorizationToken; |
| 30 | + |
| 31 | +#[derive(Clone, Copy, Debug)] |
| 32 | +pub struct AuthorizationTokenExtractionLayer; |
| 33 | + |
| 34 | +impl<S: Clone> Layer<S> for AuthorizationTokenExtractionLayer { |
| 35 | + type Service = AuthorizationTokenExtractionService<S>; |
| 36 | + |
| 37 | + fn layer(&self, service: S) -> Self::Service { |
| 38 | + AuthorizationTokenExtractionService { service } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Clone)] |
| 43 | +pub struct AuthorizationTokenExtractionService<S> { |
| 44 | + service: S, |
| 45 | +} |
| 46 | + |
| 47 | +fn get_authorization_token_opt(headers: &http::HeaderMap) -> Option<AuthorizationToken> { |
| 48 | + let authorization_header_value = headers.get("Authorization")?; |
| 49 | + let authorization_header_str = authorization_header_value.to_str().ok()?; |
| 50 | + crate::get_auth_token_from_str(authorization_header_str).ok() |
| 51 | +} |
| 52 | + |
| 53 | +impl<B, S> Service<Request<B>> for AuthorizationTokenExtractionService<S> |
| 54 | +where S: Service<Request<B>> |
| 55 | +{ |
| 56 | + type Response = S::Response; |
| 57 | + type Error = S::Error; |
| 58 | + type Future = Either<S::Future, TaskLocalFuture<TaskLocalInheritableTable, S::Future>>; |
| 59 | + |
| 60 | + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 61 | + self.service.poll_ready(cx) |
| 62 | + } |
| 63 | + |
| 64 | + fn call(&mut self, request: Request<B>) -> Self::Future { |
| 65 | + let authorization_token_opt = get_authorization_token_opt(request.headers()); |
| 66 | + debug!(authorization_token_opt = ?authorization_token_opt, "Authorization token extracted"); |
| 67 | + let fut = self.service.call(request); |
| 68 | + if let Some(authorization_token) = authorization_token_opt { |
| 69 | + Either::Right(crate::execute_with_authorization(authorization_token, fut)) |
| 70 | + } else { |
| 71 | + Either::Left(fut) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments