Skip to content

Commit

Permalink
Add an option to disable auth api routes (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbelletti committed Oct 2, 2023
1 parent 76247ae commit 231e839
Showing 1 changed file with 29 additions and 27 deletions.
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

0 comments on commit 231e839

Please sign in to comment.