Skip to content

Commit

Permalink
[TIC-878] Resend email confirmation (#51)
Browse files Browse the repository at this point in the history
* feat(user): add new route to resend email confirmation per user id

* feat(resend_email_confirmation): add specific error for 429

* bump version

* feat: add 400 error messages

* fix: raise runtime error if no user facing error
  • Loading branch information
Itai Levi authored Jun 10, 2024
1 parent af6d85a commit ce4c947
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
14 changes: 13 additions & 1 deletion propelauth_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_disable_user_can_create_orgs,
_validate_personal_api_key,
_invite_user_to_org,
_resend_email_confirmation,
)
from propelauth_py.api.org import (
_fetch_custom_role_mappings,
Expand Down Expand Up @@ -115,6 +116,7 @@
"fetch_users_in_org",
"create_user",
"invite_user_to_org",
"resend_email_confirmation",
"update_user_email",
"update_user_metadata",
"update_user_password",
Expand Down Expand Up @@ -282,6 +284,13 @@ def invite_user_to_org(email, org_id, role, additional_roles=[]):
additional_roles,
)

def resend_email_confirmation(user_id):
return _resend_email_confirmation(
auth_url,
integration_api_key,
user_id,
)

def update_user_email(user_id, new_email, require_email_confirmation):
return _update_user_email(
auth_url,
Expand Down Expand Up @@ -433,7 +442,9 @@ def delete_org(org_id):
return _delete_org(auth_url, integration_api_key, org_id)

def add_user_to_org(user_id, org_id, role, additional_roles=[]):
return _add_user_to_org(auth_url, integration_api_key, user_id, org_id, role, additional_roles)
return _add_user_to_org(
auth_url, integration_api_key, user_id, org_id, role, additional_roles
)

def remove_user_from_org(user_id, org_id):
return _remove_user_from_org(auth_url, integration_api_key, user_id, org_id)
Expand Down Expand Up @@ -598,6 +609,7 @@ def validate_api_key(api_key_token):
fetch_users_in_org=fetch_users_in_org,
create_user=create_user,
invite_user_to_org=invite_user_to_org,
resend_email_confirmation=resend_email_confirmation,
update_user_email=update_user_email,
update_user_metadata=update_user_metadata,
update_user_password=update_user_password,
Expand Down
39 changes: 35 additions & 4 deletions propelauth_py/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ def _disable_user_2fa(auth_url, integration_api_key, user_id):
return True


def _invite_user_to_org(auth_url, integration_api_key, email, org_id, role, additional_roles=[]):
def _invite_user_to_org(
auth_url, integration_api_key, email, org_id, role, additional_roles=[]
):
if not _is_valid_id(org_id):
return False

Expand Down Expand Up @@ -338,6 +340,35 @@ def _invite_user_to_org(auth_url, integration_api_key, email, org_id, role, addi

return response.text


def _resend_email_confirmation(auth_url, integration_api_key, user_id):
if not _is_valid_id(user_id):
return False

endpoint_path = "/api/backend/v1/resend_email_confirmation"
url = auth_url + endpoint_path
json = {
"user_id": user_id,
}
response = requests.post(url, json=json, 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 response.status_code == 429:
raise RuntimeError("Too many requests, please try again later.")
elif response.status_code == 400:
if response.json().get("user_facing_error"):
raise ValueError(response.json().get("user_facing_error"))
else:
raise RuntimeError("Unknown error when resending email confirmation")
elif not response.ok:
raise RuntimeError("Unknown error when resending email confirmation")

return True


####################
# PATCH/PUT #
####################
Expand Down Expand Up @@ -399,9 +430,9 @@ def _update_user_password(
url = auth_url + f"{ENDPOINT_PATH}/{user_id}/password"
json = {"password": password}
if ask_user_to_update_password_on_login is not None:
json[
"ask_user_to_update_password_on_login"
] = ask_user_to_update_password_on_login
json["ask_user_to_update_password_on_login"] = (
ask_user_to_update_password_on_login
)

response = requests.put(url, json=json, auth=_ApiKeyAuth(integration_api_key))
if response.status_code == 401:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="propelauth-py",
version="3.1.14",
version="3.1.15",
description="A python authentication library",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
2 changes: 2 additions & 0 deletions tests/test_init_base_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_disable_user_can_create_orgs,
_validate_personal_api_key,
_invite_user_to_org,
_resend_email_confirmation,
)
from propelauth_py.api.org import (
_fetch_custom_role_mappings,
Expand Down Expand Up @@ -100,6 +101,7 @@
_change_user_role_in_org,
_delete_org,
_invite_user_to_org,
_resend_email_confirmation,
]


Expand Down

0 comments on commit ce4c947

Please sign in to comment.