def get_user_by_validation_token(self, validation_token): try: validation_token_hash = bcrypt.hashpw( validation_token.encode("utf8"), secret_salt) except Exception: return None return get_one(Users.select().where( Users.validation_token_hash == validation_token_hash))
def complete_reset(self, reset_token, password): reset_token_hash = bcrypt.hashpw(reset_token.encode("utf8"), secret_salt) query = Users.select().where( Users.reset_token_hash == reset_token_hash) if query: user = query[0] user.password_hash = bcrypt.hashpw(password.encode("utf8"), bcrypt.gensalt()) user.save() return user
def complete_registration(self, validation_token, password): validation_token_hash = bcrypt.hashpw(validation_token.encode("utf8"), secret_salt) user = get_one(Users.select().where( Users.validation_token_hash == validation_token_hash)) if user and not user.password_hash: user.password_hash = bcrypt.hashpw(password.encode("utf8"), bcrypt.gensalt()) user.save() return user return None
def create_enrollment(user_id, user_email): data = request.get_json() study_id = data['study_id'] study = Studies.get_by_id(study_id) user = Users.get(Users.email == user_email) enroll = Enrollments(study=study, user=user) try: enroll.save(force_insert=True) except IntegrityError as e: return jsonify({"error": "%s" % e}), 500 d = model_to_dict(study) d.update({'enrolled': True}) return d
def add_user(self, email): validation_token = token_urlsafe(32) # uses the jwt_secret as a salt because can't search in DB otherwise validation_token_hash = bcrypt.hashpw(validation_token.encode("utf8"), secret_salt) validation_token_expiry = datetime.now(tz) + timedelta( seconds=validation_token_duration_in_seconds) query = Users.select().where(Users.email == email) if query: user = query[0] user.validation_token_hash = validation_token_hash user.validation_token_expiry = validation_token_expiry user.save() added = False else: user = Users.create( email=email, validation_token_hash=validation_token_hash, validation_token_expiry=validation_token_expiry, ) added = True return (user, added, validation_token)
def set_reset_token(self, email): reset_token = token_urlsafe(32) reset_token_hash = bcrypt.hashpw(reset_token.encode("utf8"), secret_salt) reset_token_expiry = datetime.now(tz) + timedelta( seconds=reset_token_duration_in_seconds) query = Users.select().where(Users.email == email) if query: user = query[0] user.reset_token_hash = reset_token_hash user.reset_token_expiry = reset_token_expiry user.save() return (user, reset_token) else: return (None, reset_token)
def delete_enrollment(user_id, user_email, study_id): study = Studies.get_by_id(study_id) user = Users.get_by_id(user_id) try: enroll = Enrollments.get(Enrollments.study == study, Enrollments.user == user) except DoesNotExist: return {"error": 'Could not find the given enrollment'}, 404 except Exception as e: return {"error": '%s' % e}, 500 else: enroll.delete() d = model_to_dict(study) d.update({'enrolled': False}) return d
db.session.add_all([one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty]) db.session.commit() print('product levels created') one = ProductView(9, 9, '2018-09-22 02:19:30') two = ProductView(9, 7, '2018-09-27 02:47:43') three = ProductView(9, 12, '2018-09-22 03:20:59') four = ProductView(9, 10, '2018-09-29 03:07:11') five = ProductView(9, 5, '2018-09-22 03:19:19') six = ProductView(9, 8, '2018-09-21 15:57:50') seven = ProductView(9, 6, '2018-09-22 02:12:54') eight = ProductView(9, 1, '2018-09-22 03:03:36') db.session.add_all([one, two, three, four, five, six, seven, eight]) db.session.commit() print('product view data populated') one = Users('Mukul', '*****@*****.**', 'mukul', '$5$rounds=535000$6PJhbzFlfJbcQbza$FbrPa3qqk1RJ5MSffRLO6LrQJXbgO8SudFuBpNf.wR7', '', '2018-07-23 14:09:14', '0', 'yes') two = Users('Nur Mohsin', '*****@*****.**', 'mohsin', '$5$rounds=535000$EnLkwqfGWGcWklRL$q9PbYw/TVXSzs.QpgUouZ3.6BzaPG2eLHkTyv.Qx80D', '123456789022', '2018-07-21 06:47:57', '1', 'yes') three = Users('Nur Mohsin', '*****@*****.**', 'khan', '$5$rounds=535000$wLKTQexvPQHueUsK$aFrFUXBHjrrAH61EFiYgj8cZECaaz8y6S5XS/zkkHw9', '', '2018-09-07 09:02:35', '0', 'yes') four = Users('Robin', '*****@*****.**', 'robin', '$5$rounds=535000$uiZc/VCwwa3XCTTe$Ec.JOjy4GkjpAXHtAvGt6pSc6KszajHgcyZy8v6Ivk1', '', '2018-07-26 12:36:57', '0', 'yes') five = Users('Sujon', '*****@*****.**', 'sujons', '$5$rounds=535000$aGykDT1yrocgTaDt$p2dDAMDz9g3N6o/Jj7QJY9B6NnMlUot.DCq/LOsCS13', '89345793753', '2018-09-08 13:58:36', '0', 'yes') db.session.add_all([one, two, three, four, five]) db.session.commit() print( ' Users created')
def get_user_by_reset_token(self, reset_token): reset_token_hash = bcrypt.hashpw(reset_token.encode("utf8"), secret_salt) return get_one( Users.select().where(Users.reset_token_hash == reset_token_hash))
def get_user_by_email(self, user_email): return get_one(Users.select().where(Users.email == user_email))
def get_user_by_id(self, user_id): return get_one(Users.select().where(Users.id == user_id))
from ..flask_util import jsonify_response from .bucket import ip_rate_limit, token_rate_limit from core.exceptions import UsernameAlreadyExists, ForbiddenArgument, LoginFailed, EmailAlreadyRegistered from core.models import Users, Blogs from core.cachemanager import CacheGenerator from core.types_ import JsonStatus __version__ = "0.1.0" api = Blueprint("api", __name__, static_folder="../../static/", template_folder="../../templates/", url_prefix="/api") users = Users() blogs = Blogs() CacheGenerator().generate_cache() # AUTHENTICATION def require_token(fn): """ FYI: adds another argument to the function: the current token :raise: HTTP 403 if the token is not valid """ @wraps(fn) def inner(*args, **kwargs): token = request.headers.get("Authorization") user_id = users.verify_token(token) if not token or not user_id:
def get_enrollments(user_id, user_email): user = Users.get(Users.email == user_email) enrollments = Enrollments.select( Enrollments.study_id).filter(Enrollments.user == user).tuples() return jsonify([e[0] for e in enrollments])