5
5
6
6
from odoo import api , exceptions , fields , models
7
7
8
- from odoo .addons import base
9
-
10
- base .models .res_users .USER_PRIVATE_FIELDS .append ("oauth_master_uuid" )
11
-
12
8
13
9
class ResUsers (models .Model ):
14
10
_inherit = "res.users"
@@ -27,10 +23,9 @@ def _generate_oauth_master_uuid(self):
27
23
oauth_access_max_token = fields .Integer (
28
24
string = "Max Number of Simultaneous Connections" , default = 10 , required = True
29
25
)
30
- oauth_master_uuid = fields .Char (
31
- string = "Master UUID" ,
32
- copy = False ,
33
- readonly = True ,
26
+
27
+ # use the oauth_access_token field as oauth_master_uuid
28
+ oauth_access_token = fields .Char (
34
29
required = True ,
35
30
default = lambda self : self ._generate_oauth_master_uuid (),
36
31
)
@@ -39,45 +34,62 @@ def _generate_oauth_master_uuid(self):
39
34
def multi_token_model (self ):
40
35
return self .env ["auth.oauth.multi.token" ]
41
36
37
+ @api .model
38
+ def _generate_signup_values (self , provider , validation , params ):
39
+ """Because access_token was replaced in
40
+ _auth_oauth_signin we need to replace it here."""
41
+ res = super ()._generate_signup_values (provider , validation , params )
42
+ res ["oauth_access_token" ] = params ["access_token_multi" ]
43
+ return res
44
+
42
45
@api .model
43
46
def _auth_oauth_signin (self , provider , validation , params ):
44
47
"""Override to handle sign-in with multi token."""
45
- res = super (). _auth_oauth_signin ( provider , validation , params )
48
+ params [ "access_token_multi" ] = params [ "access_token" ]
46
49
47
- oauth_uid = validation ["user_id" ]
48
50
# Lookup for user by oauth uid and provider
51
+ oauth_uid = validation ["user_id" ]
49
52
user = self .search (
50
53
[("oauth_uid" , "=" , oauth_uid ), ("oauth_provider_id" , "=" , provider )]
51
54
)
55
+
56
+ # Because access_token is automatically written to the user, we need to replace
57
+ # this by the existing oauth_access_token which acts as oauth_master_uuid
58
+ params ["access_token" ] = user .oauth_access_token
59
+ res = super ()._auth_oauth_signin (provider , validation , params )
60
+
52
61
if not user :
53
62
raise exceptions .AccessDenied ()
54
63
user .ensure_one ()
55
64
# user found and unique: create a token
56
65
self .multi_token_model .create (
57
- {"user_id" : user .id , "oauth_access_token" : params ["access_token " ]}
66
+ {"user_id" : user .id , "oauth_access_token" : params ["access_token_multi " ]}
58
67
)
59
68
return res
60
69
61
70
def action_oauth_clear_token (self ):
62
71
"""Inactivate current user tokens."""
63
72
self .mapped ("oauth_access_token_ids" )._oauth_clear_token ()
64
73
for res in self :
65
- res .oauth_access_token = False
66
- res .oauth_master_uuid = self ._generate_oauth_master_uuid ()
74
+ res .oauth_access_token = self ._generate_oauth_master_uuid ()
67
75
68
76
@api .model
69
77
def _check_credentials (self , password , env ):
70
78
"""Override to check credentials against multi tokens."""
71
79
try :
72
80
return super ()._check_credentials (password , env )
73
81
except exceptions .AccessDenied :
74
- res = self . multi_token_model . sudo (). search (
75
- [( "user_id" , "=" , self .env .uid ), ( "oauth_access_token" , "=" , password )]
82
+ passwd_allowed = (
83
+ env [ "interactive" ] or not self .env .user . _rpc_api_keys_only ()
76
84
)
77
- if not res :
78
- raise
85
+ if passwd_allowed and self .env .user .active :
86
+ res = self .multi_token_model .sudo ().search (
87
+ [
88
+ ("user_id" , "=" , self .env .uid ),
89
+ ("oauth_access_token" , "=" , password ),
90
+ ]
91
+ )
92
+ if res :
93
+ return
79
94
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" }
95
+ raise
0 commit comments