Ejemplo n.º 1
0
    async def qn_callback(self):
        """
        七牛回调
        :return:
        """
        from lib import qn
        ua = self.headers.get('User-Agent', None)
        if not (ua and ua.startswith('qiniu-callback')):
            return self.finish(RETCODE.FAILED)

        auth = self.headers.get('Authorization', None)
        if auth:
            content = str(await self._request.content.read(), 'utf-8')
            if qn.verify_callback(auth, str(self._request.url), content):
                # 鉴权成功,确认为七牛服务器回调
                info = json.loads(content)
                uid = binhex.to_bin(info['user_id'])
                # 说明一下,这个哈希值不是base64 hex等编码,具体比较奇怪看了就知道了
                # 总之因此直接使用了TextField来存放
                key = info['key']
                UploadModel.new(uid, key, info['size'], info['ext'],
                                info['type_name'], info['image_info'])
                if info['type_name'] == 'avatar':
                    # 更换用户头像
                    u = UserModel.get_by_pk(uid)
                    if u:
                        u.avatar = key
                        u.save()
                return self.finish(RETCODE.SUCCESS, key)

        self.finish(RETCODE.FAILED)
Ejemplo n.º 2
0
    async def get_user_data(self):
        code = self.params
        print(code)
        code = code['code']
        if code == 'undefined':
            self.finish(RETCODE.FAILED)
            return
        otoken, _ = await self.github.get_access_token(code)
        github = GithubClient(
            client_id=config.CLIENT_ID,
            client_secret=config.CLIENT_SECRET,
            access_token=otoken,
        )
        response = await github.request('GET', 'user')
        # response = json.loads(response)
        if response['id']:
            try:
                account = UserOAuth.get(UserOAuth.login_id == response['id'],
                                        UserOAuth.platform == 'github')
            except UserOAuth.DoesNotExist:
                account = None

            if account:
                if account.user_id:  # 返回用户已有信息
                    u = UserModel.get_by_pk(account.user_id)
                    if u:
                        expires = 30
                        u.refresh_key()
                        self.setup_user_key(u.key, expires)
                        self.finish(
                            RETCODE.SUCCESS, {
                                'oauthcode': 0,
                                'user_id': account.user_id,
                                'state': account.state,
                                'access_token': u.key
                            })
                else:
                    self.finish(
                        RETCODE.SUCCESS, {
                            'oauthcode': 1,
                            'state': account.state,
                            'oauth_id': account.id,
                            'login_id': account.login_id,
                            'platform': account.platform
                        })
            else:
                ins = [{
                    'login_id': response['id'],
                    'time': time.time(),
                    'platform': 'github',
                    'state': POST_STATE.APPLY
                }]
                if not isinstance(config.LONG_ID_GENERATOR,
                                  config.SQLSerialGenerator):
                    ins[0]['id'] = config.LONG_ID_GENERATOR().to_bin()

                UserOAuth.insert_many(ins).execute()
                self.finish(
                    RETCODE.SUCCESS, {
                        'oauthcode': 1,
                        'oauth_id': ins[0]['id'],
                        'state': ins[0]['state'],
                        'login_id': ins[0]['login_id'],
                        'platform': ins[0]['platform']
                    })
        else:
            self.finish(RETCODE.NOT_FOUND)
Ejemplo n.º 3
0
 def get_user_by_token(self: Union['BaseUserViewMixin', 'BaseView'],
                       token) -> Type[BaseUser]:
     t = UserToken.get_by_token(token)
     if t: return UserModel.get_by_pk(t.user_id)