def _parse(self, secret): try: rawbytes = base64.b64decode(secret[::-1]) if rawbytes[0] != 1: raise ValueError('Invalid version') payload_len = struct.unpack(">H", rawbytes[1:3])[0] sig_len = struct.unpack(">H", rawbytes[3:5])[0] payload = rawbytes[5:5 + payload_len] signature = rawbytes[5 + payload_len:5 + payload_len + sig_len] pubkey = load_pem_public_key( base64.b64decode( self.event.settings.ticket_secrets_pretix_sig1_pubkey), Backend()) pubkey.verify(signature, payload) t = pretix_sig1_pb2.Ticket() t.ParseFromString(payload) return t except: return None
def generate_secret(self, item: Item, variation: ItemVariation = None, subevent: SubEvent = None, current_secret: str = None, force_invalidate=False): if current_secret and not force_invalidate: ticket = self._parse(current_secret) if ticket: unchanged = ( ticket.item == item.pk and ticket.variation == (variation.pk if variation else 0) and ticket.subevent == (subevent.pk if subevent else 0)) if unchanged: return current_secret t = pretix_sig1_pb2.Ticket() t.seed = get_random_string(9) t.item = item.pk t.variation = variation.pk if variation else 0 t.subevent = subevent.pk if subevent else 0 payload = t.SerializeToString() result = base64.b64encode(self._sign_payload(payload)).decode()[::-1] return result