-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
94b575a
commit 2ab75ef
Showing
23 changed files
with
304 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
// Copyright (C) 2024 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at [email protected]. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::fmt; | ||
use std::task::{Context, Poll}; | ||
|
||
|
@@ -7,6 +26,7 @@ use tower::{Layer, Service}; | |
|
||
use crate::AuthorizationError; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct AuthorizationLayer; | ||
|
||
impl<S: Clone> Layer<S> for AuthorizationLayer { | ||
|
74 changes: 74 additions & 0 deletions
74
quickwit/quickwit-authorize/src/enterprise/authorization_token_extraction_layer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (C) 2024 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at [email protected]. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use std::task::{Context, Poll}; | ||
|
||
use futures::future::Either; | ||
use http::Request; | ||
use tokio::task::futures::TaskLocalFuture; | ||
use tokio_inherit_task_local::TaskLocalInheritableTable; | ||
use tower::{Layer, Service}; | ||
use tracing::debug; | ||
|
||
use super::AuthorizationToken; | ||
|
||
#[derive(Clone, Copy, Debug)] | ||
pub struct AuthorizationTokenExtractionLayer; | ||
|
||
impl<S: Clone> Layer<S> for AuthorizationTokenExtractionLayer { | ||
type Service = AuthorizationTokenExtractionService<S>; | ||
|
||
fn layer(&self, service: S) -> Self::Service { | ||
AuthorizationTokenExtractionService { service } | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct AuthorizationTokenExtractionService<S> { | ||
service: S, | ||
} | ||
|
||
fn get_authorization_token_opt(headers: &http::HeaderMap) -> Option<AuthorizationToken> { | ||
let authorization_header_value = headers.get("Authorization")?; | ||
let authorization_header_str = authorization_header_value.to_str().ok()?; | ||
crate::get_auth_token_from_str(authorization_header_str).ok() | ||
} | ||
|
||
impl<B, S> Service<Request<B>> for AuthorizationTokenExtractionService<S> | ||
where S: Service<Request<B>> | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = Either<S::Future, TaskLocalFuture<TaskLocalInheritableTable, S::Future>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.service.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, request: Request<B>) -> Self::Future { | ||
let authorization_token_opt = get_authorization_token_opt(request.headers()); | ||
debug!(authorization_token_opt = ?authorization_token_opt, "Authorization token extracted"); | ||
let fut = self.service.call(request); | ||
if let Some(authorization_token) = authorization_token_opt { | ||
Either::Right(crate::execute_with_authorization(authorization_token, fut)) | ||
} else { | ||
Either::Left(fut) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.