Skip to content
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
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

chore(feat): Support OTP as a type #12

wants to merge 9 commits into from

Conversation

yezz123
Copy link
Collaborator

@yezz123 yezz123 commented Feb 7, 2023

import random

from pydantic import BaseModel, Field
from pydantic_extra_types import OTP, OTP_ALPHABET


class GenerateOTP(BaseModel):
    """
    Generate an OTP
    """

    length: int = Field(6, ge=6, le=6)
    alphabet: str = Field(OTP_ALPHABET, min_length=6, max_length=32)

    def generate(self) -> OTP:
        return OTP(''.join(random.choices(self.alphabet, k=self.length)))

@yezz123 yezz123 self-assigned this Feb 7, 2023
@yezz123 yezz123 linked an issue Feb 7, 2023 that may be closed by this pull request
@samuelcolvin
Copy link
Member

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})

OTPToken then just raises a ValidationError if the token is incorrect, otherwise the value could just be None.

Does that make sense?

@yezz123
Copy link
Collaborator Author

yezz123 commented Feb 9, 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})

OTPToken then just raises a ValidationError if the token is incorrect, otherwise, the value could just be None.

Does that make sense?

I think this approach is clear, more than using OTP to validate!

I will try to make this approach!

@yezz123
Copy link
Collaborator Author

yezz123 commented Feb 12, 2023

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}')

Copy link
Member

@samuelcolvin samuelcolvin left a 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.

pydantic_extra_types/types/otp.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
@yezz123
Copy link
Collaborator Author

yezz123 commented Mar 9, 2023

@samuelcolvin all the changes fixed, This Pull request is ready to merge

Copy link
Member

@samuelcolvin samuelcolvin left a 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
Copy link
Member

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.

Copy link
Member

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.

Copy link
Collaborator Author

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

theunkn0wn1 added a commit to theunkn0wn1/pydantic-extra-types that referenced this pull request Jan 11, 2024
theunkn0wn1 added a commit to theunkn0wn1/pydantic-extra-types that referenced this pull request Jan 11, 2024
theunkn0wn1 added a commit to theunkn0wn1/pydantic-extra-types that referenced this pull request Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add OTP Type to Types
2 participants