Commit c515bb5e by Owo Sugiana

Kali pertama

0 parents
__pycache__
Auth API Key
============
Ini merupakan modul Odoo agar client bisa login menggunakan API Key yang
dikirim melalui header HTTP ``API-KEY``.
API Key yang dimaksud dibuat melalui menu *Preferensi, Keamanan Akun, KUNCI API
BARU*. Pada *Nama kunci Anda* tulis bebas misalnya: ``H2H ISO8583``, lalu klik
*BUAT KEY*.
Adapun contoh *controller*-nya sebagai berikut::
from odoo import http
from odoo.http import request
class AvailableInvoices(http.Controller):
@http.route(['/hello-world'], type='json', auth='api_key')
def show(self, **kwargs):
return dict(uid=request.env.user.id, message='Hello world.')
Lalu ini contoh client menggunakan ``curl``::
curl --header "Content-Type: application/json" \
--header "API-KEY: 5ed9ca599bdd1a97f5e05a8e011e440b528a372e" \
--data "{}" http://localhost:8069/hello-world
Teruji di Odoo 16.
from . import models
{
'name': 'Auth API Key',
'version': '0.1',
'author': 'opensipkd.com',
'license': 'LGPL-3',
'summary': 'Login menggunakan API Key',
}
from . import ir_http
# https://github.com/OCA/server-auth/blob/16.0/auth_api_key/models/ir_http.py
# https://gitlab.com/hazem.abdalazeem/project_api/-/blob/main/controllers/login.py
import logging
from odoo import models
from odoo.exceptions import AccessDenied
from odoo.http import request
_logger = logging.getLogger(__name__)
class IrHttp(models.AbstractModel):
_inherit = 'ir.http'
@classmethod
def _auth_method_api_key(cls):
headers = request.httprequest.environ
api_key = headers.get('HTTP_API_KEY')
if api_key:
uid = request.env['res.users.apikeys']._check_credentials(
scope='rpc', key=api_key)
if not uid:
raise AccessDenied()
request.update_env(user=uid)
return True
_logger.error('Butuh header API-KEY')
raise AccessDenied()
from . import test_auth_api_key
from odoo.tests.common import TransactionCase
class TestAuthApiKey(TransactionCase):
def test_auth(self):
example_uid = 6
example_api_key = '5ed9ca599bdd1a97f5e05a8e011e440b528a372e'
uid = self.env['res.users.apikeys']._check_credentials(
scope='rpc', key=example_api_key)
self.assertEqual(uid, example_uid)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!