Skip to content

Commit 04e4374

Browse files
committed
Merge PR #473 into 15.0
Signed-off-by pedrobaeza
2 parents 631b05e + a0de56c commit 04e4374

File tree

20 files changed

+953
-0
lines changed

20 files changed

+953
-0
lines changed

auth_oauth_multi_token/README.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
=================
2+
OAuth Multi Token
3+
=================
4+
5+
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
6+
!! This file is generated by oca-gen-addon-readme !!
7+
!! changes will be overwritten. !!
8+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9+
10+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
11+
:target: https://odoo-community.org/page/development-status
12+
:alt: Beta
13+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
14+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
15+
:alt: License: AGPL-3
16+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github
17+
:target: https://github.com/OCA/server-auth/tree/15.0/auth_oauth_multi_token
18+
:alt: OCA/server-auth
19+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
20+
:target: https://translation.odoo-community.org/projects/server-auth-15-0/server-auth-15-0-auth_oauth_multi_token
21+
:alt: Translate me on Weblate
22+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
23+
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/server-auth&target_branch=15.0
24+
:alt: Try me on Runboat
25+
26+
|badge1| |badge2| |badge3| |badge4| |badge5|
27+
28+
This module adds the possibility to connect with the same account
29+
on more than one device at the same time.
30+
31+
All providers are supported (Google, Facebook, Odoo, etc).
32+
33+
**Table of contents**
34+
35+
.. contents::
36+
:local:
37+
38+
Usage
39+
=====
40+
41+
Nothing changes on login action: just select your provider and try to log in.
42+
43+
Bug Tracker
44+
===========
45+
46+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-auth/issues>`_.
47+
In case of trouble, please check there if your issue has already been reported.
48+
If you spotted it first, help us smashing it by providing a detailed and welcomed
49+
`feedback <https://github.com/OCA/server-auth/issues/new?body=module:%20auth_oauth_multi_token%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
50+
51+
Do not contact contributors directly about support or help with technical issues.
52+
53+
Credits
54+
=======
55+
56+
Authors
57+
~~~~~~~
58+
59+
* Florent de Labarre
60+
* Camptocamp
61+
62+
Contributors
63+
~~~~~~~~~~~~
64+
65+
* Florent de Labarre <[email protected]>
66+
* Simone Orsi <[email protected]>
67+
* `Tecnativa <https://www.tecnativa.com/>`__:
68+
69+
* Jairo Llopis
70+
* Sergio Teruel
71+
72+
* Stéphane Bidoul <[email protected]>
73+
* Dan Tillinghast
74+
75+
Maintainers
76+
~~~~~~~~~~~
77+
78+
This module is maintained by the OCA.
79+
80+
.. image:: https://odoo-community.org/logo.png
81+
:alt: Odoo Community Association
82+
:target: https://odoo-community.org
83+
84+
OCA, or the Odoo Community Association, is a nonprofit organization whose
85+
mission is to support the collaborative development of Odoo features and
86+
promote its widespread use.
87+
88+
This module is part of the `OCA/server-auth <https://github.com/OCA/server-auth/tree/15.0/auth_oauth_multi_token>`_ project on GitHub.
89+
90+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

auth_oauth_multi_token/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2016 Florent de Labarre
2+
# Copyright 2017 Camptocamp
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
4+
5+
{
6+
"name": "OAuth Multi Token",
7+
"version": "15.0.1.0.0",
8+
"license": "AGPL-3",
9+
"author": "Florent de Labarre, Camptocamp, Odoo Community Association (OCA)",
10+
"summary": """Allow multiple connection with the same OAuth account""",
11+
"category": "Tool",
12+
"website": "https://github.com/OCA/server-auth",
13+
"depends": ["auth_oauth"],
14+
"data": [
15+
"security/ir.model.access.csv",
16+
"views/auth_oauth_multi_token.xml",
17+
"views/res_users.xml",
18+
],
19+
"installable": True,
20+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * auth_oauth_multi_token
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 14.0\n"
8+
"Report-Msgid-Bugs-To: \n"
9+
"Last-Translator: \n"
10+
"Language-Team: \n"
11+
"MIME-Version: 1.0\n"
12+
"Content-Type: text/plain; charset=UTF-8\n"
13+
"Content-Transfer-Encoding: \n"
14+
"Plural-Forms: \n"
15+
16+
#. module: auth_oauth_multi_token
17+
#: model_terms:ir.ui.view,arch_db:auth_oauth_multi_token.view_users_form
18+
msgid "Clear Tokens"
19+
msgstr ""
20+
21+
#. module: auth_oauth_multi_token
22+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__create_uid
23+
msgid "Created by"
24+
msgstr ""
25+
26+
#. module: auth_oauth_multi_token
27+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__create_date
28+
msgid "Created on"
29+
msgstr ""
30+
31+
#. module: auth_oauth_multi_token
32+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__display_name
33+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users__display_name
34+
msgid "Display Name"
35+
msgstr ""
36+
37+
#. module: auth_oauth_multi_token
38+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__id
39+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users__id
40+
msgid "ID"
41+
msgstr ""
42+
43+
#. module: auth_oauth_multi_token
44+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token____last_update
45+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users____last_update
46+
msgid "Last Modified on"
47+
msgstr ""
48+
49+
#. module: auth_oauth_multi_token
50+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__write_uid
51+
msgid "Last Updated by"
52+
msgstr ""
53+
54+
#. module: auth_oauth_multi_token
55+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__write_date
56+
msgid "Last Updated on"
57+
msgstr ""
58+
59+
#. module: auth_oauth_multi_token
60+
#: model_terms:ir.ui.view,arch_db:auth_oauth_multi_token.view_users_form
61+
msgid "Latest tokens"
62+
msgstr ""
63+
64+
#. module: auth_oauth_multi_token
65+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users__oauth_master_uuid
66+
msgid "Master UUID"
67+
msgstr ""
68+
69+
#. module: auth_oauth_multi_token
70+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users__oauth_access_max_token
71+
msgid "Max number of simultaneous connections"
72+
msgstr ""
73+
74+
#. module: auth_oauth_multi_token
75+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__oauth_access_token
76+
msgid "OAuth Access Token"
77+
msgstr ""
78+
79+
#. module: auth_oauth_multi_token
80+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_res_users__oauth_access_token_ids
81+
msgid "OAuth tokens"
82+
msgstr ""
83+
84+
#. module: auth_oauth_multi_token
85+
#: model:ir.model,name:auth_oauth_multi_token.model_auth_oauth_multi_token
86+
msgid "OAuth2 token"
87+
msgstr ""
88+
89+
#. module: auth_oauth_multi_token
90+
#: model:ir.model.fields,field_description:auth_oauth_multi_token.field_auth_oauth_multi_token__user_id
91+
msgid "User"
92+
msgstr ""
93+
94+
#. module: auth_oauth_multi_token
95+
#: model:ir.model,name:auth_oauth_multi_token.model_res_users
96+
msgid "Users"
97+
msgstr ""
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import auth_oauth_multi_token
2+
from . import res_users
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2016 Florent de Labarre
2+
# Copyright 2017 Camptocamp
3+
# Copyright 2021 ACSONE SA/NV
4+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
5+
6+
from odoo import api, fields, models
7+
8+
9+
class AuthOauthMultiToken(models.Model):
10+
"""Define a set of tokens."""
11+
12+
_name = "auth.oauth.multi.token"
13+
_description = "OAuth2 token"
14+
_order = "id desc"
15+
16+
oauth_access_token = fields.Char(
17+
string="OAuth Access Token", readonly=True, copy=False
18+
)
19+
user_id = fields.Many2one(
20+
comodel_name="res.users",
21+
string="User",
22+
required=True,
23+
readonly=True,
24+
index=True,
25+
ondelete="cascade",
26+
)
27+
28+
@api.model
29+
def create(self, vals):
30+
"""Override to validate tokens."""
31+
token = super().create(vals)
32+
token._oauth_validate_multi_token()
33+
return token
34+
35+
@api.model
36+
def _oauth_user_tokens(self, user_id):
37+
"""Retrieve tokens for given user.
38+
39+
:param user_id: Odoo ID of the user
40+
"""
41+
return self.search([("user_id", "=", user_id)])
42+
43+
def _oauth_validate_multi_token(self):
44+
"""Check current user's token and clear them if max number reached."""
45+
user_tokens = self._oauth_user_tokens(self.user_id.id)
46+
max_token = self.user_id.oauth_access_max_token
47+
if user_tokens and len(user_tokens) > max_token:
48+
# clear last token
49+
user_tokens[max_token - 1]._oauth_clear_token()
50+
51+
def _oauth_clear_token(self):
52+
"""Disable current token records."""
53+
self.unlink()
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright 2016 Florent de Labarre
2+
# Copyright 2017 Camptocamp
3+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
4+
import uuid
5+
6+
from odoo import api, exceptions, fields, models
7+
8+
from odoo.addons import base
9+
10+
base.models.res_users.USER_PRIVATE_FIELDS.append("oauth_master_uuid")
11+
12+
13+
class ResUsers(models.Model):
14+
_inherit = "res.users"
15+
16+
def _generate_oauth_master_uuid(self):
17+
return uuid.uuid4().hex
18+
19+
oauth_access_token_ids = fields.One2many(
20+
comodel_name="auth.oauth.multi.token",
21+
inverse_name="user_id",
22+
string="OAuth tokens",
23+
copy=False,
24+
readonly=True,
25+
groups="base.group_system",
26+
)
27+
oauth_access_max_token = fields.Integer(
28+
string="Max number of simultaneous connections", default=10, required=True
29+
)
30+
oauth_master_uuid = fields.Char(
31+
string="Master UUID",
32+
copy=False,
33+
readonly=True,
34+
required=True,
35+
default=lambda self: self._generate_oauth_master_uuid(),
36+
)
37+
38+
@property
39+
def multi_token_model(self):
40+
return self.env["auth.oauth.multi.token"]
41+
42+
@api.model
43+
def _auth_oauth_signin(self, provider, validation, params):
44+
"""Override to handle sign-in with multi token."""
45+
res = super()._auth_oauth_signin(provider, validation, params)
46+
47+
oauth_uid = validation["user_id"]
48+
# Lookup for user by oauth uid and provider
49+
user = self.search(
50+
[("oauth_uid", "=", oauth_uid), ("oauth_provider_id", "=", provider)]
51+
)
52+
if not user:
53+
raise exceptions.AccessDenied()
54+
user.ensure_one()
55+
# user found and unique: create a token
56+
self.multi_token_model.create(
57+
{"user_id": user.id, "oauth_access_token": params["access_token"]}
58+
)
59+
return res
60+
61+
def action_oauth_clear_token(self):
62+
"""Inactivate current user tokens."""
63+
self.mapped("oauth_access_token_ids")._oauth_clear_token()
64+
for res in self:
65+
res.oauth_access_token = False
66+
res.oauth_master_uuid = self._generate_oauth_master_uuid()
67+
68+
@api.model
69+
def _check_credentials(self, password, env):
70+
"""Override to check credentials against multi tokens."""
71+
try:
72+
return super()._check_credentials(password, env)
73+
except exceptions.AccessDenied:
74+
res = self.multi_token_model.sudo().search(
75+
[("user_id", "=", self.env.uid), ("oauth_access_token", "=", password)]
76+
)
77+
if not res:
78+
raise
79+
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"}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
On users' form you can set the number of maximum simultaneous connections.
2+
3+
By default 10 connections are allowed.
4+
5+
From there you can also clear / inactivate existing tokens.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* Florent de Labarre <[email protected]>
2+
* Simone Orsi <[email protected]>
3+
* `Tecnativa <https://www.tecnativa.com/>`__:
4+
5+
* Jairo Llopis
6+
* Sergio Teruel
7+
8+
* Stéphane Bidoul <[email protected]>
9+
* Dan Tillinghast
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This module adds the possibility to connect with the same account
2+
on more than one device at the same time.
3+
4+
All providers are supported (Google, Facebook, Odoo, etc).

0 commit comments

Comments
 (0)