|
| 1 | +import base64 |
| 2 | +import calendar |
| 3 | +import json |
| 4 | +from datetime import datetime |
| 5 | +from io import BytesIO |
| 6 | + |
| 7 | +import qrcode |
| 8 | +import requests |
| 9 | +from qrcode.image.pil import PilImage |
| 10 | + |
| 11 | +from odoo import _, fields, models |
| 12 | +from odoo.exceptions import UserError |
| 13 | + |
| 14 | + |
| 15 | +class SPPRegistry(models.Model): |
| 16 | + _inherit = "res.partner" |
| 17 | + |
| 18 | + vc_qr_code = fields.Binary(string="VC QR Code", attachment=True) |
| 19 | + |
| 20 | + def _validate_vci_issuer(self, vci_issuer): |
| 21 | + if not vci_issuer: |
| 22 | + raise UserError("No issuer found.") |
| 23 | + |
| 24 | + if not vci_issuer.auth_sub_id_type_id: |
| 25 | + raise UserError("No auth sub id type found in the issuer.") |
| 26 | + |
| 27 | + if not vci_issuer.encryption_provider_id: |
| 28 | + raise UserError("No encryption provider found in the issuer.") |
| 29 | + |
| 30 | + def _issue_vc(self, vci_issuer): |
| 31 | + self.ensure_one() |
| 32 | + |
| 33 | + encryption_provider_id = vci_issuer.encryption_provider_id |
| 34 | + |
| 35 | + reg_id = self.env["g2p.reg.id"].search( |
| 36 | + [("partner_id", "=", self.id), ("id_type", "=", vci_issuer.auth_sub_id_type_id.id)] |
| 37 | + ) |
| 38 | + if not reg_id: |
| 39 | + raise UserError(f"No Registrant found with this ID Type: {vci_issuer.auth_sub_id_type_id.name}.") |
| 40 | + |
| 41 | + web_base_url = self.env["ir.config_parameter"].sudo().get_param("web.base.url").rstrip("/") |
| 42 | + |
| 43 | + url = f"{web_base_url}/api/v1/vci/.well-known/openid-credential-issuer/{vci_issuer.name}" |
| 44 | + |
| 45 | + credential_issuer_response = requests.get(url) |
| 46 | + |
| 47 | + issuer_data = credential_issuer_response.json() |
| 48 | + |
| 49 | + credential_issuer = f"{issuer_data['credential_issuer']}/api/v1/security" |
| 50 | + credentials_supported = issuer_data.get("credentials_supported", None) |
| 51 | + credential_request = credentials_supported[0] |
| 52 | + |
| 53 | + today = datetime.today() |
| 54 | + |
| 55 | + encryption_provider_id = vci_issuer.encryption_provider_id |
| 56 | + |
| 57 | + dict_data = { |
| 58 | + "sub": reg_id.value, |
| 59 | + "name": "OpenSPP", |
| 60 | + "iat": calendar.timegm(today.timetuple()), |
| 61 | + "scope": vci_issuer.scope, |
| 62 | + "iss": credential_issuer, |
| 63 | + } |
| 64 | + |
| 65 | + signed_data = encryption_provider_id.jwt_sign(data=dict_data) |
| 66 | + |
| 67 | + return self.env["g2p.openid.vci.issuers"].issue_vc(credential_request, signed_data) |
| 68 | + |
| 69 | + def _create_qr_code(self, data): |
| 70 | + qr = qrcode.QRCode( |
| 71 | + error_correction=qrcode.constants.ERROR_CORRECT_L, |
| 72 | + image_factory=PilImage, |
| 73 | + box_size=10, |
| 74 | + border=4, |
| 75 | + ) |
| 76 | + |
| 77 | + qr.add_data(data) |
| 78 | + qr.make(fit=True) |
| 79 | + |
| 80 | + img = qr.make_image() |
| 81 | + |
| 82 | + temp = BytesIO() |
| 83 | + img.save(temp, format="PNG") |
| 84 | + qr_img = base64.b64encode(temp.getvalue()) |
| 85 | + temp.close() |
| 86 | + |
| 87 | + return qr_img |
| 88 | + |
| 89 | + def registry_issue_card(self): |
| 90 | + self.ensure_one() |
| 91 | + |
| 92 | + form_id = self.env.ref("spp_openid_vci.issue_card_wizard").id |
| 93 | + action = { |
| 94 | + "name": _("Issue Card"), |
| 95 | + "type": "ir.actions.act_window", |
| 96 | + "view_mode": "form", |
| 97 | + "view_id": form_id, |
| 98 | + "view_type": "form", |
| 99 | + "res_model": "spp.issue.card.wizard", |
| 100 | + "target": "new", |
| 101 | + "context": { |
| 102 | + "default_partner_id": self.id, |
| 103 | + }, |
| 104 | + } |
| 105 | + return action |
| 106 | + |
| 107 | + def _issue_vc_qr(self, vci_issuer): |
| 108 | + self.ensure_one() |
| 109 | + |
| 110 | + self._validate_vci_issuer(vci_issuer) |
| 111 | + |
| 112 | + result = self._issue_vc(vci_issuer) |
| 113 | + |
| 114 | + qr_img = self._create_qr_code(json.dumps(result)) |
| 115 | + |
| 116 | + self.vc_qr_code = qr_img |
| 117 | + |
| 118 | + admission_form = self.env.ref("spp_openid_vci.action_generate_id_card").report_action(self) |
| 119 | + return admission_form |
0 commit comments