From 51a9736cfee9bb2fb906543eb48cb1f96916bcbb Mon Sep 17 00:00:00 2001 From: Itai Levi Date: Fri, 14 Jun 2024 10:48:12 -0400 Subject: [PATCH] feat(user): add `logout_all_user_sessions` api request --- propelauth_py/__init__.py | 14 ++++++++++++-- propelauth_py/api/user.py | 17 +++++++++++++++++ tests/test_init_base_auth.py | 2 ++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/propelauth_py/__init__.py b/propelauth_py/__init__.py index 611e9a5..34e031b 100644 --- a/propelauth_py/__init__.py +++ b/propelauth_py/__init__.py @@ -12,6 +12,7 @@ _fetch_users_by_query, _fetch_users_in_org, _create_user, + _logout_all_user_sessions, _update_user_email, _update_user_metadata, _delete_user, @@ -137,6 +138,7 @@ "disable_user_2fa", "enable_user_can_create_orgs", "disable_user_can_create_orgs", + "logout_all_user_sessions", "allow_org_to_setup_saml_connection", "disallow_org_to_setup_saml_connection", "fetch_api_key", @@ -212,7 +214,7 @@ def fetch_org_by_query( order_by, name, ) - + def fetch_custom_role_mappings(): return _fetch_custom_role_mappings( auth_url, @@ -291,6 +293,13 @@ def resend_email_confirmation(user_id): user_id, ) + def logout_all_user_sessions(user_id): + return _logout_all_user_sessions( + auth_url, + integration_api_key, + user_id, + ) + def update_user_email(user_id, new_email, require_email_confirmation): return _update_user_email( auth_url, @@ -429,7 +438,7 @@ def update_org_metadata( members_must_have_email_domain_match=members_must_have_email_domain_match, domain=domain, ) - + def subscribe_org_to_role_mapping(org_id, custom_role_mapping_name): return _subscribe_org_to_role_mapping( auth_url, @@ -630,6 +639,7 @@ def validate_api_key(api_key_token): disable_user_2fa=disable_user_2fa, enable_user_can_create_orgs=enable_user_can_create_orgs, disable_user_can_create_orgs=disable_user_can_create_orgs, + logout_all_user_sessions=logout_all_user_sessions, allow_org_to_setup_saml_connection=allow_org_to_setup_saml_connection, disallow_org_to_setup_saml_connection=disallow_org_to_setup_saml_connection, # api key functions diff --git a/propelauth_py/api/user.py b/propelauth_py/api/user.py index b396437..4717ffe 100644 --- a/propelauth_py/api/user.py +++ b/propelauth_py/api/user.py @@ -369,6 +369,23 @@ def _resend_email_confirmation(auth_url, integration_api_key, user_id): return True +def _logout_all_user_sessions(auth_url, integration_api_key, user_id): + if not _is_valid_id(user_id): + return False + + url = auth_url + f"{ENDPOINT_PATH}/{user_id}/logout_all_sessions" + response = requests.post(url, auth=_ApiKeyAuth(integration_api_key)) + + if response.status_code == 401: + raise ValueError("integration_api_key is incorrect") + elif response.status_code == 404: + return False + elif not response.ok: + raise RuntimeError("Unknown error when logging out all user sessions") + + return True + + #################### # PATCH/PUT # #################### diff --git a/tests/test_init_base_auth.py b/tests/test_init_base_auth.py index 3a032d4..65bc516 100644 --- a/tests/test_init_base_auth.py +++ b/tests/test_init_base_auth.py @@ -24,6 +24,7 @@ _validate_personal_api_key, _invite_user_to_org, _resend_email_confirmation, + _logout_all_user_sessions, ) from propelauth_py.api.org import ( _fetch_custom_role_mappings, @@ -102,6 +103,7 @@ _delete_org, _invite_user_to_org, _resend_email_confirmation, + _logout_all_user_sessions, ]