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 an option to disable auth api routes #809

Merged
merged 1 commit into from
Oct 2, 2023
Merged
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
56 changes: 29 additions & 27 deletions py4web/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def enable_record_versioning(
current_record_label=current_record_label,
)

def enable(self, route="auth", uses=(), env=None, spa=False):
def enable(self, route="auth", uses=(), env=None, spa=False, allow_api_routes=True):
"""Enables Auth, aka generates login/logout/register/etc API pages"""
self.route = route = route.rstrip("/")
env = env or {}
Expand All @@ -944,35 +944,37 @@ def enable(self, route="auth", uses=(), env=None, spa=False):

# This exposes all API actions as /{app_name}/{route}/api/{name}
# and API Models as /{app_name}/{route}/api/{name}?@model=true
exposed_api_routes = []
if allow_api_routes:

# Exposed Public APIs
exposed_api_routes = [
dict(api_name=api_name, api_route=f"{route}/api/{api_name}", uses=auth)
for api_name in AuthAPI.public_api
if self.allows(api_name)
]

# Exposed Private APIs
exposed_api_routes.extend(
[
dict(
api_name=api_name,
api_route=f"{route}/api/{api_name}",
uses=auth.user,
)
for api_name in AuthAPI.private_api
# Exposed Public APIs
exposed_api_routes = [
dict(api_name=api_name, api_route=f"{route}/api/{api_name}", uses=auth)
for api_name in AuthAPI.public_api
if self.allows(api_name)
]
)

for item in exposed_api_routes:
api_factory = getattr(AuthAPI, item["api_name"])

@action(item["api_route"], method=methods)
@action.uses(item["uses"], *uses)
def _(auth=auth, api_factory=api_factory):
return api_factory(auth)


# Exposed Private APIs
exposed_api_routes.extend(
[
dict(
api_name=api_name,
api_route=f"{route}/api/{api_name}",
uses=auth.user,
)
for api_name in AuthAPI.private_api
if self.allows(api_name)
]
)

for item in exposed_api_routes:
api_factory = getattr(AuthAPI, item["api_name"])

@action(item["api_route"], method=methods)
@action.uses(item["uses"], *uses)
def _(auth=auth, api_factory=api_factory):
return api_factory(auth)

# This exposes all plugins as /{app_name}/{route}/plugins/{path}
for name in self.plugins:

Expand Down