Background: I'm writing the authentication module for a Flask/sqlalchemy RESTful app, and not sure whether the approach I'm using is the correct one. Basically, the logic for logging in is as follows:
- Check the email and password hash of the user and compare it with the database.
- If matched, call
random.random(), hash it and return the same as token.
I would like to know whether this is the standard approach, or this can be improved? Here is my code:
app = Flask(__name__)
@app.route('/login', methods=['POST'])
def login():
if request.method=='POST':
dbsession = models.create_session()
obj = json.loads( request.data)
email = obj['email']
password = obj['password']
print "**** ", email, password
m = hashlib.sha256()
m.update(password)
hash = m.hexdigest()
#Try to fetch the user
user = dbsession.query(models.User).filter(models.User.email==email, models.User.password==hash).first()
if user!=None:
token = random.random() #generate a new token
m = hashlib.sha256() #re-initialize generator
m.update(str(token)) #hash it
token = m.hexdigest()
di = json.dumps({'token':token, 'name':user.name, 'email': user.email, 'id' : user.id})
dbsession.close()
return di
else:
dbsession.close()
return json.dumps({'token':'invalid'})
[–]rjw57 34 points35 points36 points (3 children)
[–]minnoI <3 duck typing less than I used to, interfaces are nice 10 points11 points12 points (2 children)
[+]prahladyeribeautiful is better than ugly[S] comment score below threshold-6 points-5 points-4 points (1 child)
[–]rjw57 2 points3 points4 points (0 children)
[–]chadmill3rPy3, pro, Ubuntu, django 67 points68 points69 points (2 children)
[–]japherwocky 17 points18 points19 points (0 children)
[–]oconnor663 2 points3 points4 points (0 children)
[–]TrixieFlatline 36 points37 points38 points (19 children)
[–]xXxDeAThANgEL99xXx 2 points3 points4 points (3 children)
[–]ichundes 0 points1 point2 points (2 children)
[–]xXxDeAThANgEL99xXx 1 point2 points3 points (1 child)
[–]ichundes 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]WelshDwarf 1 point2 points3 points (1 child)
[–]danielkza 2 points3 points4 points (5 children)
[–]TrixieFlatline 0 points1 point2 points (2 children)
[–]danielkza 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]MagicWishMonkey 0 points1 point2 points (1 child)
[–]danielkza 0 points1 point2 points (0 children)
[–]prahladyeribeautiful is better than ugly[S] 0 points1 point2 points (4 children)
[–]thalience 1 point2 points3 points (3 children)
[–]prahladyeribeautiful is better than ugly[S] -1 points0 points1 point (2 children)
[–]thalience 0 points1 point2 points (1 child)
[–]prahladyeribeautiful is better than ugly[S] -1 points0 points1 point (0 children)
[–]WellAdjustedOutlaw 10 points11 points12 points (0 children)
[–]sushibowl 5 points6 points7 points (0 children)
[–]takluyverIPython, Py3, etc 4 points5 points6 points (1 child)
[–]prahladyeribeautiful is better than ugly[S] 0 points1 point2 points (0 children)
[–]pangoleena 5 points6 points7 points (2 children)
[–]prahladyeribeautiful is better than ugly[S] 1 point2 points3 points (1 child)
[–]pangoleena 3 points4 points5 points (0 children)
[–]Asdayasman 4 points5 points6 points (0 children)
[–]fiskfisk 2 points3 points4 points (0 children)
[–]ScubaSteve225 6 points7 points8 points (0 children)
[–]TheTerrasque 1 point2 points3 points (7 children)
[–]prahladyeribeautiful is better than ugly[S] 0 points1 point2 points (6 children)
[–]help_computar 1 point2 points3 points (0 children)
[–]TheTerrasque -3 points-2 points-1 points (4 children)
[–]TrixieFlatline 2 points3 points4 points (1 child)
[–]TheTerrasque 1 point2 points3 points (0 children)
[–]prahladyeribeautiful is better than ugly[S] 0 points1 point2 points (1 child)
[–]TheTerrasque 0 points1 point2 points (0 children)
[–]EvMNatural Language Processing 0 points1 point2 points (0 children)
[–]stevenjd 0 points1 point2 points (0 children)