Skip to content

Commit ea709e9

Browse files
committed
feat(auth): apply ownership check middleware to item routes
- Creates a new `_middleware.dart` file for the `/[id]` path. - Applies the `ownershipCheckMiddleware` to all item-specific requests (GET, PUT, DELETE). - This ensures ownership is verified after authentication and authorization checks have passed but before the route handler is executed.
1 parent 49d1e91 commit ea709e9

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'package:dart_frog/dart_frog.dart';
2+
import 'package:ht_api/src/middlewares/ownership_check_middleware.dart';
3+
4+
/// Middleware specific to the item-level `/api/v1/data/[id]` route path.
5+
///
6+
/// This middleware applies the [ownershipCheckMiddleware] to perform an
7+
/// ownership check on the requested item *after* the parent middleware
8+
/// (`/api/v1/data/_middleware.dart`) has already performed authentication and
9+
/// authorization checks.
10+
///
11+
/// This ensures that only authorized users can proceed, and then this
12+
/// middleware adds the final layer of security by verifying item ownership
13+
/// for non-admin users when required by the model's configuration.
14+
Handler middleware(Handler handler) {
15+
// The `ownershipCheckMiddleware` will run after the middleware from
16+
// `/api/v1/data/_middleware.dart` (authn, authz, model validation).
17+
return handler.use(ownershipCheckMiddleware());
18+
}
19+

routes/api/v1/data/[id].dart renamed to routes/api/v1/data/[id]/index.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import 'package:ht_api/src/services/user_preference_limit_service.dart'; // Impo
88
import 'package:ht_data_repository/ht_data_repository.dart';
99
import 'package:ht_shared/ht_shared.dart';
1010

11-
import '../../../_middleware.dart'; // Assuming RequestId is here
11+
import '../../../../_middleware.dart'; // Assuming RequestId is here
1212

1313
/// Handles requests for the /api/v1/data/[id] endpoint.
1414
/// Dispatches requests to specific handlers based on the HTTP method.

0 commit comments

Comments
 (0)