Skip to content

Commit

Permalink
[IMP] auth_oidc: Add _auth_oauth_signing to (un)link from groups
Browse files Browse the repository at this point in the history
  • Loading branch information
OdyX committed Aug 28, 2024
1 parent 62a5bdd commit 96788d5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions auth_oidc/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from odoo import api, models
from odoo.exceptions import AccessDenied
from odoo.fields import Command
from odoo.http import request

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,6 +45,41 @@ def _auth_oauth_get_tokens_auth_code_flow(self, oauth_provider, params):
# https://openid.net/specs/openid-connect-core-1_0.html#TokenResponse
return response_json.get("access_token"), response_json.get("id_token")

@api.model
def _auth_oauth_signin(self, provider, validation, params):
"""
retrieve and sign in the user corresponding to provider and validated
access token
:param provider: oauth provider id (int)
:param validation: result of validation of access token (dict)
:param params: oauth parameters (dict)
:return: user login (str)
:raise: AccessDenied if signin failed
"""
login = super()._auth_oauth_signin(provider, validation, params)
user = self.search([("login", "=", login)])
oauth_provider = self.env["auth.oauth.provider"].browse(provider)
# Assume the groups are exclusively managed via OAuth 'groups'
if user and oauth_provider.groups_field in validation:
group_updates = []

Check warning on line 65 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L65

Added line #L65 was not covered by tests
for group_line in oauth_provider.group_line_ids:
if group_line.oauth_group_name in validation.get(
oauth_provider.groups_field
):
_logger.debug(

Check warning on line 70 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L70

Added line #L70 was not covered by tests
f"Add user {user.id} to the group {group_line.group_id.id}"
)
group_updates.append((Command.LINK, group_line.group_id.id))

Check warning on line 73 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L73

Added line #L73 was not covered by tests
else:
_logger.debug(

Check warning on line 75 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L75

Added line #L75 was not covered by tests
f"Remove user {user.id} from the group {group_line.group_id.id}"
)
group_updates.append((Command.UNLINK, group_line.group_id.id))

Check warning on line 78 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L78

Added line #L78 was not covered by tests
if group_updates:
user.write({"groups_id": group_updates})

Check warning on line 80 in auth_oidc/models/res_users.py

View check run for this annotation

Codecov / codecov/patch

auth_oidc/models/res_users.py#L80

Added line #L80 was not covered by tests
return login

@api.model
def auth_oauth(self, provider, params):
oauth_provider = self.env["auth.oauth.provider"].browse(provider)
Expand Down

0 comments on commit 96788d5

Please sign in to comment.