Skip to content

Commit 10e7a36

Browse files
committed
Fixed an issue where OAuth2 authentication fails with 'object has no attribute' if OAUTH2_AUTO_CREATE_USER is False. pgadmin-org#9279
1 parent 5b231dd commit 10e7a36

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

web/pgadmin/authenticate/oauth2.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ def _resolve_username(self, id_token_claims, profile_dict):
437437

438438
def login(self, form):
439439
if not self.oauth2_current_client:
440-
error_msg = 'No OAuth2 provider available.'
440+
error_msg = gettext('No OAuth2 provider available.')
441441
current_app.logger.error(error_msg)
442-
return False, gettext(error_msg)
442+
return False, error_msg
443443

444444
profile = self.get_user_profile()
445445
profile_dict = self.get_profile_dict(profile)
@@ -468,35 +468,35 @@ def login(self, form):
468468
not id_token_claims and
469469
not profile_dict
470470
):
471-
error_msg = "No profile data found from OIDC provider."
471+
error_msg = gettext("No profile data found from OIDC provider.")
472472
current_app.logger.error(error_msg)
473-
return False, gettext(error_msg)
473+
return False, error_msg
474474

475475
# For non-OIDC providers, profile is required
476476
if not self._is_oidc_provider() and not profile_dict:
477-
error_msg = "No profile data found."
477+
error_msg = gettext("No profile data found.")
478478
current_app.logger.error(error_msg)
479-
return False, gettext(error_msg)
479+
return False, error_msg
480480

481481
# Resolve username using OIDC-aware logic
482482
username, email = self._resolve_username(id_token_claims, profile_dict)
483483

484484
if not username:
485485
if self._is_oidc_provider():
486-
error_msg = (
486+
error_msg = gettext(
487487
'Could not extract username from OIDC claims. '
488488
'Please ensure your OIDC provider returns standard '
489489
'claims (email, preferred_username, or sub).'
490490
)
491491
else:
492-
error_msg = (
492+
error_msg = gettext(
493493
'An email id or OAUTH2_USERNAME_CLAIM is required to '
494494
'login into pgAdmin. Please update your OAuth2 profile '
495495
'for email id or set OAUTH2_USERNAME_CLAIM config '
496496
'parameter.'
497497
)
498498
current_app.logger.error(error_msg)
499-
return False, gettext(error_msg)
499+
return False, error_msg
500500

501501
additional_claims = None
502502
if 'OAUTH2_ADDITIONAL_CLAIMS' in self.oauth2_config[
@@ -678,10 +678,12 @@ def authenticate(self, form):
678678
self.oauth2_current_client].authorize_redirect(redirect_url)
679679

680680
def __auto_create_user(self, username, email):
681-
if config.OAUTH2_AUTO_CREATE_USER:
682-
user = User.query.filter_by(username=username,
683-
auth_source=OAUTH2).first()
684-
if not user:
681+
user = User.query.filter_by(username=username,
682+
auth_source=OAUTH2).first()
683+
if user:
684+
return True, {'username': username}
685+
else:
686+
if config.OAUTH2_AUTO_CREATE_USER:
685687
create_msg = ("Creating user {0} with email {1} "
686688
"from auth source OAUTH2.")
687689
current_app.logger.info(create_msg.format(username,
@@ -693,18 +695,20 @@ def __auto_create_user(self, username, email):
693695
'active': True,
694696
'auth_source': OAUTH2
695697
})
696-
697-
return True, {'username': username}
698+
else:
699+
return False, gettext('No Email/Username found.'
700+
' Please contact your administrator.')
698701

699702
def __is_any_claim_valid(self, identity, additional_claims):
700703
if additional_claims is None:
701-
reason = "Additional claim config is None, no check to do."
704+
reason = gettext("Additional claim config is None,"
705+
" no check to do.")
702706
return (True, reason)
703707
if not isinstance(additional_claims, dict):
704-
reason = "Additional claim check config is not a dict."
708+
reason = gettext("Additional claim check config is not a dict.")
705709
return (False, reason)
706710
if additional_claims.keys() is None:
707-
reason = "Additional claim check config dict is empty."
711+
reason = gettext("Additional claim check config dict is empty.")
708712
return (False, reason)
709713
for key in additional_claims.keys():
710714
claim = identity.get(key)
@@ -716,7 +720,7 @@ def __is_any_claim_valid(self, identity, additional_claims):
716720
if not isinstance(authorized_claims, list):
717721
authorized_claims = [authorized_claims]
718722
if any(item in authorized_claims for item in claim):
719-
reason = "Claim match found. Authorized access."
723+
reason = gettext("Claim match found. Authorized access.")
720724
return True, reason
721-
reason = "No match was found."
725+
reason = gettext("No match was found.")
722726
return False, reason

0 commit comments

Comments
 (0)