def activate(key): now = service_timestamp_get() try: params = base64.b64decode(str.encode(key)).decode( 'utf-8', 'ignore') except BaseException as e: _(500, str(e)) _(406, 'The provided key has an invalid format', stop=True) insert_date = params[:10] email = params[10:] m = User.query.filter_by(insert_date=insert_date, email=email).first() if m is None: return m else: if m.is_active: _(412, 'This user cannot be activated', stop=True) else: try: m.last_update_date = now m.is_active = True db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False return False
def add(): now = service_timestamp_get() try: m = Ban(now, now, Ban.generate_signature(), 1, False) db.session.add(m) db.session.commit() return True except BaseException as e: _(500, str(e)) return False
def add_by_user_id(id, meta, value): now = service_timestamp_get() try: m = History(now, id, meta, value) db.session.add(m) db.session.commit() db.session.close() return True except: return False
def clean(): now = service_timestamp_get() diff = now - config_app().data['ban_trap_range_millis'] / 1000 try: Ban.query.filter(Ban.last_update_date < diff).delete() db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def clean(): now = service_timestamp_get() try: TokenBanlist.query.filter( TokenBanlist.expiration_time < now).delete() db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def add(expiration_time, token): now = service_timestamp_get() try: m = TokenBanlist(now, expiration_time, token) db.session.add(m) db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def signup(email, password): now = service_timestamp_get() password_hash = User.password_hash(password) try: m = User(now, now, email, password_hash) db.session.add(m) db.session.commit() db.session.close() response = base64.b64encode(bytes(str(now) + email, "utf-8")) return str(response)[2:-1] except BaseException as e: _(500, str(e)) return False
def update_email_by_id(id, email): now = service_timestamp_get() m = User.find_by_id(id) if m is None: return m try: model_history.add_by_user_id(id, 'email', m.email) m.last_update_date = now m.email = email m.is_active = False db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def update_password_by_id(id, old_password, new_password): now = service_timestamp_get() m = User.find_by_id(id) if m is None: return m if User.verify_password(old_password, m.password) is False: _(406, 'The old_password is invalid', stop=True) try: model_history.add_by_user_id(id, 'password', m.password) m.last_update_date = now m.password = User.password_hash(new_password) #m.is_active = False db.session.commit() db.session.close() return True except BaseException as e: _(500, str(e)) return False
def verify_ott_auth_token(email, token): if model_token_banlist.is_banned(token) is True: return False m = User.find_by_email(email) if m is None or m.nonce is None: return None s = Serializer(m.nonce) try: data = s.loads(token) m = db.session.query(User).get(data['id']) m_refresh_token = User.refresh_token(email, token) m.__dict__['token'] = m_refresh_token['token'] m.__dict__['expiration_time'] = m_refresh_token['expiration_time'] model_token_banlist.add(service_timestamp_get(), token) return m.__dict__ except SignatureExpired: return False except BadSignature: return False
def authenticate(email, password): now = service_timestamp_get() m = User.query.filter_by(email=email).first() if not isinstance(m, User): return None if User.verify_password(password, m.password) is not True: return False if m.is_active == 0: return False if User.refresh_nonce(m.id) is not True: return False expires_in = config_app().data['token_expires_after_seconds'] s = Serializer(m.nonce, expires_in=expires_in) token = s.dumps({'id': m.id}) expiration_time = now + expires_in return { 'token': token.decode('ascii'), 'expiration_time': expiration_time }
def ott_generate(email, token): if model_token_banlist.is_banned(token) is True: return False attrs = User.verify_auth_token(email, token) if attrs is False or attrs is None or attrs['email'] != email: return False now = service_timestamp_get() m = User.query.filter_by(email=email).first() if m is None: return m if m.is_active == 0: return False expires_in = config_app().data['ott_token_expires_after_seconds'] expiration_time = now + expires_in s = Serializer(m.nonce, expires_in=expires_in) new_token = s.dumps({'id': m.id}) return { 'token': new_token.decode('ascii'), 'expiration_time': expiration_time }
def grant(m): if isinstance(m, Ban): now = service_timestamp_get() if (now - m.last_update_date ) > config_app().data['ban_timeout_millis'] / 1000: m.is_banned = False m.counter = 0 m.last_update_date = now if m.last_update_date < ( now - config_app().data['ban_trap_range_millis'] / 1000): m.counter = 0 m.last_update_date = now else: m.counter += 1 if m.counter > config_app( ).data['ban_trap_max_attempts_in_range']: return False if m.is_banned is True: return False db.session.commit() db.session.close() return True
def update_registry_by_id(user__id, attrs={}): now = service_timestamp_get() m = Registry.query.filter_by(user__id=user__id).first() if m is None: m = Registry(now, now, user__id) db.session.add(m) try: m.last_update_date = now for attr, value in attrs: attr = attr.lower() attr = attr.replace('-', '_') if attr in Registry.safe_attrs(): if value == "true" or value == "on": value = 1 elif value == "false" or value == "off": value = 0 setattr(m, attr, value) db.session.commit() db.session.close() return True except: return False
def reset_password(token): now = service_timestamp_get() s = Serializer(config_app().data['secret_key']) try: data = s.loads(token) except SignatureExpired: return False except BadSignature: return False m = db.session.query(User).get(data['id']) if m is None: return m try: model_history.add_by_user_id(m.id, 'password', m.password) m.last_update_date = now new_password = User.generate_password() print('New password has been generated', new_password) m.password = User.password_hash(new_password) db.session.commit() db.session.close() return str(base64.b64encode(bytes(new_password, "utf-8")))[1:] except BaseException as e: _(500, str(e)) return False