Example #1
0
    def new_func(request,*args,**argw):
        # 用户在进行账号绑定时,需要与开放平台进行验证,主要验证access_token以及openid
        access_token = request.REQUEST.get('access_token','')
        openid = request.REQUEST.get('openid','')
        platform = request.REQUEST.get('platform','')
        bind_access_token = request.REQUEST.get('bind_access_token','')
        bind_openid = request.REQUEST.get('bind_openid','')

        if not access_token or not openid or not platform or not bind_access_token or not bind_openid:
            data = {'rc':6,'data':{'msg':get_msg('login','platform_overdue'),'server_now':int(time.time())}}
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        if platform == 'qq':
            result,pid,msg = auth_bind_for_qq(request,access_token,openid,bind_access_token,bind_openid)
        elif platform == 'fb':
            result,pid,msg = auth_bind_for_fb(request,access_token,openid,bind_access_token,bind_openid)
        else:
            result,pid,msg = auth_bind_for_sina(request,access_token,openid,bind_access_token,bind_openid)
        if not result:
            data = {'rc':11,'data':{'msg':msg,'server_now':int(time.time())}}
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        else:
            Session.set(platform, pid)
        result = func(request,*args,**argw)
        return result
Example #2
0
def get_access_token(request):
    #用于刷新access_token用的refresh token
    data = {
            'rc':0,
            'data':{}
            }
    para_pid = request.REQUEST.get('pid',None)
    para_platform = request.REQUEST.get('platform',None)
    session_dic = Session.new_get(para_platform+':'+para_pid)
    pid = session_dic['pid']
    access_token = session_dic['access_token']
    refresh_token = session_dic['refresh_token']
    expires_time = session_dic['expires_time']
    if not pid and not access_token and not refresh_token and not expires_time:
        data = {
                'rc':8,
                'data':{
                      'msg':get_msg('login','server_exception'),
                      'server_now':int(time.time()),
                      }
                }
        return HttpResponse(
            json.dumps(data, indent=1),
            content_type='application/x-javascript',
        )

    else:
        if expires_time > time.time():
            data['data']['access_token'] = access_token
            data['data']['pid'] = request.rk_user.account.openid
            data['data']['uid'] = request.rk_user.uid
            data['data']['nickname'] = request.rk_user.baseinfo['username']
        else:
            client_id = settings.APP_KEY_360
            client_secret = settings.APP_SECRET_KEY_360
            oauth2_url = "https://openapi.360.cn/oauth2/access_token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s&scope=basic" %(refresh_token, client_id, client_secret)
            url_request = urllib2.urlopen(oauth2_url, timeout=12)
            code, res = url_request.code, url_request.read()
            if code == 200:
                res_dict = json.loads(res)
                data['data']['access_token'] = str(res_dict['access_token'])
                data['data']['pid'] = request.rk_user.account.openid
                data['data']['uid'] = request.rk_user.uid
                data['data']['nickname'] = request.rk_user.baseinfo['username']
                expires_time = time.time() + float(res_dict['expires_in'])
                Session.set(para_platform, pid, str(res_dict['access_token']), str(res_dict['refresh_token']), expires_time)
            else:
                data = {
                        'rc':8,
                        'data':{
                              'msg':get_msg('login','server_exception'),
                              'server_now':int(time.time()),
                              }
                        }
    data['data']['server_now'] = int(time.time())
    return HttpResponse(
        json.dumps(data, indent=1),
        content_type='application/x-javascript',
    )
Example #3
0
def auth_token_for_360(request, access_token, openid, uuid, mktid, version,
                       client_type, macaddr, idfa, ios_ver):
    #print '##### in auth_token_for_360'
    fg = False
    pid = ''
    subarea = request.REQUEST.get("subarea", "1")

    #360平台验证
    if not 'access_token' in request.REQUEST:
        return fg, pid
    else:
        access_token = str(request.REQUEST['access_token'])

    platform = str(request.REQUEST['platform'])
    if access_token:
        url_360 = 'https://openapi.360.cn/user/me.json'
        code_url = '%s?access_token=%s&fields=id,name,avatar,sex,area' % (
            url_360, access_token)
        url_request = urllib2.urlopen(code_url, timeout=12)
        code, res = url_request.code, url_request.read()

        #print '#### 360, code, res=', code, res
        #可能360不需要refresh_token ?
        #refresh_token = str(request.REQUEST['refresh_token'])
        refresh_token = ''
        #print '#### refresh_token=', refresh_token
        #expires_in = float(request.REQUEST['expires_in'])  # "['123.45']" ?
        #print '#### expires_in=', request.REQUEST['expires_in']
        expires_in = 24 * 3600

        if code == 200:
            res_dict = json.loads(res)
            #print '#### 360, res_dict=', res_dict

            fg = True
            openid = str(res_dict['id'])
            pid = md5.md5('360' + openid).hexdigest()
            request.rk_user = UserBase._install(pid,
                                                platform,
                                                uuid,
                                                mktid,
                                                version,
                                                client_type,
                                                macaddr,
                                                idfa,
                                                ios_ver,
                                                subarea=subarea)
            #检查用户是否账户被冻结
            if not request.rk_user.frozen:
                #更新用户的openid和access_token
                request.rk_user.account.update_info(openid, access_token)
                request.rk_user.update_user_from_360(res_dict)
                expires_time = time.time() + expires_in

                #print '##### 360 start set session'
                Session.set(platform, pid, access_token, refresh_token,
                            expires_time)
    return fg, pid
Example #4
0
    def new_func(request, *args, **argw):
        para_pid = request.REQUEST.get('pid', None)
        para_platform = request.REQUEST.get('platform', None)

        session_overdue = False
        if para_platform is None or para_pid is None:
            session_overdue = True
        else:
            platform, pid = Session.get("{}:{}".format(para_platform, para_pid))
            if not platform or not pid or platform != para_platform or para_pid != pid:
                session_overdue = True

        #session过期
        if session_overdue:
            data = {
                'rc': 8,
                'data': {
                    'msg': get_msg('login', 'server_exception'),
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        result = func(request, *args, **argw)
        return result
Example #5
0
    def new_func(request, *args, **argw):
        para_pid = request.REQUEST.get('pid', None)
        para_platform = request.REQUEST.get('platform', None)

        session_overdue = False
        if para_platform is None or para_pid is None:
            session_overdue = True
        else:
            platform, pid = Session.get("{}:{}".format(para_platform,
                                                       para_pid))
            if not platform or not pid or platform != para_platform or para_pid != pid:
                session_overdue = True

        #session过期
        if session_overdue:
            data = {
                'rc': 8,
                'data': {
                    'msg': get_msg('login', 'server_exception'),
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        result = func(request, *args, **argw)
        return result
Example #6
0
def auth_token_for_360(request,access_token,openid,uuid,mktid,version,client_type,macaddr,idfa,ios_ver):
    #print '##### in auth_token_for_360'
    fg = False
    pid = ''
    subarea = request.REQUEST.get("subarea", "1")

    #360平台验证
    if not 'access_token' in request.REQUEST:
        return fg, pid
    else:
        access_token = str(request.REQUEST['access_token'])

    platform = str(request.REQUEST['platform'])
    if access_token:
        url_360 = 'https://openapi.360.cn/user/me.json'
        code_url = '%s?access_token=%s&fields=id,name,avatar,sex,area' % (url_360, access_token)
        url_request = urllib2.urlopen(code_url, timeout=12)
        code, res = url_request.code, url_request.read()

        #print '#### 360, code, res=', code, res
        #可能360不需要refresh_token ?
        #refresh_token = str(request.REQUEST['refresh_token'])
        refresh_token = ''
        #print '#### refresh_token=', refresh_token
        #expires_in = float(request.REQUEST['expires_in'])  # "['123.45']" ?
        #print '#### expires_in=', request.REQUEST['expires_in']
        expires_in = 24*3600

        if code == 200:
            res_dict = json.loads(res)
            #print '#### 360, res_dict=', res_dict

            fg = True
            openid = str(res_dict['id'])
            pid = md5.md5('360' + openid).hexdigest()
            request.rk_user = UserBase._install(pid, platform,uuid,mktid,version,client_type,macaddr,idfa,ios_ver, subarea=subarea)
            #检查用户是否账户被冻结
            if not request.rk_user.frozen:
                #更新用户的openid和access_token
                request.rk_user.account.update_info(openid,access_token)
                request.rk_user.update_user_from_360(res_dict)
                expires_time = time.time() + expires_in

                #print '##### 360 start set session'
                Session.set(platform, pid, access_token, refresh_token, expires_time)
    return fg,pid
Example #7
0
    def new_func(request, *args, **argw):
        # 用户在进行账号绑定时,需要与开放平台进行验证,主要验证access_token以及openid
        access_token = request.REQUEST.get('access_token', '')
        openid = request.REQUEST.get('openid', '')
        platform = request.REQUEST.get('platform', '')
        bind_access_token = request.REQUEST.get('bind_access_token', '')
        bind_openid = request.REQUEST.get('bind_openid', '')

        if not access_token or not openid or not platform or not bind_access_token or not bind_openid:
            data = {
                'rc': 6,
                'data': {
                    'msg': get_msg('login', 'platform_overdue'),
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        if platform == 'qq':
            result, pid, msg = auth_bind_for_qq(request, access_token, openid,
                                                bind_access_token, bind_openid)
        elif platform == 'fb':
            result, pid, msg = auth_bind_for_fb(request, access_token, openid,
                                                bind_access_token, bind_openid)
        else:
            result, pid, msg = auth_bind_for_sina(request, access_token,
                                                  openid, bind_access_token,
                                                  bind_openid)
        if not result:
            data = {
                'rc': 11,
                'data': {
                    'msg': msg,
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        else:
            Session.set(platform, pid)
        result = func(request, *args, **argw)
        return result
Example #8
0
    def new_func(request, *args, **argw):
        access_token = request.REQUEST.get('access_token', '')
        openid = request.REQUEST.get('openid', '')
        platform = request.REQUEST.get('platform', '')
        uuid = request.REQUEST.get("uuid", "")
        mktid = request.REQUEST.get("mktid", "")
        version = request.REQUEST.get("version", "1.0")
        client_type = request.REQUEST.get("client_type", "")
        # ios5以前用mac地址,ios6以后的用idfa
        macaddr = request.REQUEST.get("macaddr", "")
        idfa = request.REQUEST.get("idfa", "")
        ios_ver = request.REQUEST.get("ios_ver", "")

        if platform == 'oc':
            result,pid,msg = auth_token_for_oc(request, access_token, openid, uuid,
                                               mktid, version, client_type,macaddr,
                                               idfa, ios_ver)
            if not result:
                #print '##### failed to auth_token_for_oc, result, pid, msg=', result, pid, msg
                data = {
                    'rc': 3,
                    'data': {
                        'msg': msg,
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )
        else:
            result = False

            # 除360外, 必须需要 access_token, openid, platform
            # 2014/10/22: 现在 360, 只给 access_token ?
            if platform != '360' and (not access_token or not openid or not platform):
                #print '#### platform_auth, rc: 6'
                data = {
                    'rc': 6,    #缺参数
                    'data': {
                        'msg': get_msg('login', 'platform_overdue'),
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )
            
            auth_function = globals().get("auth_token_for_" + platform)

            if auth_function:
                result,pid = auth_function(request, access_token, openid, uuid,
                                           mktid, version, client_type,
                                           macaddr, idfa, ios_ver)

        if not result:
            #print '##### failed to auth_function, result, pid=', result, pid
            data = {
                'rc': 3,
                'data': {
                    'msg': get_msg('login', 'platform_overdue'),
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        # 验证成功
        else:
            #检查用户是否处于冻结状态
            frozen_msg = get_frozen_msg(request.rk_user)
            if frozen_msg:
                data = {
                    'rc': 10,
                    'data': {
                        'msg': frozen_msg,
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )

            if platform != '360':
                Session.set(platform, pid)

        result = func(request, *args, **argw)
        return result
Example #9
0
    def new_func(request, *args, **argw):
        access_token = request.REQUEST.get('access_token', '')
        openid = request.REQUEST.get('openid', '')
        platform = request.REQUEST.get('platform', '')
        uuid = request.REQUEST.get("uuid", "")
        mktid = request.REQUEST.get("mktid", "")
        version = request.REQUEST.get("version", "1.0")
        client_type = request.REQUEST.get("client_type", "")
        # ios5以前用mac地址,ios6以后的用idfa
        macaddr = request.REQUEST.get("macaddr", "")
        idfa = request.REQUEST.get("idfa", "")
        ios_ver = request.REQUEST.get("ios_ver", "")

        if platform == 'oc':
            result, pid, msg = auth_token_for_oc(request, access_token, openid,
                                                 uuid, mktid, version,
                                                 client_type, macaddr, idfa,
                                                 ios_ver)
            if not result:
                #print '##### failed to auth_token_for_oc, result, pid, msg=', result, pid, msg
                data = {
                    'rc': 3,
                    'data': {
                        'msg': msg,
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )
        else:
            result = False

            # 除360外, 必须需要 access_token, openid, platform
            # 2014/10/22: 现在 360, 只给 access_token ?
            if platform != '360' and (not access_token or not openid
                                      or not platform):
                #print '#### platform_auth, rc: 6'
                data = {
                    'rc': 6,  #缺参数
                    'data': {
                        'msg': get_msg('login', 'platform_overdue'),
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )

            auth_function = globals().get("auth_token_for_" + platform)

            if auth_function:
                result, pid = auth_function(request, access_token, openid,
                                            uuid, mktid, version, client_type,
                                            macaddr, idfa, ios_ver)

        if not result:
            #print '##### failed to auth_function, result, pid=', result, pid
            data = {
                'rc': 3,
                'data': {
                    'msg': get_msg('login', 'platform_overdue'),
                    'server_now': int(time.time())
                }
            }
            return HttpResponse(
                json.dumps(data, indent=1),
                content_type='application/x-javascript',
            )
        # 验证成功
        else:
            #检查用户是否处于冻结状态
            frozen_msg = get_frozen_msg(request.rk_user)
            if frozen_msg:
                data = {
                    'rc': 10,
                    'data': {
                        'msg': frozen_msg,
                        'server_now': int(time.time())
                    }
                }
                return HttpResponse(
                    json.dumps(data, indent=1),
                    content_type='application/x-javascript',
                )

            if platform != '360':
                Session.set(platform, pid)

        result = func(request, *args, **argw)
        return result