-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(feat): Support OTP as a type #12
Open
yezz123
wants to merge
9
commits into
main
Choose a base branch
from
type/otp
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
de502fe
chore(feat): Support OTP as type
yezz123 f5cfad7
add OTP Numeric digits
yezz123 030a366
fix code, use `pyotp`
yezz123 1c5c4fc
switch `get_validators` to `__get_pydantic_core_schema__`
yezz123 03c8535
update requirements
yezz123 98ed15b
fix `json` function
yezz123 e36c91e
fix requirements
yezz123 fd0506e
fix color
yezz123 0a83d38
fix linting issue
yezz123 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import Any | ||
|
||
import pyotp | ||
from pydantic_core import PydanticCustomError | ||
|
||
|
||
class OTPToken(str): | ||
"""A one-time password token. | ||
|
||
This is a custom type that can be used to validate a one-time password token | ||
against a secret key. The secret key is passed in the context argument of | ||
the model_validate method. | ||
|
||
The type also has a custom JSON encoder that returns the current one-time | ||
password token for the secret key. | ||
""" | ||
|
||
@staticmethod | ||
def model_validate(value: Any, *, context: Any) -> Any: | ||
if not pyotp.TOTP(context['otp_secret']).verify(value): | ||
raise PydanticCustomError('Invalid one-time password', value) | ||
return value | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls) -> Any: | ||
yield cls.model_validate | ||
|
||
class Config: | ||
json_encoders = {pyotp.TOTP: lambda v: v.now()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import pyotp | ||
import pytest | ||
from pydantic_core import PydanticCustomError | ||
|
||
from pydantic_extra_types import OTPToken | ||
|
||
|
||
def test_model_validate(): | ||
secret = 'JBSWY3DPEHPK3PXP' | ||
totp = pyotp.TOTP(secret) | ||
value = totp.now() | ||
context = {'otp_secret': secret} | ||
# Test a valid OTP token | ||
result = OTPToken.model_validate(value, context=context) | ||
assert result == value | ||
|
||
# Test an invalid OTP token | ||
with pytest.raises(PydanticCustomError, match='Invalid one-time password'): | ||
OTPToken.model_validate('Invalid one-time password', context=context) | ||
|
||
|
||
def test_json_encoder(): | ||
secret = 'JBSWY3DPEHPK3PXP' | ||
totp = pyotp.TOTP(secret) | ||
value = totp.now() | ||
|
||
# Test the custom JSON encoder | ||
json_encoded_value = OTPToken.Config.json_encoders[pyotp.TOTP](totp) | ||
assert json_encoded_value == value | ||
|
||
|
||
def test___get_pydantic_core_schema__(): | ||
# Test the get_validators method | ||
validators = list(OTPToken.__get_pydantic_core_schema__()) | ||
assert validators == [OTPToken.model_validate] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not how pydantic V2 works, please look at the examples in pydantic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, this should be breaking all tests, so something must be wrong in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
almost 3 weeks didn't update it i will update it and also changes it, I guess this one would work fine #22