1
1
import 'package:dart_frog/dart_frog.dart' ;
2
2
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/authentication_middleware.dart' ;
3
3
import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/authorization_middleware.dart' ;
4
+ import 'package:flutter_news_app_api_server_full_source_code/src/middlewares/configured_rate_limiter.dart' ;
4
5
import 'package:flutter_news_app_api_server_full_source_code/src/rbac/permissions.dart' ;
5
6
6
7
/// Middleware for the `/api/v1/users` route group.
7
8
///
8
9
/// This middleware performs the following actions:
9
10
/// 1. `requireAuthentication()` : Ensures a user is authenticated for all
10
11
/// /users/* routes.
11
- /// 2. `permissionSetter` : A middleware that provides the correct permission string
12
- /// into the context *only* for the `/users` and `/users/{id}` endpoints.
13
- /// It ignores sub-routes like `/users/{id}/settings`, leaving them to be
14
- /// handled by their own more specific middleware.
12
+ /// 2. `rateAndPermissionSetter` : A middleware that applies rate limiting and
13
+ /// provides the correct permission string into the context *only* for the
14
+ /// `/users` and `/users/{id}` endpoints. It ignores sub-routes like
15
+ /// `/users/{id}/settings`, leaving them to be handled by their own more
16
+ /// specific middleware.
15
17
/// 3. `authorizationMiddleware()` : Checks if the authenticated user has the
16
- /// permission provided by the `permissionSetter `.
18
+ /// permission provided by the `rateAndPermissionSetter `.
17
19
Handler middleware (Handler handler) {
18
- // This middleware provides the required permission string into the context .
20
+ // This middleware applies rate limiting and provides the required permission .
19
21
// It is scoped to only handle `/users` and `/users/{id}`.
20
22
// ignore: prefer_function_declarations_over_variables
21
- final permissionSetter = (Handler handler) {
23
+ final rateAndPermissionSetter = (Handler handler) {
22
24
return (RequestContext context) {
23
25
final request = context.request;
24
26
final pathSegments = request.uri.pathSegments;
@@ -31,36 +33,43 @@ Handler middleware(Handler handler) {
31
33
}
32
34
33
35
final String permission;
36
+ final Middleware rateLimiter;
34
37
final isItemRequest = pathSegments.length == 4 ;
35
38
36
39
switch (request.method) {
37
40
case HttpMethod .get :
38
41
// Admins can list all users; users can read their own profile.
39
42
permission =
40
43
isItemRequest ? Permissions .userReadOwned : Permissions .userRead;
44
+ rateLimiter = createReadRateLimiter ();
41
45
case HttpMethod .put:
42
46
// Users can update their own profile.
43
47
permission = Permissions .userUpdateOwned;
48
+ rateLimiter = createWriteRateLimiter ();
44
49
case HttpMethod .delete:
45
50
// Users can delete their own profile.
46
51
permission = Permissions .userDeleteOwned;
52
+ rateLimiter = createWriteRateLimiter ();
47
53
default :
48
54
// Disallow any other methods (e.g., POST) on this route group.
49
55
// User creation is handled by the /auth routes.
50
56
return Response (statusCode: 405 );
51
57
}
52
- // Provide the required permission to the authorization middleware.
53
- return handler (
54
- context.provide <String >(() => permission),
55
- );
58
+
59
+ // Apply the selected rate limiter and then provide the permission.
60
+ return rateLimiter (
61
+ (context) => handler (
62
+ context.provide <String >(() => permission),
63
+ ),
64
+ )(context);
56
65
};
57
66
};
58
67
59
68
return handler
60
69
// The authorization middleware runs after the permission has been set.
61
70
.use (authorizationMiddleware ())
62
- // The permission setter runs after authentication is confirmed .
63
- .use (permissionSetter )
71
+ // The rate limiter and permission setter runs after authentication.
72
+ .use (rateAndPermissionSetter )
64
73
// Authentication is the first check for all /users/* routes.
65
74
.use (requireAuthentication ());
66
75
}
0 commit comments