Skip to content

Commit 6c544df

Browse files
committed
Merge PR #130 into 19.0
Signed-off-by simahawk
2 parents 58de455 + d01007d commit 6c544df

21 files changed

Lines changed: 993 additions & 0 deletions

endpoint_auth_api_key/README.rst

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