|
1 |
| -# -*- coding: utf-8 -*- |
2 |
| -# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 1 | +import json |
| 2 | +import math |
3 | 3 | from collections import defaultdict
|
4 |
| -from urllib3.util.ssl_ import create_urllib3_context |
5 |
| -from urllib3.contrib.pyopenssl import inject_into_urllib3 |
6 |
| -from OpenSSL.crypto import load_certificate, load_privatekey, FILETYPE_PEM |
7 | 4 |
|
8 |
| -from odoo import fields, models, _ |
| 5 | +import requests |
| 6 | + |
| 7 | +from odoo import _, fields, models |
9 | 8 | from odoo.exceptions import UserError
|
10 | 9 | from odoo.tools import html_escape, zeep
|
11 | 10 | from odoo.tools.float_utils import float_round
|
12 | 11 |
|
13 |
| -import base64 |
14 |
| -import math |
15 |
| -import json |
16 |
| -import requests |
17 |
| - |
| 12 | +from odoo.addons.certificate.tools import CertificateAdapter |
18 | 13 |
|
19 | 14 | # Custom patches to perform the WSDL requests.
|
20 | 15 | # Avoid failure on servers where the DH key is too small
|
21 | 16 | EUSKADI_CIPHERS = "DEFAULT:!DH"
|
22 | 17 |
|
23 | 18 |
|
24 |
| -class PatchedHTTPAdapter(requests.adapters.HTTPAdapter): |
25 |
| - """ An adapter to block DH ciphers which may not work for the tax agencies called""" |
26 |
| - |
27 |
| - def init_poolmanager(self, *args, **kwargs): |
28 |
| - # OVERRIDE |
29 |
| - inject_into_urllib3() |
30 |
| - kwargs['ssl_context'] = create_urllib3_context(ciphers=EUSKADI_CIPHERS) |
31 |
| - return super().init_poolmanager(*args, **kwargs) |
32 |
| - |
33 |
| - def cert_verify(self, conn, url, verify, cert): |
34 |
| - # OVERRIDE |
35 |
| - # The last parameter is only used by the super method to check if the file exists. |
36 |
| - # In our case, cert is an odoo record 'certificate.certificate' so not a path to a file. |
37 |
| - # By putting 'None' as last parameter, we ensure the check about TLS configuration is |
38 |
| - # still made without checking temporary files exist. |
39 |
| - super().cert_verify(conn, url, verify, None) |
40 |
| - conn.cert_file = cert |
41 |
| - conn.key_file = None |
42 |
| - |
43 |
| - def get_connection(self, url, proxies=None): |
44 |
| - # OVERRIDE |
45 |
| - # Patch the OpenSSLContext to decode the certificate in-memory. |
46 |
| - conn = super().get_connection(url, proxies=proxies) |
47 |
| - context = conn.conn_kw['ssl_context'] |
48 |
| - |
49 |
| - def patched_load_cert_chain(l10n_es_odoo_certificate, keyfile=None, password=None): |
50 |
| - certificate = l10n_es_odoo_certificate |
51 |
| - cert_obj = load_certificate(FILETYPE_PEM, base64.b64decode(certificate.sudo().pem_certificate)) |
52 |
| - pkey_obj = load_privatekey(FILETYPE_PEM, base64.b64decode(certificate.sudo().private_key_id.pem_key)) |
53 |
| - |
54 |
| - context._ctx.use_certificate(cert_obj) |
55 |
| - context._ctx.use_privatekey(pkey_obj) |
56 |
| - |
57 |
| - context.load_cert_chain = patched_load_cert_chain |
58 |
| - |
59 |
| - return conn |
60 |
| - |
61 |
| - |
62 | 19 | class AccountEdiFormat(models.Model):
|
63 | 20 | _inherit = 'account.edi.format'
|
64 | 21 |
|
@@ -511,7 +468,7 @@ def _l10n_es_edi_call_web_service_sign_common(self, invoices, info_list, cancel=
|
511 | 468 |
|
512 | 469 | session = requests.Session()
|
513 | 470 | session.cert = company.l10n_es_sii_certificate_id
|
514 |
| - session.mount('https://', PatchedHTTPAdapter()) |
| 471 | + session.mount('https://', CertificateAdapter(ciphers=EUSKADI_CIPHERS)) |
515 | 472 |
|
516 | 473 | client = zeep.Client(connection_vals['url'], operation_timeout=60, timeout=60, session=session)
|
517 | 474 |
|
|
0 commit comments