Skip to content

fix: auth route conflict #987

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -110,7 +110,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
8 changes: 4 additions & 4 deletions robyn/processpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ def spawn_process(
server.apply_response_headers(response_headers)

for route in routes:
route_type, endpoint, function, is_const = route
server.add_route(route_type, endpoint, function, is_const)
middleware_type, endpoint, function, is_const = route
server.add_route(middleware_type, endpoint, function, is_const)

for middleware_type, middleware_function in global_middlewares:
server.add_global_middleware(middleware_type, middleware_function)

for route_type, endpoint, function in route_middlewares:
server.add_middleware_route(route_type, endpoint, function)
for middleware_type, endpoint, route_type, function in route_middlewares:
server.add_middleware_route(middleware_type, endpoint, function, route_type)

if Events.STARTUP in event_handlers:
server.add_startup_handler(event_handlers[Events.STARTUP])
Expand Down
1 change: 1 addition & 0 deletions robyn/robyn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ class Server:
middleware_type: MiddlewareType,
route: str,
function: FunctionInfo,
route_type: HttpMethod,
) -> None:
pass
def add_startup_handler(self, function: FunctionInfo) -> None:
Expand Down
10 changes: 7 additions & 3 deletions robyn/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Route(NamedTuple):
class RouteMiddleware(NamedTuple):
middleware_type: MiddlewareType
route: str
route_type: HttpMethod
function: FunctionInfo


Expand Down Expand Up @@ -263,6 +264,7 @@ def add_route(
self,
middleware_type: MiddlewareType,
endpoint: str,
route_type: Optional[HttpMethod],
handler: Callable,
injected_dependencies: dict,
) -> Callable:
Expand All @@ -283,10 +285,10 @@ def add_route(
params,
new_injected_dependencies,
)
self.route_middlewares.append(RouteMiddleware(middleware_type, endpoint, function))
self.route_middlewares.append(RouteMiddleware(middleware_type, endpoint, route_type, function))
return handler

def add_auth_middleware(self, endpoint: str):
def add_auth_middleware(self, endpoint: str, route_type: HttpMethod):
"""
This method adds an authentication middleware to the specified endpoint.
"""
Expand All @@ -308,6 +310,7 @@ def inner_handler(request: Request, *args):
self.add_route(
MiddlewareType.BEFORE_REQUEST,
endpoint,
route_type,
inner_handler,
injected_dependencies,
)
Expand Down Expand Up @@ -336,11 +339,12 @@ def inner_handler(*args, **kwargs):
self.add_route(
middleware_type,
endpoint,
None,
async_inner_handler,
injected_dependencies,
)
else:
self.add_route(middleware_type, endpoint, inner_handler, injected_dependencies)
self.add_route(middleware_type, endpoint, None, inner_handler, injected_dependencies)
else:
params = dict(inspect.signature(handler).parameters)

Expand Down
25 changes: 22 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,29 @@ impl Server {
middleware_type: &MiddlewareType,
route: &str,
function: FunctionInfo,
http_method: Option<HttpMethod>,
) {

let mut endpoint = route.to_string();

if let Some(method) = http_method {

endpoint = method.to_string().to_owned();

if !route.starts_with('/') {
endpoint.push('/');
}

endpoint.push_str(route);
}

debug!(
"MiddleWare Route added for {:?} {} ",
middleware_type, route
middleware_type, &endpoint
);

self.middleware_router
.add_route(middleware_type, route, function, None)
.add_route(middleware_type, &endpoint, function, None)
.unwrap();
}

Expand Down Expand Up @@ -420,13 +436,16 @@ async fn index(
) -> impl Responder {
let mut request = Request::from_actix_request(&req, payload, &global_request_headers).await;

let mut route = req.method().as_str().to_owned();
route.push_str(req.uri().path());

// Before middleware
// Global
let mut before_middlewares =
middleware_router.get_global_middlewares(&MiddlewareType::BeforeRequest);
// Route specific
if let Some((function, route_params)) =
middleware_router.get_route(&MiddlewareType::BeforeRequest, req.uri().path())
middleware_router.get_route(&MiddlewareType::BeforeRequest, &route)
{
before_middlewares.push(function);
request.path_params = route_params;
Expand Down
7 changes: 7 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ impl HttpMethod {
}
}

// for: https://stackoverflow.com/a/32712140/9652621
impl std::fmt::Display for HttpMethod {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[pyclass]
#[derive(Default, Debug, Clone)]
pub struct Url {
Expand Down
Loading