def direct_userinfo_redirect(request): code = request.GET.get('code', '') state = request.GET.get('state', '') final_state = unquote_state(request, state) CFG = final_cfg(request, final_state) access_info = get_access_info(CFG['appID'], CFG['appsecret'], code) if 'errcode' in access_info: return redirect( get_oauth_redirect_url(final_oauth_uri(request, final_state), 'snsapi_userinfo', final_state, direct_redirect=True)) userinfo = get_userinfo(access_info.get('access_token', ''), access_info.get('openid', '')) if 'openid' not in userinfo: return redirect( get_oauth_redirect_url(final_oauth_uri(request, final_state), 'snsapi_userinfo', final_state, direct_redirect=True)) return redirect( furl(final_state).remove(userinfo.keys()).add(userinfo).url)
def returned_wrapper(request, *args, **kwargs): user_id = request.get_signed_cookie(key, default='', salt=settings.COOKIE_SALT) if not user_id: return redirect( get_oauth_redirect_url(settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_userinfo', request.get_full_path())) return func(request, *args, **kwargs)
def returned_wrapper(request, *args, **kwargs): if not settings.DEBUG and not request.COOKIES.get('user_id'): # 3rd OAuth # return redirect(settings.WECHAT_OAUTH2_REDIRECT_URL) # Current OAuth redirect_url = furl( entry or settings.WECHAT_OAUTH2_REDIRECT_ENTRY).add({}).url return redirect( get_oauth_redirect_url(settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_userinfo', redirect_url)) return func(request, *args, **kwargs)
def direct_base_redirect(request): code = request.GET.get('code', '') state = request.GET.get('state', '') final_state = unquote_state(request, state) CFG = final_cfg(request, final_state) access_info = get_access_info(CFG['appID'], CFG['appsecret'], code) if 'errcode' in access_info: return redirect( get_oauth_redirect_url(final_oauth_uri(request, final_state), 'snsapi_base', final_state, direct_redirect=True)) return redirect( furl(final_state).remove(access_info.keys()).add(access_info).url)
def returned_wrapper(request, *args, **kwargs): if not settings.DEBUG and request.wechat: vtoken = request.GET.get('vtoken', '') or request.POST.get( 'vtoken', '') token_check_key = request.GET.get( settings.TOKEN_CHECK_KEY, '') or request.POST.get( settings.TOKEN_CHECK_KEY, '') if not r.token_exists(token_check_key, vtoken): # 3rd OAuth # return redirect(settings.WECHAT_OAUTH2_REDIRECT_URL) # Current OAuth redirect_url = furl( entry or settings.WECHAT_OAUTH2_REDIRECT_ENTRY).add({}).url return redirect( get_oauth_redirect_url( settings.WECHAT_OAUTH2_REDIRECT_URI, 'snsapi_userinfo', redirect_url)) return func(request, *args, **kwargs)
def userinfo_redirect(request): code = request.GET.get('code', '') state = request.GET.get('state', '') final_state = unquote_state(request, state) CFG = final_cfg(request, final_state) access_info = get_access_info(CFG['appID'], CFG['appsecret'], code) if 'errcode' in access_info: return redirect( get_oauth_redirect_url(final_oauth_uri(request, final_state), 'snsapi_userinfo', final_state)) userinfo = get_userinfo(access_info.get('access_token', ''), access_info.get('openid', '')) if 'openid' not in userinfo: return redirect( get_oauth_redirect_url(final_oauth_uri(request, final_state), 'snsapi_userinfo', final_state)) query_params = {} # Toset Cookie When ``DJANGO_WE_USERINFO_REDIRECT_SET_COOKIE = True`` if hasattr(settings, 'DJANGO_WE_USERINFO_REDIRECT_SET_COOKIE') and getattr( settings, 'DJANGO_WE_USERINFO_REDIRECT_SET_COOKIE'): if hasattr(settings, 'DJANGO_WE_USERINFO_COOKIE_FUNC') and hasattr( settings.DJANGO_WE_USERINFO_COOKIE_FUNC, '__call__'): query_params, cookie_key, cookie_value = settings.DJANGO_WE_USERINFO_COOKIE_FUNC( code, final_state, access_info, userinfo) or {} response = redirect( furl(final_state).remove(userinfo.keys()).add(userinfo).remove( query_params.keys()).add(query_params).url) if hasattr(settings, 'DJANGO_WE_USERINFO_SET_COOKIE_FUNC') and hasattr( settings.DJANGO_WE_USERINFO_SET_COOKIE_FUNC, '__call__'): return settings.DJANGO_WE_USERINFO_SET_COOKIE_FUNC( code, final_state, access_info, userinfo, query_params, cookie_key, cookie_value) max_age = hasattr( settings, 'DJANGO_WE_COOKIE_MAX_AGE') and getattr( settings, 'DJANGO_WE_COOKIE_MAX_AGE') or 30 * 24 * 60 * 60 # 30d cookie_salt = hasattr(settings, 'DJANGO_WE_COOKIE_SALT') and getattr( settings, 'DJANGO_WE_COOKIE_SALT' ) or 'djwe' # Salt for ``set_signed_cookie`` response.set_signed_cookie(cookie_key, cookie_value, salt=cookie_salt, **{ 'max_age': max_age, 'httponly': True, }) return response return render( request, 'django_we/errmsg.html', { 'title': 'Error', 'errmsg': 'DJANGO_WE_USERINFO_COOKIE_FUNC Should Exists' }) if hasattr(settings, 'DJANGO_WE_USERINFO_FUNC') and hasattr( settings.DJANGO_WE_USERINFO_FUNC, '__call__'): query_params = settings.DJANGO_WE_USERINFO_FUNC( code, final_state, access_info, userinfo) or {} return redirect( furl(final_state).remove(userinfo.keys()).add(userinfo).remove( query_params.keys()).add(query_params).url)