From 021b8dfcb99928b363e4546f626e3deb5793e392 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 24 Nov 2022 13:42:37 +0300 Subject: auth: Implement `create_resource` function * gn3/auth/authentication/checks.py: new `authenticated_p` decorator to apply on any function that requires the user to be authenticated before it runs. * gn3/auth/authorisation/checks.py: use a `auth.authentication.users.User` object rather than a UUID object in the global `g`. * gn3/auth/authorisation/groups.py: Implement the `authenticated_user_group` function to get the group(s) in which the currently authenticated user belongs. * gn3/auth/authorisation/resources.py: Implement the `create_resource` function correctly. * tests/unit/auth/conftest.py: extract the User objects into a global variable for reusability with the tests. * tests/unit/auth/test_resources.py: Use global user objects from conftest in the tests. Set a User object (rather than UUID) in the global `g` variable. --- gn3/auth/authentication/checks.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 gn3/auth/authentication/checks.py (limited to 'gn3/auth/authentication') diff --git a/gn3/auth/authentication/checks.py b/gn3/auth/authentication/checks.py new file mode 100644 index 0000000..63b0752 --- /dev/null +++ b/gn3/auth/authentication/checks.py @@ -0,0 +1,14 @@ +"""Functions to check for user authentication.""" + +from flask import g + +from .exceptions import AuthenticationError + +def authenticated_p(func): + """Decorator for functions requiring authentication.""" + def __authenticated__(*args, **kwargs): + user = g.user if hasattr(g, "user") else False + if user: + return func(*args, **kwargs) + raise AuthenticationError("You need to be authenticated") + return __authenticated__ -- cgit v1.2.3