Skip to content
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

Add basic scaffolding for custom authorization functions #78

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions migration/src/refactor_platform_rs.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- SQL dump generated using DBML (dbml-lang.org)
-- SQL dump generated using DBML (dbml.dbdiagram.io)
-- Database: PostgreSQL
-- Generated at: 2024-10-31T16:04:10.825Z
-- Generated at: 2024-12-03T17:35:25.615Z


CREATE TYPE "status" AS ENUM (
Expand Down
6 changes: 4 additions & 2 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ use log::debug;
)]
pub async fn index(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file I just changed to use the user derived from the JWT rather than the user_id that is passed in as query params. Somewhat adjacent to the purpose of the rest of the code.

// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Query(params): Query<HashMap<String, String>>,
Query(mut params): Query<HashMap<String, String>>,
) -> Result<impl IntoResponse, Error> {
debug!("GET all Organizations");

params.insert("user_id".to_string(), user.id.to_string());

let organizations = OrganizationApi::find_by(app_state.db_conn_ref(), params).await?;

debug!("Found Organizations: {:?}", organizations);
Expand Down
1 change: 1 addition & 0 deletions web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use tower_http::cors::CorsLayer;
mod controller;
mod error;
pub(crate) mod extractors;
pub(crate) mod protect;
mod router;

pub async fn init_server(app_state: AppState) -> Result<()> {
Expand Down
34 changes: 34 additions & 0 deletions web/src/protect/coaching_relationships.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::{extractors::authenticated_user::AuthenticatedUser, AppState};
use axum::{
extract::{Path, Request, State},
http::StatusCode,
middleware::Next,
response::IntoResponse,
};

use entity::Id;
use entity_api::organization;
use std::collections::HashSet;

/// Checks that the organization record referenced by `organization_id`
/// exists and that the authenticated user is associated with i.t
/// Intended to be given to axum::middleware::from_fn_with_state in the router
pub(crate) async fn index(
State(app_state): State<AppState>,
AuthenticatedUser(user): AuthenticatedUser,
Path(organization_id): Path<Id>,
request: Request,
next: Next,
) -> impl IntoResponse {
let user_organization_ids = organization::find_by_user(app_state.db_conn_ref(), user.id)
.await
.unwrap_or(vec![])
.into_iter()
.map(|org| org.id)
.collect::<HashSet<Id>>();
if user_organization_ids.contains(&organization_id) {
next.run(request).await
} else {
(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response()
}
}
1 change: 1 addition & 0 deletions web/src/protect/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod coaching_relationships;
7 changes: 6 additions & 1 deletion web/src/router.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::AppState;
use crate::{protect, AppState};
use axum::{
middleware::from_fn_with_state,
routing::{delete, get, post, put},
Router,
};
Expand Down Expand Up @@ -175,6 +176,10 @@ fn organization_coaching_relationship_routes(app_state: AppState) -> Router {
"/organizations/:organization_id/coaching_relationships",
get(organization::coaching_relationship_controller::index),
)
.route_layer(from_fn_with_state(
app_state.clone(),
protect::coaching_relationships::index,
))
.route(
"/organizations/:organization_id/coaching_relationships/:relationship_id",
get(organization::coaching_relationship_controller::read),
Expand Down