Beispiel #1
0
def sign_out():
    '''
    GET or PUT:
        用户登出api
        use_jwt_auth:<int>, 是否使用jwt验证. 0 或 1,默认为0不使用.
                     如果是jwt验证登录信息的客户端use_jwt_auth应为1
        :param adm:
        :return:
    '''

    use_jwt_auth = str_to_num(request.argget.all('use_jwt_auth', 0))
    if use_jwt_auth:

        # 使用jwt验证的客户端登出
        jwt_auth = JwtAuth()
        s, r = jwt_auth.clean_login()
        if s:
            data = {
                "msg": gettext("Successfully logged out"),
                "msg_type": "s",
                "http_status": 201,
                "to_url": get_config("login_manager", "LOGIN_OUT_TO")
            }
        else:
            data = {"msg": r, "msg_type": "s", "http_status": 400}
    else:
        logout_user()

        data = {
            "msg": gettext("Successfully logged out"),
            "msg_type": "s",
            "http_status": 201,
            "to_url": get_config("login_manager", "LOGIN_OUT_TO")
        }
    return response_format(data)
Beispiel #2
0
def p_sign_in(username, password, code_url_obj, code, remember_me, use_jwt_auth=0):

    '''
    用户登录函数
    :param adm:
    :return:
    '''
    data = {}
    if current_user.is_authenticated and username in [current_user.username,
                                                      current_user.email,
                                                      current_user.mphone_num]:
        data['msg'] = gettext("Is logged in")
        data["msg_type"] = "s"
        data["http_status"] = 201
        data['to_url'] = request.argget.all('next') or get_config("login_manager", "LOGIN_IN_TO")
        return data

    # name & pass
    s, r = email_format_ver(username)
    s2, r2 = mobile_phone_format_ver(username)
    if s:
        user = mdb_user.db.user.find_one({"email":username})
    elif s2:
        user = mdb_user.db.user.find_one({"mphone_num": username})
    else:
        user = mdb_user.db.user.find_one({"username":username})
    if not user:
        data = {"msg":gettext("Account or password error"), "msg_type":"e", "http_status":401}
        return data

    user = User(user["_id"])

    # 判断是否多次密码错误,是就要验证图片验证码
    user_p = mdb_user.db.user_login_log.find_one({'user_id':user.str_id})
    PW_WRONG_NUM_IMG_CODE = get_config("login_manager", "PW_WRONG_NUM_IMG_CODE")
    if user_p and 'pass_error' in user_p and user_p['pass_error'] >= PW_WRONG_NUM_IMG_CODE:
        # 图片验证码验证
        r = verify_image_code(code_url_obj, code)
        if not r:

            data["open_img_verif_code"] = True
            data['msg'] = gettext("Verification code error")
            data["msg_type"] = "e"
            data["http_status"] = 401
            return data

    # 密码验证
    if user and user.verify_password(password) and not user.is_delete:
        if user.is_active:
            if use_jwt_auth:
                # 使用的时jwt验证
                # 获取token
                jwt_auth = JwtAuth()
                data["auth_token"] = jwt_auth.get_login_token(user)
                client = "app"
            else:
                login_user(user, remember_me)
                client = "browser"
            # 记录登录日志
            login_log(user, client)

            data['msg'] = gettext("Sign in success")
            data["msg_type"] = "s"
            data["http_status"] = 201
            data["to_url"] = request.argget.all('next') or get_config("login_manager", "LOGIN_IN_TO")
            return data

        # 未激活
        data['msg'] = gettext("Account is inactive or frozen")
        data["msg_type"] = "w"
        data["http_status"] = 401

    else:
        # 密码错误
        mdb_user.db.user_login_log.update_one({'user_id':user.str_id},
                                              {"$inc":{"pass_error":1}},
                                              upsert=True)

        # 判断是否多次密码错误
        if user_p and 'pass_error' in user_p and user_p['pass_error'] >= PW_WRONG_NUM_IMG_CODE:
            # 图片验证码验证码
            data["open_img_verif_code"] = True
        data['msg'] = gettext("Account or password error")
        data["msg_type"] = "e"
        data["http_status"] = 401
    return data
Beispiel #3
0
#!/usr/bin/env python
# -*-coding:utf-8-*-
# @Time : 2017/11/1 ~ 2019/9/1
# @Author : Allen Woo
from apps.app import login_manager
from apps.core.auth.jwt_auth import JwtAuth
from apps.modules.user.process.user import User, AnonymousUser
# JWT验证
jwt_auth = JwtAuth()


@login_manager.user_loader
def load_user(user_id):
    """
    当检测到用户已登录时回调此函数(登录针对非BearerToken验证用户的客户端,如普通浏览器)
    :param user_id:
    :return:用户实例
    """
    user = User(user_id)
    return user


@login_manager.request_loader
def load_user_req(request):
    """
    当检测到用户未登录时回调此函数(未登录针对的是cookie session的客户端,如普通浏览器)
    如果客户端使用的是api携带登录token, 则进行验证后返回对应用户信息
    :param request:
    :return:
    """
    s, user = jwt_auth.user_identify()