-
Notifications
You must be signed in to change notification settings - Fork 24
refactor: add auth middleware #505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e930e4
refactor: add auth middleware
gusinacio f33f6dd
test: add tap test
gusinacio bdfdc13
test: add middleware composition test
gusinacio c4f9da6
refactor: use auth middleware
gusinacio 159ff4a
refactor: create context middleware
gusinacio fecfc0b
refactor: remove option from tap receipt
gusinacio 6365178
docs: add documentation to auth middleware
gusinacio 77aaa77
test: split composition tests
gusinacio 95dee9e
test: split tap middleware tests
gusinacio f805565
docs: fix typos
gusinacio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
| // Copyright 2023-, Edge & Node, GraphOps, and Semiotic Labs. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Injects tap context to be used by the checks | ||
| //! | ||
| //! Requires Deployment Id extension to available | ||
|
|
||
| use serde_json::value::RawValue; | ||
| use std::sync::Arc; | ||
|
|
||
| use axum::{ | ||
| body::to_bytes, | ||
| extract::{Path, Request}, | ||
| middleware::Next, | ||
| response::Response, | ||
| RequestExt, | ||
| }; | ||
| use tap_core::receipt::Context; | ||
| use thegraph_core::DeploymentId; | ||
|
|
||
| use crate::{error::IndexerServiceError, tap::AgoraQuery}; | ||
|
|
||
| #[derive(Debug, serde::Deserialize, serde::Serialize)] | ||
| struct QueryBody { | ||
| query: String, | ||
| variables: Option<Box<RawValue>>, | ||
| } | ||
|
|
||
| pub async fn context_middleware( | ||
| mut request: Request, | ||
| next: Next, | ||
| ) -> Result<Response, IndexerServiceError> { | ||
| let deployment_id = match request.extensions().get::<DeploymentId>() { | ||
| Some(deployment) => *deployment, | ||
| None => match request.extract_parts::<Path<DeploymentId>>().await { | ||
| Ok(Path(deployment)) => deployment, | ||
| Err(_) => return Err(IndexerServiceError::DeploymentIdNotFound), | ||
| }, | ||
| }; | ||
|
|
||
| let (mut parts, body) = request.into_parts(); | ||
| let bytes = to_bytes(body, usize::MAX).await?; | ||
| let query_body: QueryBody = serde_json::from_slice(&bytes)?; | ||
|
|
||
| let variables = query_body | ||
| .variables | ||
| .as_ref() | ||
| .map(ToString::to_string) | ||
| .unwrap_or_default(); | ||
|
|
||
| let mut ctx = Context::new(); | ||
| ctx.insert(AgoraQuery { | ||
| deployment_id, | ||
| query: query_body.query.clone(), | ||
| variables, | ||
| }); | ||
| parts.extensions.insert(Arc::new(ctx)); | ||
| let request = Request::from_parts(parts, bytes.into()); | ||
| Ok(next.run(request).await) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use axum::{ | ||
| body::Body, | ||
| http::{Extensions, Request}, | ||
| middleware::from_fn, | ||
| routing::get, | ||
| Router, | ||
| }; | ||
| use reqwest::StatusCode; | ||
| use tap_core::receipt::Context; | ||
| use test_assets::ESCROW_SUBGRAPH_DEPLOYMENT; | ||
| use tower::ServiceExt; | ||
|
|
||
| use crate::{ | ||
| middleware::inject_context::{context_middleware, QueryBody}, | ||
| tap::AgoraQuery, | ||
| }; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_context_middleware() { | ||
| let middleware = from_fn(context_middleware); | ||
| let deployment = *ESCROW_SUBGRAPH_DEPLOYMENT; | ||
| let query_body = QueryBody { | ||
| query: "hello".to_string(), | ||
| variables: None, | ||
| }; | ||
| let body = serde_json::to_string(&query_body).unwrap(); | ||
|
|
||
| let handle = move |extensions: Extensions| async move { | ||
| let ctx = extensions | ||
| .get::<Arc<Context>>() | ||
| .expect("Should contain context"); | ||
| let agora = ctx.get::<AgoraQuery>().expect("should contain agora query"); | ||
| assert_eq!(agora.deployment_id, deployment); | ||
| assert_eq!(agora.query, query_body.query); | ||
|
|
||
| let variables = query_body | ||
| .variables | ||
| .as_ref() | ||
| .map(ToString::to_string) | ||
| .unwrap_or_default(); | ||
| assert_eq!(agora.variables, variables); | ||
| Body::empty() | ||
| }; | ||
|
|
||
| let app = Router::new().route("/", get(handle)).layer(middleware); | ||
|
|
||
| let res = app | ||
| .oneshot( | ||
| Request::builder() | ||
| .uri("/") | ||
| .extension(deployment) | ||
| .extension(deployment) | ||
| .body(body) | ||
| .unwrap(), | ||
| ) | ||
| .await | ||
| .unwrap(); | ||
| assert_eq!(res.status(), StatusCode::OK); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.