Skip to content

Commit 0dfc403

Browse files
committed
Merge PR #100 into 18.0
Signed-off-by simahawk
2 parents 12959dc + f782f55 commit 0dfc403

20 files changed

+892
-0
lines changed

endpoint_auth_api_key/README.rst

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
=====================
2+
Endpoint Auth API key
3+
=====================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:1765cc31393e134a1815e257272bd7ecd1eb57e4bb56f7a4f2098f47283fc2e9
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Alpha
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
20+
:target: https://github.com/OCA/web-api/tree/17.0/endpoint_auth_api_key
21+
:alt: OCA/web-api
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/web-api-17-0/web-api-17-0-endpoint_auth_api_key
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=17.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
Provide API key auth for endpoints.
32+
33+
.. IMPORTANT::
34+
This is an alpha version, the data model and design can change at any time without warning.
35+
Only for development or testing purpose, do not use in production.
36+
`More details on development status <https://odoo-community.org/page/development-status>`_
37+
38+
**Table of contents**
39+
40+
.. contents::
41+
:local:
42+
43+
Bug Tracker
44+
===========
45+
46+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/issues>`_.
47+
In case of trouble, please check there if your issue has already been reported.
48+
If you spotted it first, help us to smash it by providing a detailed and welcomed
49+
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_auth_api_key%0Aversion:%2017.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+
* Camptocamp
60+
61+
Contributors
62+
------------
63+
64+
- Simone Orsi <[email protected]>
65+
66+
- `Trobz <https://trobz.com>`__:
67+
68+
- Son Ho <[email protected]>
69+
70+
Other credits
71+
-------------
72+
73+
The migration of this module from 14.0 to 16.0 was financially supported
74+
by Camptocamp
75+
76+
Maintainers
77+
-----------
78+
79+
This module is maintained by the OCA.
80+
81+
.. image:: https://odoo-community.org/logo.png
82+
:alt: Odoo Community Association
83+
:target: https://odoo-community.org
84+
85+
OCA, or the Odoo Community Association, is a nonprofit organization whose
86+
mission is to support the collaborative development of Odoo features and
87+
promote its widespread use.
88+
89+
.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px
90+
:target: https://github.com/simahawk
91+
:alt: simahawk
92+
93+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
94+
95+
|maintainer-simahawk|
96+
97+
This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/17.0/endpoint_auth_api_key>`_ project on GitHub.
98+
99+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

endpoint_auth_api_key/__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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2021 Camptocamp SA
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
{
5+
"name": "Endpoint Auth API key",
6+
"summary": """Provide API key auth for endpoints.""",
7+
"version": "18.0.1.0.0",
8+
"license": "LGPL-3",
9+
"development_status": "Alpha",
10+
"author": "Camptocamp, Odoo Community Association (OCA)",
11+
"maintainers": ["simahawk"],
12+
"website": "https://github.com/OCA/web-api",
13+
"depends": ["endpoint", "auth_api_key_group"],
14+
"demo": ["demo/api_key_demo.xml", "demo/endpoint_demo.xml"],
15+
"data": ["views/endpoint_view.xml"],
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<odoo noupdate="1">
3+
<record id="auth_api_key_demo" model="auth.api.key">
4+
<field name="name">Endpoint API key demo</field>
5+
<field name="key">cZ6dF2UQwNcm</field>
6+
<field name="user_id" ref="base.user_demo" />
7+
</record>
8+
9+
<record id="auth_api_key_demo2" model="auth.api.key">
10+
<field name="name">Endpoint API key demo 2</field>
11+
<field name="key">kV47QyOTC5mS</field>
12+
<field name="user_id" ref="base.user_demo" />
13+
</record>
14+
15+
<record id="auth_api_key_group_demo" model="auth.api.key.group">
16+
<field name="name">Demo Group 1</field>
17+
<field name="code">demo_group1</field>
18+
<field name="auth_api_key_ids" eval="[(4, ref('auth_api_key_demo'))]" />
19+
</record>
20+
</odoo>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record id="endpoint_demo_1" model="endpoint.endpoint">
4+
<field name="name">Demo Endpoint - auth api key</field>
5+
<field name="route">/demo/api/key</field>
6+
<field name="request_method">GET</field>
7+
<field name="auth_type">api_key</field>
8+
<field
9+
name="auth_api_key_group_ids"
10+
eval="[(4, ref('auth_api_key_group_demo'))]"
11+
/>
12+
<field name="exec_mode">code</field>
13+
<field name="code_snippet">
14+
result = {"response": Response("ok")}
15+
</field>
16+
</record>
17+
</odoo>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * endpoint_auth_api_key
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 17.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: endpoint_auth_api_key
17+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_endpoint__auth_api_key_group_ids
18+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_mixin__auth_api_key_group_ids
19+
msgid "Allowed API key groups"
20+
msgstr ""
21+
22+
#. module: endpoint_auth_api_key
23+
#: model:ir.model,name:endpoint_auth_api_key.model_endpoint_mixin
24+
msgid "Endpoint mixin"
25+
msgstr ""
26+
27+
#. module: endpoint_auth_api_key
28+
#: model_terms:ir.ui.view,arch_db:endpoint_auth_api_key.endpoint_mixin_form_view
29+
msgid "Manage groups"
30+
msgstr ""

endpoint_auth_api_key/i18n/it.po

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * endpoint_auth_api_key
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 16.0\n"
8+
"Report-Msgid-Bugs-To: \n"
9+
"PO-Revision-Date: 2024-04-02 12:35+0000\n"
10+
"Last-Translator: mymage <[email protected]>\n"
11+
"Language-Team: none\n"
12+
"Language: it\n"
13+
"MIME-Version: 1.0\n"
14+
"Content-Type: text/plain; charset=UTF-8\n"
15+
"Content-Transfer-Encoding: \n"
16+
"Plural-Forms: nplurals=2; plural=n != 1;\n"
17+
"X-Generator: Weblate 4.17\n"
18+
19+
#. module: endpoint_auth_api_key
20+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_endpoint__auth_api_key_group_ids
21+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_mixin__auth_api_key_group_ids
22+
msgid "Allowed API key groups"
23+
msgstr "Consenti gruppi chiavi API"
24+
25+
#. module: endpoint_auth_api_key
26+
#: model:ir.model,name:endpoint_auth_api_key.model_endpoint_mixin
27+
msgid "Endpoint mixin"
28+
msgstr "Mixin endpoint"
29+
30+
#. module: endpoint_auth_api_key
31+
#: model_terms:ir.ui.view,arch_db:endpoint_auth_api_key.endpoint_mixin_form_view
32+
msgid "Manage groups"
33+
msgstr "Gestione gruppi"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Translation of Odoo Server.
2+
# This file contains the translation of the following modules:
3+
# * endpoint_auth_api_key
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: Odoo Server 17.0\n"
8+
"Report-Msgid-Bugs-To: \n"
9+
"PO-Revision-Date: 2025-04-04 06:06+0000\n"
10+
"Last-Translator: xtanuiha <[email protected]>\n"
11+
"Language-Team: none\n"
12+
"Language: zh_CN\n"
13+
"MIME-Version: 1.0\n"
14+
"Content-Type: text/plain; charset=UTF-8\n"
15+
"Content-Transfer-Encoding: \n"
16+
"Plural-Forms: nplurals=1; plural=0;\n"
17+
"X-Generator: Weblate 5.10.2\n"
18+
19+
#. module: endpoint_auth_api_key
20+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_endpoint__auth_api_key_group_ids
21+
#: model:ir.model.fields,field_description:endpoint_auth_api_key.field_endpoint_mixin__auth_api_key_group_ids
22+
msgid "Allowed API key groups"
23+
msgstr "允许的API密钥组"
24+
25+
#. module: endpoint_auth_api_key
26+
#: model:ir.model,name:endpoint_auth_api_key.model_endpoint_mixin
27+
msgid "Endpoint mixin"
28+
msgstr "端点混入类"
29+
30+
#. module: endpoint_auth_api_key
31+
#: model_terms:ir.ui.view,arch_db:endpoint_auth_api_key.endpoint_mixin_form_view
32+
msgid "Manage groups"
33+
msgstr "管理组"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import endpoint_mixin
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021 Camptocamp SA
2+
# @author: Simone Orsi <[email protected]>
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
import werkzeug
6+
7+
from odoo import fields, models
8+
9+
10+
class EndpointMixin(models.AbstractModel):
11+
_inherit = "endpoint.mixin"
12+
13+
auth_api_key_group_ids = fields.Many2many(
14+
comodel_name="auth.api.key.group",
15+
string="Allowed API key groups",
16+
)
17+
18+
def _selection_auth_type(self):
19+
return super()._selection_auth_type() + [("api_key", "API key")]
20+
21+
def _validate_request(self, request):
22+
super()._validate_request(request)
23+
if self.auth_type == "api_key":
24+
self._validate_api_key(request)
25+
return
26+
27+
def _validate_api_key(self, request):
28+
key_id = request.auth_api_key_id
29+
if key_id not in self._allowed_api_key_ids():
30+
self._logger.error("API key %s not allowed on %s", key_id, self.route)
31+
raise werkzeug.exceptions.Forbidden()
32+
33+
def _allowed_api_key_ids(self):
34+
return self.auth_api_key_group_ids.auth_api_key_ids.ids
35+
36+
def action_view_api_key_groups(self):
37+
xmlid = "auth_api_key_group.auth_api_key_group_act_window"
38+
action = self.env["ir.actions.act_window"]._for_xml_id(xmlid)
39+
action["domain"] = [("id", "in", self.auth_api_key_group_ids.ids)]
40+
return action

0 commit comments

Comments
 (0)