Skip to content

Commit

Permalink
fix(auth): properly handle async Flask routes in decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Aug 29, 2024
1 parent 6690ae7 commit dac7e56
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
6 changes: 3 additions & 3 deletions bento_lib/auth/middleware/flask.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from flask import Flask, Request, Response, request, g
from flask import Flask, Request, Response, current_app, g, request
from functools import wraps

from bento_lib.auth.exceptions import BentoAuthException
Expand Down Expand Up @@ -91,14 +91,14 @@ def wrapper(*args, **kwargs):
self.check_authz_evaluate(request, permissions, resource, require_token, set_authz_flag)
except BentoAuthException as e:
return self._make_auth_error(e)
return func(*args, **kwargs)
return current_app.ensure_sync(func)(*args, **kwargs)
return wrapper
return decorator

def deco_public_endpoint(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
res = current_app.ensure_sync(func)(*args, **kwargs)
if self.enabled:
self.mark_authz_done(request)
return res
Expand Down
93 changes: 47 additions & 46 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "bento-lib"
version = "12.1.0"
version = "12.1.1"
description = "A set of common utilities and helpers for Bento platform services."
authors = [
"David Lougheed <[email protected]>",
Expand Down
7 changes: 5 additions & 2 deletions tests/test_platform_flask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bento_lib.responses.flask_errors as fe

import asyncio
import logging
import pytest
import responses
Expand Down Expand Up @@ -80,12 +81,14 @@ def auth_post_exempted():

@test_app_auth.route("/post-public", methods=["POST"])
@auth_middleware.deco_public_endpoint
def auth_post_public():
async def auth_post_public(): # ensure Flask public decorator works for async code too
await asyncio.sleep(0.1)
return jsonify(request.json)

@test_app_auth.route("/post-private", methods=["POST"])
@auth_middleware.deco_require_permissions_on_resource(frozenset({P_INGEST_DATA}))
def auth_post_private():
async def auth_post_private(): # ensure Flask private decorator works for async code too
await asyncio.sleep(0.1)
return jsonify(request.json)

@test_app_auth.route("/post-private-no-flag", methods=["POST"])
Expand Down

0 comments on commit dac7e56

Please sign in to comment.