def index(request): """ Controller for the index/landing page. """ if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): # Get Production or Sandbox value environment = login_form.cleaned_data['environment'] # URL to send login request oauth_url = 'https://login.salesforce.com/services/oauth2/authorize' if environment == 'Sandbox': oauth_url = 'https://test.salesforce.com/services/oauth2/authorize' # Set up URL based on Salesforce Connected App details oauth_url = oauth_url + '?response_type=code&client_id=' + settings.SALESFORCE_CONSUMER_KEY + '&redirect_uri=' + settings.SALESFORCE_REDIRECT_URI + '&state=' + environment # Re-direct to login page return HttpResponseRedirect(oauth_url) else: login_form = LoginForm() return render_to_response( 'index.html', RequestContext(request, {'login_form': login_form}))
def index(request): """ Controller for the index/landing page. """ if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): # Get Production or Sandbox value environment = login_form.cleaned_data['environment'] # URL to send login request oauth_url = 'https://login.salesforce.com/services/oauth2/authorize' if environment == 'Sandbox': oauth_url = 'https://test.salesforce.com/services/oauth2/authorize' # Set up URL based on Salesforce Connected App details oauth_url = oauth_url + '?response_type=code&client_id=' + settings.SALESFORCE_CONSUMER_KEY + '&redirect_uri=' + settings.SALESFORCE_REDIRECT_URI + '&state='+ environment # Re-direct to login page return HttpResponseRedirect(oauth_url) else: login_form = LoginForm() return render_to_response('index.html', RequestContext(request,{'login_form': login_form}))
def oauth_response(request): """ Controller for the oauth_response page. """ # Default variables error_exists = False error_message = '' username = '' org_name = '' org_id = '' email = '' # On page load if request.GET: # Get OAuth response values oauth_code = request.GET.get('code') environment = request.GET.get('state') access_token = '' instance_url = '' if 'Sandbox' in environment: login_url = 'https://test.salesforce.com' else: login_url = 'https://login.salesforce.com' # Log in to REST API to obtain access token r = requests.post( login_url + '/services/oauth2/token', headers={'content-type': 'application/x-www-form-urlencoded'}, data={ 'grant_type': 'authorization_code', 'client_id': settings.SALESFORCE_CONSUMER_KEY, 'client_secret': settings.SALESFORCE_CONSUMER_SECRET, 'redirect_uri': settings.SALESFORCE_REDIRECT_URI, 'code': oauth_code }) # Load JSON response auth_response = json.loads(r.text) # If login error - return error for user if 'error_description' in auth_response: error_exists = True error_message = auth_response['error_description'] # Otherwise get session details else: access_token = auth_response['access_token'] instance_url = auth_response['instance_url'] user_id = auth_response['id'][-18:] org_id = auth_response['id'][:-19] org_id = org_id[-18:] # get username of the authenticated user r = requests.get( instance_url + '/services/data/v' + str(settings.SALESFORCE_API_VERSION) + '.0/sobjects/User/' + user_id + '?fields=Username,Email', headers={'Authorization': 'OAuth ' + access_token}) query_response = json.loads(r.text) if 'Username' in query_response: username = query_response['Username'] if 'Email' in query_response: email = query_response['Email'] # get the org name of the authenticated user r = requests.get( instance_url + '/services/data/v' + str(settings.SALESFORCE_API_VERSION) + '.0/sobjects/Organization/' + org_id + '?fields=Name', headers={'Authorization': 'OAuth ' + access_token}) if 'Name' in json.loads(r.text): org_name = json.loads(r.text)['Name'] login_form = LoginForm( initial={ 'environment': environment, 'access_token': access_token, 'instance_url': instance_url, 'org_id': org_id, 'username': username, 'org_name': org_name, 'email': email }) # Run after user selects logout or get schema if request.POST: login_form = LoginForm(request.POST) if login_form.is_valid(): # Copy all variables from form environment = login_form.cleaned_data['environment'] access_token = login_form.cleaned_data['access_token'] instance_url = login_form.cleaned_data['instance_url'] org_id = login_form.cleaned_data['org_id'] username = login_form.cleaned_data['username'] email = login_form.cleaned_data['email'] org_name = login_form.cleaned_data['org_name'] # Logout action if 'logout' in request.POST: r = requests.post(instance_url + '/services/oauth2/revoke', headers={ 'content-type': 'application/x-www-form-urlencoded' }, data={'token': access_token}) return HttpResponseRedirect( '/logout?instance_prefix=' + instance_url.replace( 'https://', '').replace('.salesforce.com', '')) # Continue action. Start job to get metadata for the job if 'get_metadata' in request.POST: job = Job() job.random_id = uuid.uuid4() job.created_date = datetime.datetime.now() job.status = 'Not Started' job.username = username job.email = email job.environment = environment job.org_id = org_id job.org_name = org_name job.instance_url = instance_url job.access_token = access_token job.save() # Start downloading metadata using async task get_metadata.delay(job) # Return to loading page. This will cycle an AJAX request to check when job is finished return HttpResponseRedirect('/loading/' + str(job.random_id)) return render_to_response( 'oauth_response.html', RequestContext( request, { 'error': error_exists, 'error_message': error_message, 'username': username, 'org_name': org_name, 'login_form': login_form }))
def oauth_response(request): """ Controller for the oauth_response page. """ # Default variables error_exists = False error_message = '' username = '' org_name = '' org_id = '' email = '' # On page load if request.GET: # Get OAuth response values oauth_code = request.GET.get('code') environment = request.GET.get('state') access_token = '' instance_url = '' if 'Production' in environment: login_url = 'https://login.salesforce.com' else: login_url = 'https://test.salesforce.com' # Log in to REST API to obtain access token r = requests.post(login_url + '/services/oauth2/token', headers={ 'content-type':'application/x-www-form-urlencoded'}, data={'grant_type':'authorization_code','client_id': settings.SALESFORCE_CONSUMER_KEY,'client_secret':settings.SALESFORCE_CONSUMER_SECRET,'redirect_uri': settings.SALESFORCE_REDIRECT_URI,'code': oauth_code}) # Load JSON response auth_response = json.loads(r.text) # If login error - return error for user if 'error_description' in auth_response: error_exists = True error_message = auth_response['error_description'] # Otherwise get session details else: access_token = auth_response['access_token'] instance_url = auth_response['instance_url'] user_id = auth_response['id'][-18:] org_id = auth_response['id'][:-19] org_id = org_id[-18:] # get username of the authenticated user r = requests.get(instance_url + '/services/data/v' + str(settings.SALESFORCE_API_VERSION) + '.0/sobjects/User/' + user_id + '?fields=Username,Email', headers={'Authorization': 'OAuth ' + access_token}) query_response = json.loads(r.text) username = query_response['Username'] email = query_response['Email'] # get the org name of the authenticated user r = requests.get(instance_url + '/services/data/v' + str(settings.SALESFORCE_API_VERSION) + '.0/sobjects/Organization/' + org_id + '?fields=Name', headers={'Authorization': 'OAuth ' + access_token}) org_name = json.loads(r.text)['Name'] login_form = LoginForm(initial={'environment': environment, 'access_token': access_token, 'instance_url': instance_url, 'org_id': org_id, 'username': username, 'org_name':org_name, 'email': email}) # Run after user selects logout or get schema if request.POST: login_form = LoginForm(request.POST) if login_form.is_valid(): # Copy all variables from form environment = login_form.cleaned_data['environment'] access_token = login_form.cleaned_data['access_token'] instance_url = login_form.cleaned_data['instance_url'] org_id = login_form.cleaned_data['org_id'] username = login_form.cleaned_data['username'] email = login_form.cleaned_data['email'] org_name = login_form.cleaned_data['org_name'] # Logout action if 'logout' in request.POST: r = requests.post(instance_url + '/services/oauth2/revoke', headers={'content-type':'application/x-www-form-urlencoded'}, data={'token': access_token}) return HttpResponseRedirect('/logout?instance_prefix=' + instance_url.replace('https://','').replace('.salesforce.com','')) # Continue action. Start job to get metadata for the job if 'get_metadata' in request.POST: job = Job() job.random_id = uuid.uuid4() job.created_date = datetime.datetime.now() job.status = 'Not Started' job.username = username job.email = email job.org_id = org_id job.org_name = org_name job.instance_url = instance_url job.access_token = access_token job.save() # Start downloading metadata using async task get_metadata.delay(job) # Return to loading page. This will cycle an AJAX request to check when job is finished return HttpResponseRedirect('/loading/' + str(job.random_id)) return render_to_response('oauth_response.html', RequestContext(request,{'error': error_exists, 'error_message': error_message, 'username': username, 'org_name': org_name, 'login_form': login_form}))