def login(): if request.method == 'GET': user_dict = request.authorization elif request.method == 'POST': user_dict = request.get_json() if user_dict is None or user_dict is '': return util.make_auth_challenge(msg='No credentials provided') try: username = user_dict['username'] except: return util.make_json_error(msg='Missing username', status_code=401) try: password = user_dict['password'] except: return util.make_json_error(msg='Missing password', status_code=401) if not User.authenticate(username, password): return util.make_json_error(msg='Wrong username and/or password', status_code=401) try: user = User.get_by_name(user_dict['username']) login_user(user) return util.make_json_success(msg='Success') except UserNotFoundError as e: return util.make_json_error(msg='User not found', status_code=401)
def test_signup(self): username = str(uuid.uuid4()) payload = dict(username=username, password='******', email='{}@test.com'.format(username), passvalid='test_pass') response = self.post_json('/signup', data=payload) self.assertEqual(response.status_code, 200) self.assertEqual(response.json, dict(msg='Success', status='ok')) user = User.get_by_name(username) self.assertEqual(user.username, username)
def load_user_request(id): user_dict = request.authorization if user_dict is None or user_dict is '': return None try: username = user_dict['username'] except: return None try: password = user_dict['password'] except: return None if not User.authenticate(username, password): return None try: user = User.get_by_name(user_dict['username']) login_user(user) return user except UserNotFoundError as e: return None
def signup(): json_data = request.get_json() # TODO: Validation try: username = json_data['username'] except: return util.make_json_error(msg='Missing username') try: email = json_data['email'] except: return util.make_json_error(msg='Missing email') try: password = json_data['password'] except: return util.make_json_error(msg='Missing password') try: passvalid = json_data['passvalid'] except: return util.make_json_error(msg='Missing password validation') # Optional try: lulebo_username = json_data['lulebo_username'] except: lulebo_username = None try: lulebo_password = json_data['lulebo_password'] except: lulebo_password = None # Validation if password != passvalid: return util.make_json_error(msg='Passwords don\'t match') try: User.get_by_name(username) return util.make_json_error( msg='User "{}" already registered'.format(username)) except: pass try: User.get_by_email(email) return util.make_json_error(msg='Email already registered') except: pass # All checks passed, create user user = User( username=username, email=email, password=password, lulebo_username=lulebo_username if lulebo_username is not None else '', lulebo_password=lulebo_password if lulebo_password is not None else '', uuid=str(uuid.uuid4())) try: db.session.add(user) db.session.commit() msg, status = 'Success', 'ok' except Exception as e: db.session.rollback() msg, status = 'unknown error', 'error' db.session.close() return jsonify({'msg': msg, 'status': status})