From a1f69cce59e267b2ad92f7f11333b8df8ab9278d Mon Sep 17 00:00:00 2001 From: Itai Levi Date: Fri, 29 Sep 2023 15:45:32 -0400 Subject: [PATCH] [TIC-339] Add missing endpoints (#10) * feat(__init__): add missing endpoints * Bump version number --- .gitignore | 1 + propelauth_fastapi/__init__.py | 149 ++++++++++++++++++++++----------- setup.py | 8 +- tests/__init__.py | 0 4 files changed, 105 insertions(+), 53 deletions(-) create mode 100644 tests/__init__.py diff --git a/.gitignore b/.gitignore index dadb445..142def7 100644 --- a/.gitignore +++ b/.gitignore @@ -350,6 +350,7 @@ tags # http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ ### VisualStudioCode ### +.vscode .vscode/* !.vscode/settings.json !.vscode/tasks.json diff --git a/propelauth_fastapi/__init__.py b/propelauth_fastapi/__init__.py index b17f701..77ced9e 100644 --- a/propelauth_fastapi/__init__.py +++ b/propelauth_fastapi/__init__.py @@ -21,7 +21,9 @@ def __call__(self, credentials: HTTPAuthorizationCredentials = Depends(_security if credentials is None: authorization_header = "" else: - authorization_header = credentials.scheme + " " + credentials.credentials + authorization_header = ( + credentials.scheme + " " + credentials.credentials + ) user = self.auth.validate_access_token_and_get_user(authorization_header) return user @@ -59,9 +61,13 @@ def require_org_member(user: User, required_org_id: str): def _require_org_member_with_minimum_role_wrapper(auth: Auth, debug_mode: bool): - def require_org_member_with_minimum_role(user: User, required_org_id: str, minimum_required_role: str): + def require_org_member_with_minimum_role( + user: User, required_org_id: str, minimum_required_role: str + ): try: - return auth.validate_minimum_org_role_and_get_org(user, required_org_id, minimum_required_role) + return auth.validate_minimum_org_role_and_get_org( + user, required_org_id, minimum_required_role + ) except ForbiddenException as e: _handle_forbidden_exception(e, debug_mode) @@ -79,9 +85,13 @@ def require_org_member_with_exact_role(user: User, required_org_id: str, role: s def _require_org_member_with_permission_wrapper(auth: Auth, debug_mode: bool): - def require_org_member_with_permission(user: User, required_org_id: str, permission: str): + def require_org_member_with_permission( + user: User, required_org_id: str, permission: str + ): try: - return auth.validate_permission_and_get_org(user, required_org_id, permission) + return auth.validate_permission_and_get_org( + user, required_org_id, permission + ) except ForbiddenException as e: _handle_forbidden_exception(e, debug_mode) @@ -89,9 +99,13 @@ def require_org_member_with_permission(user: User, required_org_id: str, permiss def _require_org_member_with_all_permissions_wrapper(auth: Auth, debug_mode: bool): - def require_org_member_with_all_permissions(user: User, required_org_id: str, permissions: List[str]): + def require_org_member_with_all_permissions( + user: User, required_org_id: str, permissions: List[str] + ): try: - return auth.validate_all_permissions_and_get_org(user, required_org_id, permissions) + return auth.validate_all_permissions_and_get_org( + user, required_org_id, permissions + ) except ForbiddenException as e: _handle_forbidden_exception(e, debug_mode) @@ -105,44 +119,68 @@ def _handle_forbidden_exception(e: ForbiddenException, debug_mode: bool): raise HTTPException(status_code=403) - -Auth = namedtuple("Auth", [ - "require_user", "optional_user", - "require_org_member", - "require_org_member_with_minimum_role", - "require_org_member_with_exact_role", - "require_org_member_with_permission", - "require_org_member_with_all_permissions", - "fetch_user_metadata_by_user_id", "fetch_user_metadata_by_email", "fetch_user_metadata_by_username", - "fetch_batch_user_metadata_by_user_ids", - "fetch_batch_user_metadata_by_emails", - "fetch_batch_user_metadata_by_usernames", - "fetch_org", "fetch_org_by_query", "fetch_users_by_query", "fetch_users_in_org", - "create_user", - "update_user_email", - "update_user_metadata", - "update_user_password", - "create_magic_link", "create_access_token", - "migrate_user_from_external_source", "create_org", "add_user_to_org", - "update_org_metadata", - "delete_user", "disable_user", "enable_user", "disable_user_2fa", - "enable_user_can_create_orgs", "disable_user_can_create_orgs", - "allow_org_to_setup_saml_connection", "disallow_org_to_setup_saml_connection", - - "fetch_api_key", - "fetch_current_api_keys", - "fetch_archived_api_keys", - "create_api_key", - "update_api_key", - "delete_api_key", - "validate_api_key", - "validate_personal_api_key", - "validate_org_api_key", -]) - - -def init_auth(auth_url: str, api_key: str, token_verification_metadata: TokenVerificationMetadata = None, - debug_mode=False): +Auth = namedtuple( + "Auth", + [ + "require_user", + "optional_user", + "require_org_member", + "require_org_member_with_minimum_role", + "require_org_member_with_exact_role", + "require_org_member_with_permission", + "require_org_member_with_all_permissions", + "fetch_user_metadata_by_user_id", + "fetch_user_metadata_by_email", + "fetch_user_metadata_by_username", + "fetch_batch_user_metadata_by_user_ids", + "fetch_batch_user_metadata_by_emails", + "fetch_batch_user_metadata_by_usernames", + "fetch_org", + "fetch_org_by_query", + "fetch_users_by_query", + "fetch_users_in_org", + "create_user", + "update_user_email", + "update_user_metadata", + "update_user_password", + "create_magic_link", + "create_access_token", + "migrate_user_from_external_source", + "create_org", + "add_user_to_org", + "update_org_metadata", + "delete_user", + "disable_user", + "enable_user", + "disable_user_2fa", + "enable_user_can_create_orgs", + "disable_user_can_create_orgs", + "allow_org_to_setup_saml_connection", + "disallow_org_to_setup_saml_connection", + "fetch_api_key", + "fetch_current_api_keys", + "fetch_archived_api_keys", + "create_api_key", + "update_api_key", + "delete_api_key", + "validate_api_key", + "validate_personal_api_key", + "validate_org_api_key", + "change_user_role_in_org", + "clear_user_password", + "delete_org", + "invite_user_to_org", + "remove_user_from_org", + ], +) + + +def init_auth( + auth_url: str, + api_key: str, + token_verification_metadata: TokenVerificationMetadata = None, + debug_mode=False, +): """Fetches metadata required to validate access tokens and returns auth decorators and utilities""" auth = init_base_auth(auth_url, api_key, token_verification_metadata) @@ -150,10 +188,18 @@ def init_auth(auth_url: str, api_key: str, token_verification_metadata: TokenVer require_user=RequiredUserDependency(auth, debug_mode), optional_user=OptionalUserDependency(auth), require_org_member=_require_org_member_wrapper(auth, debug_mode), - require_org_member_with_minimum_role=_require_org_member_with_minimum_role_wrapper(auth, debug_mode), - require_org_member_with_exact_role=_require_org_member_with_exact_role_wrapper(auth, debug_mode), - require_org_member_with_permission=_require_org_member_with_permission_wrapper(auth, debug_mode), - require_org_member_with_all_permissions=_require_org_member_with_all_permissions_wrapper(auth, debug_mode), + require_org_member_with_minimum_role=_require_org_member_with_minimum_role_wrapper( + auth, debug_mode + ), + require_org_member_with_exact_role=_require_org_member_with_exact_role_wrapper( + auth, debug_mode + ), + require_org_member_with_permission=_require_org_member_with_permission_wrapper( + auth, debug_mode + ), + require_org_member_with_all_permissions=_require_org_member_with_all_permissions_wrapper( + auth, debug_mode + ), fetch_user_metadata_by_user_id=auth.fetch_user_metadata_by_user_id, fetch_user_metadata_by_email=auth.fetch_user_metadata_by_email, fetch_user_metadata_by_username=auth.fetch_user_metadata_by_username, @@ -191,4 +237,9 @@ def init_auth(auth_url: str, api_key: str, token_verification_metadata: TokenVer validate_api_key=auth.validate_api_key, validate_personal_api_key=auth.validate_personal_api_key, validate_org_api_key=auth.validate_org_api_key, + change_user_role_in_org=auth.change_user_role_in_org, + clear_user_password=auth.clear_user_password, + delete_org=auth.delete_org, + invite_user_to_org=auth.invite_user_to_org, + remove_user_from_org=auth.remove_user_from_org, ) diff --git a/setup.py b/setup.py index 1a6cf40..dded9b1 100644 --- a/setup.py +++ b/setup.py @@ -6,12 +6,12 @@ README = (HERE / "README.md").read_text() # See https://pytest-runner.readthedocs.io/en/latest/#conditional-requirement -needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv) -pytest_runner = ['pytest-runner'] if needs_pytest else [] +needs_pytest = {"pytest", "test", "ptr"}.intersection(sys.argv) +pytest_runner = ["pytest-runner"] if needs_pytest else [] setup( name="propelauth-fastapi", - version="2.1.4", + version="2.1.5", description="A FastAPI library for managing authentication, backed by PropelAuth", long_description=README, long_description_content_type="text/markdown", @@ -20,7 +20,7 @@ author="PropelAuth", author_email="support@propelauth.com", license="MIT", - install_requires=["propelauth-py==3.1.4", "requests"], + install_requires=["propelauth-py==3.1.6", "requests"], setup_requires=pytest_runner, tests_require=["pytest==4.4.1"], test_suite="tests", diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29