Skip to content

Commit e305183

Browse files
CRogoskobros-tech
authored andcommitted
[IMP] auth_oauth_multi_token: make it compatible with odoo.sh "login as"
1 parent 7abc6d4 commit e305183

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

auth_oauth_multi_token/models/res_users.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
from odoo import api, exceptions, fields, models
77

8-
from odoo.addons import base
9-
10-
base.models.res_users.USER_PRIVATE_FIELDS.append("oauth_master_uuid")
11-
128

139
class ResUsers(models.Model):
1410
_inherit = "res.users"
@@ -24,60 +20,77 @@ def _generate_oauth_master_uuid(self):
2420
readonly=True,
2521
groups="base.group_system",
2622
)
23+
2724
oauth_access_max_token = fields.Integer(
2825
string="Max Number of Simultaneous Connections", default=10, required=True
2926
)
30-
oauth_master_uuid = fields.Char(
27+
28+
# use the oauth_access_token field as oauth_master_uuid
29+
oauth_access_token = fields.Char(
3130
string="Master UUID",
32-
copy=False,
33-
readonly=True,
34-
required=True,
3531
default=lambda self: self._generate_oauth_master_uuid(),
3632
)
3733

3834
@property
3935
def multi_token_model(self):
4036
return self.env["auth.oauth.multi.token"]
4137

38+
@api.model
39+
def _generate_signup_values(self, provider, validation, params):
40+
"""Because access_token was replace in
41+
_auth_oauth_signin we need to replace it here."""
42+
res = super()._generate_signup_values(provider, validation, params)
43+
res["oauth_access_token"] = params["access_token_multi"]
44+
return res
45+
4246
@api.model
4347
def _auth_oauth_signin(self, provider, validation, params):
4448
"""Override to handle sign-in with multi token."""
45-
res = super()._auth_oauth_signin(provider, validation, params)
49+
params["access_token_multi"] = params["access_token"]
4650

47-
oauth_uid = validation["user_id"]
4851
# Lookup for user by oauth uid and provider
52+
oauth_uid = validation["user_id"]
4953
user = self.search(
5054
[("oauth_uid", "=", oauth_uid), ("oauth_provider_id", "=", provider)]
5155
)
56+
57+
# Because access_token is automatically written to the user,
58+
# we need to replace this by the existing oauth_access_token
59+
params["access_token"] = user.oauth_access_token
60+
res = super()._auth_oauth_signin(provider, validation, params)
61+
5262
if not user:
5363
raise exceptions.AccessDenied()
5464
user.ensure_one()
5565
# user found and unique: create a token
5666
self.multi_token_model.create(
57-
{"user_id": user.id, "oauth_access_token": params["access_token"]}
67+
{"user_id": user.id, "oauth_access_token": params["access_token_multi"]}
5868
)
5969
return res
6070

6171
def action_oauth_clear_token(self):
6272
"""Inactivate current user tokens."""
6373
self.mapped("oauth_access_token_ids")._oauth_clear_token()
6474
for res in self:
65-
res.oauth_access_token = False
66-
res.oauth_master_uuid = self._generate_oauth_master_uuid()
75+
res.oauth_access_token = self._generate_oauth_master_uuid()
6776

6877
@api.model
6978
def _check_credentials(self, password, env):
7079
"""Override to check credentials against multi tokens."""
7180
try:
7281
return super()._check_credentials(password, env)
7382
except exceptions.AccessDenied:
74-
res = self.multi_token_model.sudo().search(
75-
[("user_id", "=", self.env.uid), ("oauth_access_token", "=", password)]
83+
passwd_allowed = (
84+
env["interactive"] or not self.env.user._rpc_api_keys_only()
7685
)
77-
if not res:
78-
raise
86+
if passwd_allowed and self.env.user.active:
87+
res = self.multi_token_model.sudo().search(
88+
[
89+
("user_id", "=", self.env.uid),
90+
("oauth_access_token", "=", password),
91+
]
92+
)
93+
if res:
94+
return
7995

80-
def _get_session_token_fields(self):
81-
res = super()._get_session_token_fields()
82-
res.remove("oauth_access_token")
83-
return res | {"oauth_master_uuid"}
96+
raise

auth_oauth_multi_token/tests/test_multi_token.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ def test_access_multi_token(self):
8080
len(self.user.oauth_access_token_ids), self.user.oauth_access_max_token
8181
)
8282

83-
def test_remove_oauth_access_token(self):
83+
def test_oauth_access_token_odoo_sh(self):
84+
# do not change the _get_session_token_fields
85+
# result to stay compatible with odoo.sh
8486
res = self.user._get_session_token_fields()
85-
self.assertFalse("oauth_access_token" in res)
86-
self.assertTrue("oauth_master_uuid" in res)
87+
self.assertTrue("oauth_access_token" in res)
88+
self.assertFalse("oauth_master_uuid" in res)
8789

8890
def test_action_oauth_clear_token(self):
8991
self.user.action_oauth_clear_token()

0 commit comments

Comments
 (0)