-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4892 from systeminit/sf/sdf-api-perms
feat: add `WorkspacePermission` axum middleware & v2 change set apply
- Loading branch information
Showing
21 changed files
with
323 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
relation approver: user | ||
relation owner: user | ||
permission approve = approver+owner | ||
permission manage = owner | ||
} |
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
This file was deleted.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod workspace_permission; | ||
|
||
pub use self::workspace_permission::{WorkspacePermission, WorkspacePermissionLayer}; |
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,96 @@ | ||
use std::task::{Context, Poll}; | ||
|
||
use axum::{ | ||
body::Body, | ||
extract::FromRequestParts, | ||
http::Request, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use futures::future::BoxFuture; | ||
use permissions::{Permission, PermissionBuilder}; | ||
use tower::{Layer, Service}; | ||
|
||
use crate::{ | ||
extract::{self, Authorization}, | ||
AppState, | ||
}; | ||
|
||
#[derive(Clone)] | ||
pub struct WorkspacePermissionLayer { | ||
state: AppState, | ||
permission: Permission, | ||
} | ||
|
||
impl WorkspacePermissionLayer { | ||
pub fn new(state: AppState, permission: Permission) -> Self { | ||
Self { state, permission } | ||
} | ||
} | ||
|
||
impl<S> Layer<S> for WorkspacePermissionLayer { | ||
type Service = WorkspacePermission<S>; | ||
|
||
fn layer(&self, inner: S) -> Self::Service { | ||
WorkspacePermission { | ||
inner, | ||
state: self.state.clone(), | ||
permission: self.permission, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct WorkspacePermission<S> { | ||
inner: S, | ||
state: AppState, | ||
permission: Permission, | ||
} | ||
|
||
impl<S> Service<Request<Body>> for WorkspacePermission<S> | ||
where | ||
S: Service<Request<Body>, Response = Response> + Clone + Send + 'static, | ||
S::Future: Send + 'static, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.inner.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, req: Request<Body>) -> Self::Future { | ||
let mut me = self.clone(); | ||
|
||
Box::pin(async move { | ||
let (mut parts, body) = req.into_parts(); | ||
|
||
let Authorization(claim) = | ||
match Authorization::from_request_parts(&mut parts, &me.state).await { | ||
Ok(claim) => claim, | ||
Err(err) => return Ok(err.into_response()), | ||
}; | ||
|
||
if let Some(client) = me.state.spicedb_client() { | ||
let is_allowed = match PermissionBuilder::new() | ||
.workspace_object(claim.workspace_pk.into()) | ||
.permission(me.permission) | ||
.user_subject(claim.user_pk.into()) | ||
.has_permission(client) | ||
.await | ||
{ | ||
Ok(is_allowed) => is_allowed, | ||
Err(_) => return Ok(extract::unauthorized_error().into_response()), | ||
}; | ||
if !is_allowed { | ||
return Ok(extract::unauthorized_error().into_response()); | ||
} | ||
} | ||
|
||
let req = Request::from_parts(parts, body); | ||
|
||
let response = me.inner.call(req).await?; | ||
Ok(response) | ||
}) | ||
} | ||
} |
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.