| 1 |
import base64 |
| 2 |
import stdnum.pl.nip |
| 3 |
|
| 4 |
from odoo import api, fields, models |
| 5 |
from odoo.exceptions import UserError, ValidationError |
| 6 |
|
| 7 |
from odoo.addons.l10n_pl_edi.tools.ksef_api_service import KsefApiService |
| 8 |
from odoo.addons.l10n_pl_edi.tools.xades_signer import XadesSigner |
| 9 |
|
| 10 |
|
| 11 |
class ResConfigSettings(models.TransientModel): |
| 12 |
_inherit = 'res.config.settings' |
| 13 |
|
| 14 |
l10n_pl_edi_certificate = fields.Many2one( |
| 15 |
'certificate.certificate', |
| 16 |
"KSeF Certificate", |
| 17 |
compute='_compute_l10n_pl_edi_certificate', |
| 18 |
inverse='_set_l10n_pl_edi_certificate', |
| 19 |
readonly=False, |
| 20 |
) |
| 21 |
l10n_pl_edi_access_token = fields.Char( |
| 22 |
"KSeF Access Token", |
| 23 |
related='company_id.l10n_pl_edi_access_token', |
| 24 |
readonly=True, |
| 25 |
) |
| 26 |
l10n_pl_edi_register = fields.Boolean( |
| 27 |
"Allow KSeF integration", |
| 28 |
related='company_id.l10n_pl_edi_register', |
| 29 |
readonly=False, |
| 30 |
) |
| 31 |
|
| 32 |
@api.depends('company_id') |
| 33 |
def _compute_l10n_pl_edi_certificate(self): |
| 34 |
for config in self: |
| 35 |
config.l10n_pl_edi_certificate = config.company_id.l10n_pl_edi_certificate |
| 36 |
|
| 37 |
@api.onchange('l10n_pl_edi_register') |
| 38 |
def _l10n_pl_edi_reset(self): |
| 39 |
|
| 40 |
for config in self: |
| 41 |
if not config.l10n_pl_edi_register: |
| 42 |
config.l10n_pl_edi_certificate = False |
| 43 |
|
| 44 |
def _set_l10n_pl_edi_certificate(self): |
| 45 |
for config in self: |
| 46 |
company = config.company_id.sudo() |
| 47 |
if config.l10n_pl_edi_certificate: |
| 48 |
config._l10n_pl_edi_ksef_authenticate() |
| 49 |
company.l10n_pl_edi_register = True |
| 50 |
company.l10n_pl_edi_certificate = config.l10n_pl_edi_certificate |
| 51 |
else: |
| 52 |
|
| 53 |
company.write(dict.fromkeys([ |
| 54 |
'l10n_pl_edi_certificate', |
| 55 |
'l10n_pl_edi_access_token', |
| 56 |
'l10n_pl_edi_refresh_token', |
| 57 |
'l10n_pl_edi_register', |
| 58 |
'l10n_pl_edi_session_id', |
| 59 |
'l10n_pl_edi_session_key', |
| 60 |
'l10n_pl_edi_session_iv', |
| 61 |
], False)) |
| 62 |
|
| 63 |
def _l10n_pl_edi_ksef_authenticate(self): |
| 64 |
""" |
| 65 |
Orchestrates the entire authentication flow using the service. |
| 66 |
""" |
| 67 |
self.ensure_one() |
| 68 |
|
| 69 |
vat = self.company_id.vat |
| 70 |
if not vat or not vat.startswith('PL'): |
| 71 |
raise ValidationError(self.env._("A polish VAT number must be set on your company.")) |
| 72 |
if not self.l10n_pl_edi_certificate: |
| 73 |
raise ValidationError(self.env._("Please set up a valid KSeF Certificate, with its Private Key set")) |
| 74 |
|
| 75 |
nip = stdnum.pl.nip.compact(vat) |
| 76 |
|
| 77 |
ksef_service = KsefApiService(self.company_id) |
| 78 |
temp_token, ref_number = None, None |
| 79 |
|
| 80 |
if not self.l10n_pl_edi_certificate.private_key_id: |
| 81 |
raise ValidationError(self.env._( |
| 82 |
"The selected certificate record (%(name)s) is missing a private key.", |
| 83 |
name=self.l10n_pl_edi_certificate.display_name |
| 84 |
)) |
| 85 |
|
| 86 |
key_bytes = base64.b64decode(self.l10n_pl_edi_certificate.private_key_id.pem_key) |
| 87 |
cert_bytes = base64.b64decode(self.l10n_pl_edi_certificate.pem_certificate) |
| 88 |
if key_bytes and cert_bytes: |
| 89 |
signer = XadesSigner(key_bytes, cert_bytes) |
| 90 |
else: |
| 91 |
raise UserError(self.env._("KSeF certificate and private key are not set.")) |
| 92 |
|
| 93 |
challenge_data = ksef_service.get_challenge() |
| 94 |
challenge_code = challenge_data.get('challenge') |
| 95 |
|
| 96 |
signed_xml = signer.sign_authentication_challenge(challenge_code, nip) |
| 97 |
token_data = ksef_service.authenticate_xades(signed_xml) |
| 98 |
|
| 99 |
temp_token = token_data.get('authenticationToken', {}).get('token') |
| 100 |
ref_number = token_data.get('referenceNumber') |
| 101 |
if not temp_token or not ref_number: |
| 102 |
raise ValidationError(self.env._("Failed to initiate KSeF authentication.")) |
| 103 |
|
| 104 |
status_data = ksef_service.check_auth_status(ref_number, temp_token) |
| 105 |
if status_data.get('status', {}).get('code') != 200: |
| 106 |
raise ValidationError(self.env._("Authentication with KSeF failed.")) |
| 107 |
|
| 108 |
token_data = ksef_service.redeem_token(temp_token) |
| 109 |
access_token = token_data.get('accessToken', {}).get('token') |
| 110 |
refresh_token = token_data.get('refreshToken', {}).get('token') |
| 111 |
if not access_token or not refresh_token: |
| 112 |
raise ValidationError(self.env._("Failed to retrieve access or refresh tokens.")) |
| 113 |
|
| 114 |
self.company_id.sudo().write({ |
| 115 |
'l10n_pl_edi_access_token': access_token, |
| 116 |
'l10n_pl_edi_refresh_token': refresh_token, |
| 117 |
}) |