0

I am working with Google App Engine and using the datastore to create a table for Users containing the entities username, the hash of the password, the email and the date joined. But when I run a function to register a user the function that hashes the passwords is returning the NoneType error.

I tried to look for the error, but the form is posting the values and I am able to get them from the headers.

This is the piece of code that handles the registration

def make_salt():
chars = string.ascii_uppercase + string.ascii_lowercase + string.ascii_digits
return ''.join(random.choice(chars) for x in range(5))

def make_pw_h(name, pw, salt = None):
    if salt:
        salt = make_salt()
 return "%s,%s" % (salt, hashlib.sha256(name + pw + salt).hexdigest())

def check_pw_h(name, pw, h):
     h = h.split(',')[1]
    return h == make_pw_h(name, pw, h)

class Users(db.Model):
    username = db.StringProperty(required = True)
    pw_hash = db.StringProperty(required = True)
    emai = db.StringProperty()
    user_since = db.DateTimeProperty(auto_now_add = True)

@classmethod
def by_id(cls, uid):
    return Users.get_by_id(uid)

@classmethod
def by_name(cls, name):
    user = Users.all().filter('name = ', name).get()
    return user

@classmethod
def register(cls, name, pw, email = None):
    pw_h = make_pw_h(name, pw)
    return Users(username = name,
                pw_hash = pw_h,
                email = email)

@classmethod
def login(cls, name, pw):
    u = cls.by_name(name)
    if u and check_pw(pw):
        return u        

def make_secure_val(s):
    return '%s|%s' % (val, hmac.new(secret, val).hexdigest())

def check_secure_val(sec_val):
    val = val.split('|')[0]
    if sec_val == make_secure_val(val):
        return val

class BaseHandler(webapp2.RequestHandler):

#this function uses the render_str funcion to display the html form in the browser
def render(self, template, **kw):
    self.response.out.write(render_str(template, **kw))

#this funcion is a simple function using the template engine to write out the required data with 
#any extra parameters
def write(self, *a, **kw):
    self.response.out.write(*a, **kw)

def set_sec_coki(self, name, val):
    sec_val = make_secure_val(val)
    self.response.headers.add_cookie('Set-Cookie', "%s=%s; Path=/" % (name, sec_val))

def read_sec_coki(self, name):
    coki_val = self.request.cookies.get(name)
    return coki_val and check_secure_val(coki_val)

def Initialize(self, *a, **kw):
    webapp2.RequestHandler.Initialize(self, *a, **kw)
    uid = self.read_sec_coki('user-id')
    self.user = uid and Users.by_id(int(uid))


class Signup(BaseHandler):
def get(self):
    self.render("signup-form.html")

def post(self):
    have_error = False
    self.username = self.request.get('username')
    self.password = self.request.get('password')
    self.verify = self.request.get('verify')
    self.email = self.request.get('email')

    params = dict(username = self.username,
                  email = self.email)

    if not valid_username(self.username):
        params['error_username'] = "That's not a valid username."
        have_error = True

    if not valid_password(self.password):
        params['error_password'] = "That wasn't a valid password."
        have_error = True
    elif self.password != self.verify:
        params['error_verify'] = "Your passwords didn't match."
        have_error = True

    if not valid_email(self.email):
        params['error_email'] = "That's not a valid email."
        have_error = True

    if have_error:
        self.render('signup-form.html', **params)
    else:
        u = Users.by_name(self.username)
        if u:
            msg = "User already exists"
            self.render('signup-form.html', error_username = msg)
        else:
            sing_user = Users.register(self.username, self.password, self.email)
            sing_user.put()

            self.login(sing_user)


            self.set_sec_coki('user-id', sing_user.key().id())

            self.redirect('/welcome')


class WelcomePage(BaseHandler):
def get(self):
    coki_val = read_sec_coki('user-id')
    u_id = coki_val.check_secure_val(coki_val)
    part_usr = u_id.by_id(int(u_id))
    name = part_usr.username
    self.render("welcome.html", username = name)

And this is the error stack that I am getting when I try to register a new user via the form

Traceback (most recent call last):
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1536, in     __call__
rv = self.handle_exception(request, response, e)
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/bigb/google_projects/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/bigb/google_projects/my-ramblings/blog.py", line 197, in post
sing_user = Users.register(self.username, self.password, self.email)
File "/home/bigb/google_projects/my-ramblings/blog.py", line 55, in register
pw_h = make_pw_h(name, pw)
File "/home/bigb/google_projects/my-ramblings/blog.py", line 32, in make_pw_h
return "%s,%s" % (salt, hashlib.sha256(name + pw + salt).hexdigest())
TypeError: coercing to Unicode: need string or buffer, NoneType found
4

1 回答 1

2

You call make_pw_h() with just two arguments, name and pw. You leave salt to the default, None:

def make_pw_h(name, pw, salt = None):
    if salt:
        salt = make_salt()
    return "%s,%s" % (salt, hashlib.sha256(name + pw + salt).hexdigest())

This means the if salt: test is False and you end up with passing None to hashlib.sha256.

Perhaps you meant to test if salt is not set:

def make_pw_h(name, pw, salt=None):
    if salt is None:
        salt = make_salt()
    return "%s,%s" % (salt, hashlib.sha256(name + pw + salt).hexdigest())
于 2013-07-01T17:52:23.743 回答