Skip to content

Add OTP sendmail #95

@mekarpeles

Description

@mekarpeles

The OTP must first be sanitized by a regex.

from openlibrary.core import cache

class thirdparty_otp(delegate.page):
    path = "/account/otp/send"

    def POST(self):
        import time
        i = web.input(otp=None, email=None, ts=None, ip=None)
        if not (i.ip and i.email and i.ts and i.otp):
            return web.badrequest()

        mc = cache.get_memcache()

        # IP-based throttle                                                                                                                                                       
        ip_key = f"otp:ip:{i.ip}"
        if not mc.add(ip_key, 1, expires=60):  # 60s TTL                                                                                                                          
            return web.forbidden("This IP must wait before requesting again")

        # Email-based throttle                                                                                                                                                    
        email_key = f"otp:email:{i.email}"
        if not mc.add(email_key, 1, expires=60):
            return web.forbidden("This email must wait before requesting again")

        web.sendmail(                                                                                                                                                            
            config.from_address,                                                                                                                                                 
            i.email,                                                                                                                                                             
            subject="Your One Time Password",                                                                                                                                    
            message=web.safestr(f"Your one time password is: {i.otp}"),                                                                                                          
        )                                                                                                                                                                        
        return delegate.RawText(f"OTP Successfully Sent")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions