def login(): """ This methods uses to login in order to gain access token :return: access token """ param = {'username': fields.String(), 'password': fields.String()} try: json_data = parse_req(param) username = json_data.get('username', None) password = json_data.get('password', None) except: return jsonify("An error occurred when getting data !") user = userRegister.find_one({'username': username}) if not safe_str_cmp(user['password'], password): return jsonify("Your password is wrong!") access_token = create_access_token(identity=str(user['_id']), expires_delta=ACCESS_EXPIRES) dict1 = {'access_token': access_token, 'message': 'Login Successfully !'} user_token = dict( _id=str(ObjectId()), person_id=user['_id'], access_token=access_token, ) Token.insert_one(user_token) return jsonify(dict1)
def activate_session_token(self, user): session_token = Token(owner=user, data=bcrypt.gensalt(30).decode(), expiring_date=datetime.today() + timedelta(days=2)) self.set_secure_cookie(TOKEN_COOKIE_NAME, session_token.data) self.session.add(session_token)
def score_message(hug_token, message_id: hug.types.number, score: hug.types.number): try: msg = Message.get(Message.token == Token.get(Token.token == hug_token), Message.id == message_id, Message.reply_to != None) Score.create(message=msg, value=score) return {"status": "ok"} except Message.DoesNotExist: raise HTTPError(HTTP_404) except peewee.IntegrityError: raise HTTPError(HTTP_409)
def registration(login: hug.types.text, password: hug.types.text): if len(password) >= 6: password = hashlib.sha1(password.encode()).hexdigest() else: raise HTTPError(HTTP_422) token = uuid.uuid4().hex token = Token.create(token=token) try: User.create(**locals()) except peewee.IntegrityError: token.delete() raise HTTPError(HTTP_409) return {'status': 'ok'}
def receive_message(hug_token, after_msg_id: hug.types.number): token = Token.get(Token.token == hug_token) return Message.select().where(Message.token == token, Message.reply_to != None, Message.id > after_msg_id).dicts()
def send_message(hug_token, message: hug.types.text): msg = Message.create(content=message, token=Token.get(Token.token == hug_token)) return model_to_dict(chatbot.process_question(msg))
def get_new_token(): token = uuid.uuid4().hex Token.create(token=token) return {'token': token}
def token_authentication(token): try: Token.get(Token.token == token) return True except peewee.DoesNotExist: return False