Example #1
0
 def delete_identity(user_id, type):
     '''删除时,需要判断type的类型
        因为username、mobile、email的解绑需要清除user表的对应字段
     '''
     user = User.get(id=user_id)
     with db.auto_commit():
         # 判断是否是网站内部的身份类型
         if ClientTypeEnum(
                 type) in current_app.config['CLINET_INNER_TYPES']:
             attr = ClientTypeEnum(type).name.lower()
             setattr(user, attr, None)
             user.save(commit=False)
         identity = Identity.get_or_404(user_id=user_id, type=type)
         identity.hard_delete(commit=False)  # 硬删除
Example #2
0
def get_token():
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: Voter.verify,
    }
    identity = promise[ClientTypeEnum(100)](form.account.data,
                                            form.secret.data)
    # Token
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(
        identity['uid'],
        # form.type.data,
        identity['scope'],
        expiration)
    if identity['scope'] == 'UserScope':
        scope = 1
    elif identity['scope'] == 'AdminScope':
        scope = 2
    elif identity['scope'] == 'CmsScope':
        scope = 3
    t = {
        'code': 200,
        'token': token.decode('ascii'),
        'scope': scope,
        'msg': 'ok'
    }
    return jsonify(t), 200
Example #3
0
def get_token():
    form = UserMinaForm().validate_for_api()

    promise = {
        ClientTypeEnum.USER_MINA: get_openId,
    }
    identity = promise[ClientTypeEnum(form.type.data)](
        form.code.data
    )
    pid_reuslt = User.is_exist_pid(identity['openId'])
    if not pid_reuslt:
        User.register_by_mina(identity['openId'])

    Result = User.verify(identity['openId'])

    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(Result['uid'],
                                identity['openId'],
                                form.type.data,
                                Result['scope'],
                                expiration)
    t = {
        'token': token.decode('ascii')
    }
    # 序列化一下
    return jsonify(t), 201
Example #4
0
 def validate_type(self,value):
     #尝试把客户传递过来数字转换为枚举类型
     try:
         client = ClientTypeEnum(value.data)  #解决了表单提交的数据类型的转换确认
     except ValueError as e:
         raise e
     self.type.data = client   # 这里把枚举类型对象client 赋值给type,后面用type就可以直接用到枚举类型呢
Example #5
0
 def validate_type(self, value):
     try:
         client = ClientTypeEnum(value.data)
     except Exception as e:
         raise e
         
     self.type.data = client
Example #6
0
 def validate_type(self, value):
     try:
         client = ClientTypeEnum(value.data)
         self.type.data = client
     except ValueError as e:
         print(e)
         raise e
Example #7
0
 def create_identity(user_id, identifier, credential, type):
     # 判断是否是网站内部的身份类型
     if ClientTypeEnum(type) in current_app.config['CLINET_INNER_TYPES']:
         with db.auto_commit():
             user = User.get(id=user_id)
             attr = ClientTypeEnum(type).name.lower()
             setattr(user, attr, identifier)
             Identity.create(commit=False,
                             user_id=user_id,
                             type=type,
                             identifier=identifier,
                             credential=credential)
             user.save(commit=False)
     # 第三方平台,则无需修改用户信息
     else:
         Identity.create(user_id=user_id, type=type, identifier=identifier)
Example #8
0
 def validate_type(self, value):
     try:
         client = ClientTypeEnum(value.data)
         #数字转化为枚举类型,指代不同的注册方式
     except ValueError as e:
         raise e
     self.type.data = client
Example #9
0
 def validate_type(self, value):
     try:
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     # 把转换成枚举类型的数据赋值给账户类型
     self.type.data = client
Example #10
0
 def validate_sign_type(self, value):
     # 验证登录类型
     try:
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     self.sign_type.data = client
Example #11
0
 def validate_type(self, value):
     try:
         # 枚举转换
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     self.type.data = client
Example #12
0
def get_token():
    """
    :return: 返回加密后的Token和一个HTTP状态码
    """
    # api的get_token就相当于web的login
    # 接收用户的参数
    form = ClientForm().validate_for_api()

    promise = {
        ClientTypeEnum.USER_EMAIL: User.verify
    }

    identity = promise[ClientTypeEnum(form.type.data)](
        form.account.data,
        form.secret.data
    )

    # 生成Token
    token = generate_auth_token(identity['uid'],
                                form.type.data,
                                identity['scope'],
                                expiration=current_app.config['TOKEN_EXPIRATION'])
    # 将Token字节码转换为ascii码
    t = {
        'token': token.decode('ascii'),
        'uid': identity['uid']
    }

    return jsonify(t), 201
Example #13
0
def create_client():
    """
    :return:
    客户端 注册 """
    # 校验客户端的数据, 使用app.validators.forms.py
    # data = request.json     # 接受数据, 必须是data=data
    # data = request.args.to_dict()

    # form = ClientForm(data=data)    # 使用自定义的校验ClientForm校验数据

    # if form.validate():
    #     promise = {
    #         ClientTypeEnum.USER_EMAIL: __register_user_by_email
    #     }
    #     # print(form.account.data)
    #     # print(form.secret.data)
    #     # print(form.type.data)     #  100
    #     promise[ClientTypeEnum(form.type.data)]()           # 调用相应的方法
    # else:
    #     print(form.errors)
    #     raise ClientTypeError()

    form = ClientForm().validate_for_api()

    promise = {ClientTypeEnum.USER_EMAIL: __register_user_by_email}
    promise[ClientTypeEnum(form.type.data)]()

    return Success()
Example #14
0
def get_token():
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: User.verify
    }

    #验证身份通过,也成功拿到用户的id
    identity = promise[ClientTypeEnum(form.type.data)](
        form.account.data,
        form.secret.data
    )

    #下一步就是生成令牌
    #令牌是需要加密的,会涉及到加密的算法,flask自带了一个 itsdangerous 库,方便我们去生成令牌


    #expiration = current_app.config['TOKEN_EXPIRATION'],   #多了逗号坑人..返回就是元组了..
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(
        identity['uid'],
        form.type.data,
        identity['scope'],
        expiration
    )

    t = {
        "token":token.decode('ascii') #这不是普通的字符串,还需要decode下
    }
    return jsonify(t),201
Example #15
0
 def validate_type(self, value):
     try:
         # value从请求传过来,value是IntergerField类型,value.data取值
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     # type是IntegerField类型,client是一个实际的值,因此要self.type.data
     self.type.data = client
Example #16
0
 def validate_client_type(self, value):
     try:
         if type(value.data) == str:
             value.data = int(value.data)
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     self.client_type.data = client
Example #17
0
 def password(self, raw):
     # 站内登录方式(用户名、手机、邮箱)的密码需要加密
     if ClientTypeEnum(
             self.type) in current_app.config['CLINET_INNER_TYPES']:
         self._credential = generate_password_hash(raw)
     # 第三方应用的token
     else:
         self._credential = raw
Example #18
0
 def validate_type(self, value):
     try:
         client = ClientTypeEnum(value.data)
         print("client")
         print(client)
     except ValueError as e:
         raise e
     self.type.data = client
Example #19
0
 def validate_type(self, value):
     try:
         # 判断用户传过来的数字是否是枚举类型的一种】
         # 将数字转换为枚举类型,可读性强
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     self.type.data = client
Example #20
0
    def validate_type(self, value):
        try:
            # 如果不能成功转为枚举类型,说明参数是无效的
            client_type = ClientTypeEnum(value.data)
        except ValueError as e:
            raise e

        self.type.data = client_type
Example #21
0
def get_token():
    form = ClientForm().validate_for_api()
    promise = {ClientTypeEnum.USER_EMAIL: User.verify_by_email}
    identity = promise[ClientTypeEnum(form.type.data)](form.account.data,
                                                       form.secret.data)
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(identity['uid'], form.type.data,
                                identity['scope'], expiration)
    return jsonify({'token': token.decode('ascii')}), 201
Example #22
0
 def validate_type(self, field):
     """
         对type进行自定义验证器
         :return:
     """
     try:
         client = ClientTypeEnum(field.data)
     except ValueError as e:
         raise e
     self.type.data = client
Example #23
0
    def validate_type(self, value):
        try:
            # 将用户传来的参数去枚举类中匹配,如果匹配失败,则抛出异常
            # 如果匹配成功则将int转换成枚举
            client = ClientTypeEnum(value.data)  # value.data 取到值
        except ValueError as e:
            raise e

    # 面向对象的继承特性,减少代码量,ClientForm是很有必要存在的
        self.type.data = client  # 将枚举赋值给 type.data
Example #24
0
 def validate_type(self, value):
     """
     自定义type验证器
     :param value: 将传递过来的数字转换为枚举,再将枚举赋值给type
     :return:
     """
     try:
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
     self.type.data = client
Example #25
0
def get_token():
    form = ClientForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: manager.user_model.verify,
        ClientTypeEnum.USER_MINA: manager.user_model.verify_mina
    }
    user = promise[ClientTypeEnum(form.type.data)](form.account.data,
                                                   form.secret.data)

    access_token, refresh_token = get_tokens(user)
    return json_res(access_token=access_token, refresh_token=refresh_token)
Example #26
0
def get_token():
    form = ClientForm().validate_for_api()
    promise = {ClientTypeEnum.USER_EMAIL: User.verify}
    identity = promise[ClientTypeEnum(form.type.data)](form.account.data,
                                                       form.secret.data)
    # Token
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(identity['uid'], form.type.data,
                                identity['scope'], expiration)
    t = {'token': token.decode('ascii')}  # 转化字符串类型
    # return jsonify(t), 201
    return SuccessViewModel.redict(t)
Example #27
0
 def validate_type(self, value):
     '''
     自定义验证器
     :param value:
     :return:
     '''
     try:
         # 将用户传来的参数去枚举类中匹配,如果匹配失败,则抛出异常
         # 如果匹配成功则将int转换成枚举
         client = ClientTypeEnum(value.data)
     except ValueError as e:
         raise e
Example #28
0
def get_token():
    form = ClientValidator().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: User.verify,
    }
    identity = promise[ClientTypeEnum(form.type.data)](form.account.data,
                                                       form.secret.data)
    # Token生成
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(identity['uid'], form.type.data,
                                identity['scope'], expiration)
    t = {'token': token.decode('ascii')}
    return Success(data=t)
Example #29
0
def get_token():
    form = MinaLoginForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_EMAIL: Flusers.verify,
        ClientTypeEnum.USER_MINA: Flusers.mina_verify,
        ClientTypeEnum.USER_WX: Flusers.wx_verify
    }

    identify = promise[ClientTypeEnum(form.type.data)](form.code.data)
    expires_in = current_app.config['EXPIRES_IN']
    token = generate_auth_token(identify['uid'], form.type.data,
                                identify['scope'], expires_in)
    t = {'token': token.decode('ascii')}
    return jsonify(t), 201
Example #30
0
def get_mina_token():
    """获取小程序的token"""
    form = UserMinaForm().validate_for_api()
    promise = {
        ClientTypeEnum.USER_MINA: User.mina_verify,
        ClientTypeEnum.USER_EMAIL: User.verify
    }
    mina_code = form.code.data
    identify = promise[ClientTypeEnum(form.type.data)](mina_code)
    expiration = current_app.config['TOKEN_EXPIRATION']
    token = generate_auth_token(identify['uid'], form.type.data,
                                identify['scope'], expiration)
    t = {'token': token.decode('ascii')}
    return jsonify(t), 201