Skip to content

Commit 8dee0de

Browse files
authored
Add support for overriding messaging template in magic link, enchanted link, and OTP messages (#462)
1 parent cbeda2e commit 8dee0de

File tree

7 files changed

+50
-17
lines changed

7 files changed

+50
-17
lines changed

descope/authmethod/enchantedlink.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def sign_up_or_in(
7171
login_options = LoginOptions(
7272
custom_claims=signup_options.customClaims,
7373
template_options=signup_options.templateOptions,
74+
template_id=signup_options.templateId,
7475
)
7576

7677
body = EnchantedLink._compose_signin_body(

descope/authmethod/magiclink.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ def sign_up_or_in(
7777
login_options = LoginOptions(
7878
custom_claims=signup_options.customClaims,
7979
template_options=signup_options.templateOptions,
80+
template_id=signup_options.templateId,
8081
)
8182
body = MagicLink._compose_signin_body(
8283
login_id,

descope/authmethod/otp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def sign_up_or_in(
118118
login_options = LoginOptions(
119119
custom_claims=signup_options.customClaims,
120120
template_options=signup_options.templateOptions,
121+
template_id=signup_options.templateId,
121122
)
122123
body = OTP._compose_signin_body(
123124
login_id,

descope/common.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import Optional
2+
from typing import Any, Optional
33

44
from descope.exceptions import ERROR_TYPE_INVALID_ARGUMENT, AuthException
55

@@ -111,11 +111,14 @@ def __init__(
111111
self,
112112
stepup: bool = False,
113113
mfa: bool = False,
114-
revoke_other_sessions: Optional[None] = None,
114+
revoke_other_sessions: Optional[bool] = None,
115115
custom_claims: Optional[dict] = None,
116116
template_options: Optional[
117117
dict
118118
] = None, # for providing messaging template options (templates that are being sent via email / text message)
119+
template_id: Optional[
120+
str
121+
] = None, # for overriding the default template (templates that are being sent via email / text message)
119122
):
120123
self.stepup = stepup
121124
self.customClaims = custom_claims
@@ -124,6 +127,8 @@ def __init__(
124127
self.revokeOtherSessions = revoke_other_sessions
125128
if template_options is not None:
126129
self.templateOptions = template_options
130+
if template_id is not None:
131+
self.templateId = template_id
127132

128133

129134
class AccessKeyLoginOptions:
@@ -152,24 +157,30 @@ def validate_refresh_token_provided(
152157
class SignUpOptions:
153158
def __init__(
154159
self,
155-
revoke_other_sessions: Optional[None] = None,
160+
revoke_other_sessions: Optional[bool] = None,
156161
custom_claims: Optional[dict] = None,
157162
template_options: Optional[
158163
dict
159164
] = None, # for providing messaging template options (templates that are being sent via email / text message)
165+
template_id: Optional[
166+
str
167+
] = None, # for overriding the default template (templates that are being sent via email / text message)
160168
):
161-
self.revoke_other_sessions = revoke_other_sessions
169+
self.revokeOtherSessions = revoke_other_sessions
162170
self.customClaims = custom_claims
163171
self.templateOptions = template_options
172+
self.templateId = template_id
164173

165174

166175
def signup_options_to_dict(signup_options: Optional[SignUpOptions] = None) -> dict:
167-
res = {}
176+
res: dict[str, Any] = {}
168177
if signup_options is not None:
169178
if signup_options.customClaims is not None:
170179
res["customClaims"] = signup_options.customClaims
180+
if signup_options.templateId is not None:
181+
res["templateId"] = signup_options.templateId
171182
if signup_options.templateOptions is not None:
172183
res["templateOptions"] = signup_options.templateOptions
173-
if signup_options.revoke_other_sessions is not None:
174-
res["revokeOtherSessions"] = signup_options.revoke_other_sessions
184+
if signup_options.revokeOtherSessions is not None:
185+
res["revokeOtherSessions"] = signup_options.revokeOtherSessions
175186
return res

tests/test_enchantedlink.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ def test_sign_in(self):
165165
LoginOptions(
166166
stepup=True,
167167
template_options={"blue": "bla"},
168+
template_id="foo",
168169
revoke_other_sessions=True,
169170
),
170171
refresh_token=refresh_token,
@@ -183,6 +184,7 @@ def test_sign_in(self):
183184
"stepup": True,
184185
"customClaims": None,
185186
"templateOptions": {"blue": "bla"},
187+
"templateId": "foo",
186188
"revokeOtherSessions": True,
187189
"mfa": False,
188190
},
@@ -304,7 +306,9 @@ def test_sign_up(self):
304306
"http://test.me",
305307
None,
306308
SignUpOptions(
307-
template_options={"bla": "blue"}, revoke_other_sessions=True
309+
template_options={"bla": "blue"},
310+
template_id="foo",
311+
revoke_other_sessions=True,
308312
),
309313
)
310314
mock_post.assert_called_with(
@@ -321,6 +325,7 @@ def test_sign_up(self):
321325
"email": "[email protected]",
322326
"loginOptions": {
323327
"templateOptions": {"bla": "blue"},
328+
"templateId": "foo",
324329
"revokeOtherSessions": True,
325330
},
326331
},

tests/test_magiclink.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_compose_body(self):
6060
},
6161
)
6262

63-
lo = LoginOptions(stepup=True, custom_claims={"k1": "v1"})
63+
lo = LoginOptions(stepup=True, custom_claims={"k1": "v1"}, template_id="foo")
6464
self.assertEqual(
6565
MagicLink._compose_signin_body("id1", "uri1", lo),
6666
{
@@ -70,6 +70,7 @@ def test_compose_body(self):
7070
"stepup": True,
7171
"mfa": False,
7272
"customClaims": {"k1": "v1"},
73+
"templateId": "foo",
7374
},
7475
},
7576
)
@@ -193,7 +194,9 @@ def test_sign_in(self):
193194
DeliveryMethod.EMAIL,
194195
195196
"http://test.me",
196-
LoginOptions(stepup=True, template_options={"blue": "bla"}),
197+
LoginOptions(
198+
stepup=True, template_options={"blue": "bla"}, template_id=None
199+
),
197200
refresh_token=refresh_token,
198201
)
199202
mock_post.assert_called_with(
@@ -274,7 +277,7 @@ def test_sign_up(self):
274277
275278
"http://test.me",
276279
signup_user_details,
277-
SignUpOptions(template_options={"bla": "blue"}),
280+
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
278281
)
279282
self.assertEqual("t***@example.com", resp)
280283

@@ -294,7 +297,10 @@ def test_sign_up(self):
294297
"email": "[email protected]",
295298
},
296299
"email": "[email protected]",
297-
"loginOptions": {"templateOptions": {"bla": "blue"}},
300+
"loginOptions": {
301+
"templateOptions": {"bla": "blue"},
302+
"templateId": "foo",
303+
},
298304
},
299305
allow_redirects=False,
300306
verify=True,
@@ -420,7 +426,7 @@ def test_sign_up_or_in(self):
420426
DeliveryMethod.EMAIL,
421427
422428
"http://test.me",
423-
SignUpOptions(template_options={"bla": "blue"}),
429+
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
424430
),
425431
)
426432
mock_post.assert_called_with(
@@ -437,6 +443,7 @@ def test_sign_up_or_in(self):
437443
"customClaims": None,
438444
"mfa": False,
439445
"templateOptions": {"bla": "blue"},
446+
"templateId": "foo",
440447
},
441448
},
442449
allow_redirects=False,

tests/test_otp.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def test_sign_up(self):
258258
DeliveryMethod.EMAIL,
259259
260260
signup_user_details,
261-
SignUpOptions(template_options={"bla": "blue"}),
261+
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
262262
),
263263
)
264264
mock_post.assert_called_with(
@@ -277,7 +277,10 @@ def test_sign_up(self):
277277
"email": "[email protected]",
278278
},
279279
"email": "[email protected]",
280-
"loginOptions": {"templateOptions": {"bla": "blue"}},
280+
"loginOptions": {
281+
"templateOptions": {"bla": "blue"},
282+
"templateId": "foo",
283+
},
281284
},
282285
allow_redirects=False,
283286
verify=True,
@@ -386,7 +389,9 @@ def test_sign_in(self):
386389
client.otp.sign_in(
387390
DeliveryMethod.EMAIL,
388391
389-
LoginOptions(stepup=True, template_options={"blue": "bla"}),
392+
LoginOptions(
393+
stepup=True, template_options={"blue": "bla"}, template_id="foo"
394+
),
390395
refresh_token=refresh_token,
391396
)
392397
mock_post.assert_called_with(
@@ -402,6 +407,7 @@ def test_sign_in(self):
402407
"stepup": True,
403408
"customClaims": None,
404409
"templateOptions": {"blue": "bla"},
410+
"templateId": "foo",
405411
"mfa": False,
406412
},
407413
},
@@ -449,7 +455,7 @@ def test_sign_up_or_in(self):
449455
client.otp.sign_up_or_in(
450456
DeliveryMethod.EMAIL,
451457
452-
SignUpOptions(template_options={"bla": "blue"}),
458+
SignUpOptions(template_options={"bla": "blue"}, template_id="foo"),
453459
),
454460
)
455461
mock_post.assert_called_with(
@@ -465,6 +471,7 @@ def test_sign_up_or_in(self):
465471
"customClaims": None,
466472
"mfa": False,
467473
"templateOptions": {"bla": "blue"},
474+
"templateId": "foo",
468475
},
469476
},
470477
allow_redirects=False,

0 commit comments

Comments
 (0)