Example #1
0
def refresh():
    """Обновить токен"""
    json = request.get_json()

    if "token" not in json:
        return errors.wrong_payload("token")

    token = json["token"]

    actual_token = Token.get_or_none((Token.token == token)
                                     & (Token.token_type == "refresh"))

    if actual_token is None:
        return errors.token_invalid()

    if actual_token.valid_until < datetime.datetime.now():
        return errors.token_outdated()

    user = actual_token.user

    token = Token.generate_access_token(user)
    refresh_token = Token.generate_refresh_token(user)

    return jsonify({
        "success": 1,
        "access_token": {
            "token": token.token,
            "valid_until": token.valid_until.timestamp(),
        },
        "refresh_token": {
            "token": refresh_token.token,
            "valid_until": refresh_token.valid_until.timestamp(),
        },
    })
Example #2
0
def register():
    """Регистрация"""
    json = request.get_json()

    missed_payload = []

    if "username" not in json:
        missed_payload.append("username")
    if "password" not in json:
        missed_payload.append("password")
    if "email" not in json:
        missed_payload.append("email")
    if "name" not in json:
        missed_payload.append("name")

    if len(missed_payload) > 0:
        return errors.wrong_payload(missed_payload)

    username = json["username"]
    password = json["password"]
    email = json["email"]
    name = json["name"]

    user = User.get_or_none(User.username == username)
    if user is not None:
        return errors.registration_username_busy()
    user = User.get_or_none(User.email == email)
    if user is not None:
        return errors.registration_email_busy()

    user = User.create(
        username=username,
        password=salted(password, current_app.config["PASSWORD_SALT"]),
        email=email,
        registration_date=datetime.datetime.now(),
        last_active_date=datetime.datetime.now(),
        name=name,
    )

    token = Token.generate_access_token(user)
    refresh_token = Token.generate_refresh_token(user)

    return jsonify({
        "success": 1,
        "access_token": {
            "token": token.token,
            "valid_until": token.valid_until.timestamp(),
        },
        "refresh_token": {
            "token": refresh_token.token,
            "valid_until": refresh_token.valid_until.timestamp(),
        },
    })
Example #3
0
def login():
    """Авторизация"""
    json = request.get_json()

    has_login = "******" in json or "email" in json
    has_password = "******" in json
    if not has_login:
        return errors.wrong_payload("username", "email")
    if not has_password:
        return errors.wrong_payload("password")

    user = None
    if "username" in json:
        username = json["username"]

        user = User.get_or_none(User.username == username)
        if user is None:
            user = User.get_or_none(User.email == username)
    elif "email" in json:
        email = json["email"]

        user = User.get_or_none(User.username == email)
        if user is None:
            user = User.get_or_none(User.email == email)

    password = json["password"]

    if user is not None and authorize(user, password):
        token = Token.generate_access_token(user)
        refresh_token = Token.generate_refresh_token(user)

        return jsonify({
            "success": 1,
            "access_token": {
                "token": token.token,
                "valid_until": token.valid_until.timestamp(),
            },
            "refresh_token": {
                "token": refresh_token.token,
                "valid_until": refresh_token.valid_until.timestamp(),
            },
        })

    return errors.not_authorized()
Example #4
0
def tokens():
    user = User.create(
        username="******",
        password="******",
        email="asd",
        registration_date=datetime.datetime.now(),
        last_active_date=datetime.datetime.now(),
        name="name",
        birthday=datetime.date.today(),
        about="",
        avatar=None,
    )

    a = Token.generate_access_token(user)
    r = Token.generate_refresh_token(user)

    from src.model import db

    db.db_wrapper.database.close()

    return {"access_token": a, "refresh_token": r}