diff --git a/streamlit_authenticator/utilities/helpers.py b/streamlit_authenticator/utilities/helpers.py index fd8207af..e85f077c 100644 --- a/streamlit_authenticator/utilities/helpers.py +++ b/streamlit_authenticator/utilities/helpers.py @@ -5,10 +5,12 @@ Libraries imported: - string: Module providing support for ASCII character encoding. - random: Module generating random characters. +- captcha: Module generating captcha images. """ import string import random +from captcha.image import ImageCaptcha class Helpers: """ @@ -32,4 +34,19 @@ def generate_random_pw(cls, length: int=16) -> str: """ letters = string.ascii_letters + string.digits return ''.join(random.choice(letters) for i in range(length)).replace(' ','') - \ No newline at end of file + @classmethod + def generate_captcha(cls) -> tuple: + """ + Generates a captcha image. + + Returns + ------- + int + The randomly generated four digit captcha. + ImageCaptcha + The randomly generated captcha object. + """ + image = ImageCaptcha(width=120, height=75) + random_digit = random.choices(string.digits, k=4) + return random_digit, image.generate(random_digit) +