Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to generate a reset token in python?

I'm trying to make a validation process for a password reset, what i've used are two values: the epoch time, and i want to use the users's old password (pbkdf2) as a key,

Since i dont want to get non ASCII characters, i've used SimpleEncode library because it's fast since it's only a BASE64 with a key used, but the problem is that the password is too long (196 chars) so i get a long key!

What i've done is split the result code = simpleencode.encode(key,asci)[::30], but this will not be unique!

To get an idea how it works, i've tried Facebook reset process, but what is given is a number! so how this process works, don't they use a key to make it hard for someone to forge a link to reset someone's password?

Update: how the algorithme will work:

1- get the time using epoche time.time()

2- generate the Base64 of the epoche time (to use for the URL) and the epoch time value + a key, this key is PBKDF2(password).

3- generate the url www.example.com/reset/user/Base64(time.time()) and send this URL + the simpleencode.encode(key,asci)[::30]

4- when the user clicks on the URL, he put the generated code, this generated code, if it matches with the URL, then let him modifiy the password, else, it is a forget URL!

like image 250
Abdelouahab Pp Avatar asked Feb 05 '13 17:02

Abdelouahab Pp


People also ask

How do I generate a secure token in Python?

Generating tokens¶. The secrets module provides functions for generating secure tokens, suitable for applications such as password resets, hard-to-guess URLs, and similar. Return a random byte string containing nbytes number of bytes. If nbytes is None or not supplied, a reasonable default is used.

How do I generate a random token in Python?

Generating Tokens. The secrets module is part of the Python standard library in Python 3.6 and newer. You can import this module into your application or into a Python shell as follows: >>> import secrets. At the core of this module there are three functions that generate random tokens using the best random number generator provided by your system.

What is the best way to generate a password reset token?

Password reset tokens aren't very much like passwords. They're short-lived, single-use, and - most relevantly here - machine generated and non-memorable. You absolutely must use a cryptographically secure random number generator to produce the token. crypto.randomBytes is good enough, yes.

What is a token in Python?

A token is the smallest individual unit in a python program. All statements and instructions in a program are built with tokens. The various tokens in python are : 1. Keywords: Keywords are words that have some special meaning or significance in a programming language.


1 Answers

Not sure it's the best way, but I'd probably just generate a UUID4, which can be used in a URL to reset the password and expire it after 'n' amount of time.

>>> import uuid
>>> uuid.uuid4().hex
'8c05904f0051419283d1024fc5ce1a59'

You could use something like http://redis.io to hold that key, with a value of the appropriate user ID and set its time to live. So, when something comes in from http://example.com/password-reset/8c05904f0051419283d1024fc5ce1a59 it looks to see if it's valid and if so then allows changes to set a new password.

If you did want a "validation pin", then store along with the token, a small random key, eg:

>>> from string import digits
>>> from random import choice
>>> ''.join(choice(digits) for i in xrange(4))
'2545'

And request that be entered on the reset link.

like image 159
Jon Clements Avatar answered Sep 27 '22 17:09

Jon Clements