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

fix: auth route conflicts #1033

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
2 changes: 1 addition & 1 deletion robyn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def add_route(
injected_dependencies = self.dependencies.get_dependency_map(self)

if auth_required:
self.middleware_router.add_auth_middleware(endpoint)(handler)
self.middleware_router.add_auth_middleware(endpoint, route_type)(handler)

if isinstance(route_type, str):
http_methods = {
Expand Down
13 changes: 9 additions & 4 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,19 @@ def add_route(
self.route_middlewares.append(RouteMiddleware(middleware_type, endpoint, function))
return handler

def add_auth_middleware(self, endpoint: str):
def add_auth_middleware(self, endpoint: str, method: HttpMethod):
"""
This method adds an authentication middleware to the specified endpoint.
"""

Args:
endpoint (str): The endpoint path
method (HttpMethod): The HTTP method for this route
"""
injected_dependencies = {}

# Create a unique route key that includes both method and path
route_key = f"{method}:{endpoint}"

def decorator(handler):
@wraps(handler)
def inner_handler(request: Request, *args):
Expand All @@ -302,12 +308,11 @@ def inner_handler(request: Request, *args):
if identity is None:
return self.authentication_handler.unauthorized_response
request.identity = identity

return request

self.add_route(
MiddlewareType.BEFORE_REQUEST,
endpoint,
route_key,
inner_handler,
injected_dependencies,
)
Expand Down
10 changes: 3 additions & 7 deletions src/routers/middleware_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ impl Router<(FunctionInfo, HashMap<String, String>), MiddlewareType> for Middlew
) -> Option<(FunctionInfo, HashMap<String, String>)> {
let table = self.routes.get(route_method)?;

let table_lock = table.read().ok()?;
let res = table_lock.at(route).ok()?;
let mut route_params = HashMap::new();
for (key, value) in res.params.iter() {
route_params.insert(key.to_string(), value.to_string());
match table.read().at(route) {
Ok(m) => Some((m.value.clone(), m.params.into_iter().collect())),
Err(_) => None,
}

Some((res.value.to_owned(), route_params))
}
}

Expand Down
Loading