Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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'}
Ejemplo n.º 5
0
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()
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
def get_new_token():
    token = uuid.uuid4().hex
    Token.create(token=token)
    return {'token': token}
Ejemplo n.º 8
0
def token_authentication(token):
    try:
        Token.get(Token.token == token)
        return True
    except peewee.DoesNotExist:
        return False