Skip to content

Commit

Permalink
auth_api_key: get rid of server env
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed Sep 24, 2021
1 parent dead75f commit 4af669c
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 50 deletions.
1 change: 0 additions & 1 deletion auth_api_key/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-auth",
"development_status": "Beta",
"depends": ["server_environment"],
"data": ["security/ir.model.access.csv", "views/auth_api_key.xml"],
"demo": [],
}
21 changes: 0 additions & 21 deletions auth_api_key/models/auth_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class AuthApiKey(models.Model):
_name = "auth.api.key"
_inherit = "server.env.mixin"
_description = "API Key"

name = fields.Char(required=True)
Expand All @@ -27,26 +26,6 @@ class AuthApiKey(models.Model):

_sql_constraints = [("name_uniq", "unique(name)", "Api Key name must be unique.")]

def _server_env_section_name(self):
"""Name of the section in the configuration files
We override the default implementation to keep the compatibility
with the previous implementation of auth_api_key. The section name
into the configuration file must be formatted as
'api_key_{name}'
"""
self.ensure_one()
return "api_key_{}".format(self.name)

@property
def _server_env_fields(self):
base_fields = super()._server_env_fields
api_key_fields = {"key": {}}
api_key_fields.update(base_fields)
return api_key_fields

@api.model
def _retrieve_api_key(self, key):
return self.browse(self._retrieve_api_key_id(key))
Expand Down
9 changes: 1 addition & 8 deletions auth_api_key/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
The api key menu is available into Settings > Technical in debug mode.
By default, when you create an API key, the key is saved into the database.
It is also possible to provide the value of this key via the configuration
file. This can be very useful to avoid mixing your keys between your various
environments when restoring databases. All you have to do is to add a new
section to your configuration file according to the following convention:

.. code-block:: ini
[api_key_<Record Name>]
key=my_api_key
If you want to manage them via serve environment settings use `auth_api_key_server_env`.
8 changes: 6 additions & 2 deletions auth_api_key/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Authenticate http requests from an API key.

API keys are codes passed in (in the http header API-KEY) by programs calling an API in order to identify -in this case- the calling program's user.
API keys are codes passed in (in the http header API-KEY)
by programs calling an API in order to identify -in this case- the calling program's user.

Take care while using this kind of mechanism since information into http headers are visible in clear. Thus, use it only to authenticate requests from known sources. For unknown sources, it is a good practice to filter out this header at proxy level.
Take care while using this kind of mechanism since information into http headers are visible in clear.
Thus, use it only to authenticate requests from known sources.

For unknown sources, it is a good practice to filter out this header at proxy level.
18 changes: 0 additions & 18 deletions auth_api_key/tests/test_auth_api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from odoo.exceptions import AccessError, ValidationError
from odoo.tests.common import SavepointCase

from odoo.addons.server_environment import serv_config


class TestAuthApiKey(SavepointCase):
@classmethod
Expand All @@ -15,29 +13,13 @@ def setUpClass(cls, *args, **kwargs):
cls.api_key_good = cls.AuthApiKey.create(
{"name": "good", "user_id": cls.demo_user.id, "key": "api_key"}
)
cls.api_key_from_env = cls.AuthApiKey.create(
{"name": "from_env", "key": "dummy", "user_id": cls.demo_user.id}
)
cls.api_key_from_env.refresh()
serv_config.add_section("api_key_from_env")
serv_config.set("api_key_from_env", "key", "api_key_from_env")

def test_lookup_key_from_db(self):
demo_user = self.env.ref("base.user_demo")
self.assertEqual(
self.env["auth.api.key"]._retrieve_uid_from_api_key("api_key"), demo_user.id
)

def test_lookup_key_from_env(self):
self.assertEqual(
self.env["auth.api.key"]._retrieve_uid_from_api_key("api_key_from_env"),
self.demo_user.id,
)
with self.assertRaises(ValidationError):
# dummy key must be replace with the one from env and
# therefore should be unusable
self.env["auth.api.key"]._retrieve_uid_from_api_key("dummy")

def test_wrong_key(self):
with self.assertRaises(ValidationError), self.env.cr.savepoint():
self.env["auth.api.key"]._retrieve_uid_from_api_key("api_wrong_key")
Expand Down

0 comments on commit 4af669c

Please sign in to comment.