Fixed error occurring while authentication in Google Cloud SQL deployment wizard in server mode. #5750

This commit is contained in:
Yogesh Mahajan 2023-03-24 15:50:37 +05:30 committed by GitHub
parent 8700a17dad
commit 58aca506fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 33 deletions

View File

@ -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)

View File

@ -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);
});
}
});
},
},
}
];}
}