-
Notifications
You must be signed in to change notification settings - Fork 45
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
base: main
Are you sure you want to change the base?
Conversation
yezz123
commented
Feb 7, 2023
I'm a bit unclear on the usage here. I think the api should perhaps be more like class LoginForm(BaseModel):
password: str # whatever
ot_token: OTPToken
...
LoginForm.model_validate(form_data, context={'otp_secret': otp_secret_from_database_etc})
Does that make sense? |
I think this approach is clear, more than using I will try to make this approach! |
Hey @samuelcolvin I think we have schema close to this: from pydantic import BaseModel
from pydantic_extra_types import OTPToken
import pyotp
class LoginForm(BaseModel):
username: str
password: str
ot_token: OTPToken
# Generate a secret key to be used for OTP generation
otp_secret = pyotp.random_base32()
# User input in the form of a dictionary
form_data = {
'username': 'user123',
'password': 'secret_password',
'ot_token': pyotp.TOTP(otp_secret).now()
}
# Validate the form data
form = LoginForm.model_validate(form_data, context={'otp_secret': otp_secret})
# Print the validated form data
print(f'Login successful for user {form}') |
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.
news to be updated to pydantic V2.
We'll also need to customise JSON Schema.
@samuelcolvin all the changes fixed, This Pull request is ready to merge |
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.
please have a look at how custom types work in pydantic v2
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls) -> Any: | ||
yield cls.model_validate |
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