def revoke(request): """Revokes access to the Bing Ads accounts of the user authenticated in the current session.""" assert isinstance(request, HttpRequest) try: Users = get_user_model() user = User.objects.get(username=request.user.username) bingadsuser = user.bingadsuser if(bingadsuser != None): bingadsuser.refresh_token = "" bingadsuser.save() except User.DoesNotExist: pass except BingAdsUser.DoesNotExist: pass clear_session_data(request) form = BingAdsPasswordAuthenticationForm() form.username = "" form.password = "" return render( request, 'app/index.html', context_instance = RequestContext(request, { 'form': form, 'year':datetime.now().year, }) )
def revoke(request): """Revokes access to the Bing Ads accounts of the user authenticated in the current session.""" assert isinstance(request, HttpRequest) try: Users = get_user_model() user = User.objects.get(username=request.user.username) bingadsuser = user.bingadsuser if (bingadsuser != None): bingadsuser.refresh_token = "" bingadsuser.save() except User.DoesNotExist: pass except BingAdsUser.DoesNotExist: pass clear_session_data(request) form = BingAdsPasswordAuthenticationForm() form.username = "" form.password = "" return render(request, 'app/index.html', context_instance=RequestContext(request, { 'form': form, 'year': datetime.now().year, }))
def authorize_bing_ads_user(request, authentication, authentication_type, environment): assert isinstance(request, HttpRequest) global customer_service bingadsuser = None try: Users = get_user_model() user = User.objects.get(username=request.user.username) except User.DoesNotExist: user = User.objects.create_user(username=request.user.username, password=password) try: bingadsuser = user.bingadsuser except BingAdsUser.DoesNotExist: bingadsuser = BingAdsUser() bingadsuser.user = user pass # If we have a refresh token let's refresh it if(authentication_type == 'OAuthWebAuthCodeGrant' and bingadsuser != None and bingadsuser.refresh_token != ""): authentication.request_oauth_tokens_by_refresh_token(bingadsuser.refresh_token) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token # If the current HTTP request is a callback from the Microsoft Account authorization server, # use the current request url containing authorization code to request new access and refresh tokens elif (authentication_type == 'OAuthWebAuthCodeGrant' and request.GET.get('code') != None): authentication.request_oauth_tokens_by_response_uri(response_uri = request.get_full_path()) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token # If there is no refresh token saved and no callback from the authorization server, # then connect to the authorization server and request user consent. elif (authentication_type == 'OAuthWebAuthCodeGrant' and bingadsuser.refresh_token == ""): return redirect(authentication.get_authorization_endpoint()) set_session_data(request, authentication, authentication_type, environment) user.save() bingadsuser.save() # At this point even if the user has a valid web application user account, we don't know whether they have access to Bing Ads. # Let's test to see if they can call Bing Ads services, and only let Bing Ads users login to this application. bing_ads_user = None errors=[] try: bing_ads_user = get_user(None) except WebFault as ex: errors=get_webfault_errors(ex) pass form = BingAdsPasswordAuthenticationForm() form.username = "" form.password = "" return render( request, 'app/index.html', context_instance = RequestContext(request, { 'bingadsuser': bing_ads_user, 'errors': errors, 'form': form, 'year':datetime.now().year, }) )
def authorize_bing_ads_user(request, authentication, authentication_type, environment): assert isinstance(request, HttpRequest) global customer_service bingadsuser = None try: Users = get_user_model() user = User.objects.get(username=request.user.username) except User.DoesNotExist: user = User.objects.create_user(username=request.user.username, password=password) try: bingadsuser = user.bingadsuser except BingAdsUser.DoesNotExist: bingadsuser = BingAdsUser() bingadsuser.user = user pass # If we have a refresh token let's refresh it if (authentication_type == 'OAuthWebAuthCodeGrant' and bingadsuser != None and bingadsuser.refresh_token != ""): authentication.request_oauth_tokens_by_refresh_token( bingadsuser.refresh_token) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token # If the current HTTP request is a callback from the Microsoft Account authorization server, # use the current request url containing authorization code to request new access and refresh tokens elif (authentication_type == 'OAuthWebAuthCodeGrant' and request.GET.get('code') != None): authentication.request_oauth_tokens_by_response_uri( response_uri=request.get_full_path()) bingadsuser.refresh_token = authentication.oauth_tokens.refresh_token # If there is no refresh token saved and no callback from the authorization server, # then connect to the authorization server and request user consent. elif (authentication_type == 'OAuthWebAuthCodeGrant' and bingadsuser.refresh_token == ""): return redirect(authentication.get_authorization_endpoint()) set_session_data(request, authentication, authentication_type, environment) user.save() bingadsuser.save() # At this point even if the user has a valid web application user account, we don't know whether they have access to Bing Ads. # Let's test to see if they can call Bing Ads services, and only let Bing Ads users login to this application. bing_ads_user = None errors = [] try: bing_ads_user = get_user(None) except WebFault as ex: errors = get_webfault_errors(ex) pass form = BingAdsPasswordAuthenticationForm() form.username = "" form.password = "" return render(request, 'app/index.html', context_instance=RequestContext( request, { 'bingadsuser': bing_ads_user, 'errors': errors, 'form': form, 'year': datetime.now().year, }))