From 58aca506feeb6bdbfc17009dba6944be2d77b6de Mon Sep 17 00:00:00 2001 From: Yogesh Mahajan Date: Fri, 24 Mar 2023 15:50:37 +0530 Subject: [PATCH] Fixed error occurring while authentication in Google Cloud SQL deployment wizard in server mode. #5750 --- web/pgadmin/misc/cloud/google/__init__.py | 26 +++++++++--- .../misc/cloud/static/js/google_schema.ui.js | 40 ++++++------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/web/pgadmin/misc/cloud/google/__init__.py b/web/pgadmin/misc/cloud/google/__init__.py index 5d0cf3420..abf772d47 100644 --- a/web/pgadmin/misc/cloud/google/__init__.py +++ b/web/pgadmin/misc/cloud/google/__init__.py @@ -13,19 +13,18 @@ import json import os from pathlib import Path -from oauthlib.oauth2 import AccessDeniedError - from config import root from pgadmin.utils.csrf import pgCSRFProtect -from pgadmin import make_json_response -from pgadmin.utils.ajax import plain_text_response +from pgadmin.utils.ajax import plain_text_response, unauthorized, \ + make_json_response, bad_request from pgadmin.misc.bgprocess import BatchProcess from pgadmin.misc.cloud.utils import _create_server, CloudProcessDesc -from pgadmin.utils import PgAdminModule - +from pgadmin.utils import PgAdminModule, filename_with_file_manager_path from flask_security import login_required from flask import session, current_app, request +from flask_babel import gettext as _ +from oauthlib.oauth2 import AccessDeniedError from googleapiclient import discovery from googleapiclient.errors import HttpError from google_auth_oauthlib.flow import InstalledAppFlow @@ -61,6 +60,12 @@ blueprint = GooglePostgresqlModule(MODULE_NAME, __name__, static_url_path='/misc/cloud/google') +@blueprint.route("/") +@login_required +def index(): + return bad_request(errormsg=_("This URL cannot be called directly.")) + + @blueprint.route('/verify_credentials/', methods=['POST'], endpoint='verify_credentials') @login_required @@ -75,6 +80,14 @@ def verify_credentials(): error = None res_data = {} + try: + client_secret_path = \ + filename_with_file_manager_path(client_secret_path) + except PermissionError as e: + return unauthorized(errormsg=str(e)) + except Exception as e: + return bad_request(errormsg=str(e)) + if client_secret_path is not None and Path(client_secret_path).exists(): with open(client_secret_path, 'r') as json_file: client_config = json.load(json_file) @@ -100,6 +113,7 @@ def verify_credentials(): session['google']['google_obj'] = pickle.dumps(_google, -1) else: error = 'Client secret path not found' + session.pop('google', None) return make_json_response(success=status, errormsg=error, data=res_data) diff --git a/web/pgadmin/misc/cloud/static/js/google_schema.ui.js b/web/pgadmin/misc/cloud/static/js/google_schema.ui.js index 4479a8675..35eb0538a 100644 --- a/web/pgadmin/misc/cloud/static/js/google_schema.ui.js +++ b/web/pgadmin/misc/cloud/static/js/google_schema.ui.js @@ -55,18 +55,23 @@ class GoogleCredSchema extends BaseUISchema{ disabled: (state)=>{ return state.client_secret_file ? false : true; }, - depChange: ()=> { - return {is_authenticating: true}; - }, deferredDepChange: (state, source)=>{ return new Promise((resolve, reject)=>{ /* button clicked */ if(source == 'auth_btn') { obj.fieldOptions.authenticateGoogle(state.client_secret_file) - .then(()=>{ - resolve(()=>({ - is_authenticating: false, - })); + .then((apiRes)=>{ + resolve(()=>{ + if(apiRes){ + obj.fieldOptions.verification_ack() + .then(()=>{ + resolve(); + }) + .catch((err)=>{ + reject(err); + }); + } + }); }) .catch((err)=>{ reject(err); @@ -74,26 +79,7 @@ class GoogleCredSchema extends BaseUISchema{ } }); } - }, - { - id: 'is_authenticating', - visible: false, - type: '', - deps:['auth_btn'], - deferredDepChange: (state, source)=>{ - return new Promise((resolve, reject)=>{ - if(source == 'auth_btn' && state.is_authenticating ) { - obj.fieldOptions.verification_ack() - .then(()=>{ - resolve(); - }) - .catch((err)=>{ - reject(err); - }); - } - }); - }, - }, + } ];} }