Skip to content

Commit

Permalink
Add support for overriding messaging template in magic link, enchante…
Browse files Browse the repository at this point in the history
…d link, and OTP messages (#462)
  • Loading branch information
shilgapira authored Nov 25, 2024
1 parent cbeda2e commit 8dee0de
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions descope/authmethod/enchantedlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def sign_up_or_in(
login_options = LoginOptions(
custom_claims=signup_options.customClaims,
template_options=signup_options.templateOptions,
template_id=signup_options.templateId,
)

body = EnchantedLink._compose_signin_body(
Expand Down
1 change: 1 addition & 0 deletions descope/authmethod/magiclink.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def sign_up_or_in(
login_options = LoginOptions(
custom_claims=signup_options.customClaims,
template_options=signup_options.templateOptions,
template_id=signup_options.templateId,
)
body = MagicLink._compose_signin_body(
login_id,
Expand Down
1 change: 1 addition & 0 deletions descope/authmethod/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def sign_up_or_in(
login_options = LoginOptions(
custom_claims=signup_options.customClaims,
template_options=signup_options.templateOptions,
template_id=signup_options.templateId,
)
body = OTP._compose_signin_body(
login_id,
Expand Down
25 changes: 18 additions & 7 deletions descope/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Optional
from typing import Any, Optional

from descope.exceptions import ERROR_TYPE_INVALID_ARGUMENT, AuthException

Expand Down Expand Up @@ -111,11 +111,14 @@ def __init__(
self,
stepup: bool = False,
mfa: bool = False,
revoke_other_sessions: Optional[None] = None,
revoke_other_sessions: Optional[bool] = None,
custom_claims: Optional[dict] = None,
template_options: Optional[
dict
] = None, # for providing messaging template options (templates that are being sent via email / text message)
template_id: Optional[
str
] = None, # for overriding the default template (templates that are being sent via email / text message)
):
self.stepup = stepup
self.customClaims = custom_claims
Expand All @@ -124,6 +127,8 @@ def __init__(
self.revokeOtherSessions = revoke_other_sessions
if template_options is not None:
self.templateOptions = template_options
if template_id is not None:
self.templateId = template_id


class AccessKeyLoginOptions:
Expand Down Expand Up @@ -152,24 +157,30 @@ def validate_refresh_token_provided(
class SignUpOptions:
def __init__(
self,
revoke_other_sessions: Optional[None] = None,
revoke_other_sessions: Optional[bool] = None,
custom_claims: Optional[dict] = None,
template_options: Optional[
dict
] = None, # for providing messaging template options (templates that are being sent via email / text message)
template_id: Optional[
str
] = None, # for overriding the default template (templates that are being sent via email / text message)
):
self.revoke_other_sessions = revoke_other_sessions
self.revokeOtherSessions = revoke_other_sessions
self.customClaims = custom_claims
self.templateOptions = template_options
self.templateId = template_id


def signup_options_to_dict(signup_options: Optional[SignUpOptions] = None) -> dict:
res = {}
res: dict[str, Any] = {}
if signup_options is not None:
if signup_options.customClaims is not None:
res["customClaims"] = signup_options.customClaims
if signup_options.templateId is not None:
res["templateId"] = signup_options.templateId
if signup_options.templateOptions is not None:
res["templateOptions"] = signup_options.templateOptions
if signup_options.revoke_other_sessions is not None:
res["revokeOtherSessions"] = signup_options.revoke_other_sessions
if signup_options.revokeOtherSessions is not None:
res["revokeOtherSessions"] = signup_options.revokeOtherSessions
return res
7 changes: 6 additions & 1 deletion tests/test_enchantedlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def test_sign_in(self):
LoginOptions(
stepup=True,
template_options={"blue": "bla"},
template_id="foo",
revoke_other_sessions=True,
),
refresh_token=refresh_token,
Expand All @@ -183,6 +184,7 @@ def test_sign_in(self):
"stepup": True,
"customClaims": None,
"templateOptions": {"blue": "bla"},
"templateId": "foo",
"revokeOtherSessions": True,
"mfa": False,
},
Expand Down Expand Up @@ -304,7 +306,9 @@ def test_sign_up(self):
"http://test.me",
None,
SignUpOptions(
template_options={"bla": "blue"}, revoke_other_sessions=True
template_options={"bla": "blue"},
template_id="foo",
revoke_other_sessions=True,
),
)
mock_post.assert_called_with(
Expand All @@ -321,6 +325,7 @@ def test_sign_up(self):
"email": "[email protected]",
"loginOptions": {
"templateOptions": {"bla": "blue"},
"templateId": "foo",
"revokeOtherSessions": True,
},
},
Expand Down
17 changes: 12 additions & 5 deletions tests/test_magiclink.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_compose_body(self):
},
)

lo = LoginOptions(stepup=True, custom_claims={"k1": "v1"})
lo = LoginOptions(stepup=True, custom_claims={"k1": "v1"}, template_id="foo")
self.assertEqual(
MagicLink._compose_signin_body("id1", "uri1", lo),
{
Expand All @@ -70,6 +70,7 @@ def test_compose_body(self):
"stepup": True,
"mfa": False,
"customClaims": {"k1": "v1"},
"templateId": "foo",
},
},
)
Expand Down Expand Up @@ -193,7 +194,9 @@ def test_sign_in(self):
DeliveryMethod.EMAIL,
"[email protected]",
"http://test.me",
LoginOptions(stepup=True, template_options={"blue": "bla"}),
LoginOptions(
stepup=True, template_options={"blue": "bla"}, template_id=None
),
refresh_token=refresh_token,
)
mock_post.assert_called_with(
Expand Down Expand Up @@ -274,7 +277,7 @@ def test_sign_up(self):
"[email protected]",
"http://test.me",
signup_user_details,
SignUpOptions(template_options={"bla": "blue"}),
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
)
self.assertEqual("t***@example.com", resp)

Expand All @@ -294,7 +297,10 @@ def test_sign_up(self):
"email": "[email protected]",
},
"email": "[email protected]",
"loginOptions": {"templateOptions": {"bla": "blue"}},
"loginOptions": {
"templateOptions": {"bla": "blue"},
"templateId": "foo",
},
},
allow_redirects=False,
verify=True,
Expand Down Expand Up @@ -420,7 +426,7 @@ def test_sign_up_or_in(self):
DeliveryMethod.EMAIL,
"[email protected]",
"http://test.me",
SignUpOptions(template_options={"bla": "blue"}),
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
),
)
mock_post.assert_called_with(
Expand All @@ -437,6 +443,7 @@ def test_sign_up_or_in(self):
"customClaims": None,
"mfa": False,
"templateOptions": {"bla": "blue"},
"templateId": "foo",
},
},
allow_redirects=False,
Expand Down
15 changes: 11 additions & 4 deletions tests/test_otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def test_sign_up(self):
DeliveryMethod.EMAIL,
"[email protected]",
signup_user_details,
SignUpOptions(template_options={"bla": "blue"}),
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
),
)
mock_post.assert_called_with(
Expand All @@ -277,7 +277,10 @@ def test_sign_up(self):
"email": "[email protected]",
},
"email": "[email protected]",
"loginOptions": {"templateOptions": {"bla": "blue"}},
"loginOptions": {
"templateOptions": {"bla": "blue"},
"templateId": "foo",
},
},
allow_redirects=False,
verify=True,
Expand Down Expand Up @@ -386,7 +389,9 @@ def test_sign_in(self):
client.otp.sign_in(
DeliveryMethod.EMAIL,
"[email protected]",
LoginOptions(stepup=True, template_options={"blue": "bla"}),
LoginOptions(
stepup=True, template_options={"blue": "bla"}, template_id="foo"
),
refresh_token=refresh_token,
)
mock_post.assert_called_with(
Expand All @@ -402,6 +407,7 @@ def test_sign_in(self):
"stepup": True,
"customClaims": None,
"templateOptions": {"blue": "bla"},
"templateId": "foo",
"mfa": False,
},
},
Expand Down Expand Up @@ -449,7 +455,7 @@ def test_sign_up_or_in(self):
client.otp.sign_up_or_in(
DeliveryMethod.EMAIL,
"[email protected]",
SignUpOptions(template_options={"bla": "blue"}),
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
),
)
mock_post.assert_called_with(
Expand All @@ -465,6 +471,7 @@ def test_sign_up_or_in(self):
"customClaims": None,
"mfa": False,
"templateOptions": {"bla": "blue"},
"templateId": "foo",
},
},
allow_redirects=False,
Expand Down

0 comments on commit 8dee0de

Please sign in to comment.