In this article, we'll see how to create a random token to use in a Python app URL to reset a user's password.
We can use the token_urlsafe()
method of the secrets
module by specifying the number of bytes
that will make up the random token.
import secrets
def create_password_recovery_url(bytes_num=32):
base_url = 'https://site.tld/?t='
return base_url + secrets.token_urlsafe(bytes_num)
Usage example:
def main():
print(create_password_recovery_url())
if __name__ == '__main__':
main()
A possible output could be the following:
https://site.tld/?t=I04sbDVcHoWU82uQogN4CsuzR5ry2y-jANTUi3zWSe4