2021-08-08 12:43:02 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2021-09-01 13:33:19 +00:00
|
|
|
from .models import GreenCertificate
|
2021-08-08 12:43:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def read_qrcode(file) -> str:
|
|
|
|
"""
|
|
|
|
Lit le contenu du fichier.
|
|
|
|
Peut être combiné avec zbar pour directement lire le contenu du QRCode.
|
|
|
|
"""
|
|
|
|
with file:
|
2021-08-23 11:18:59 +00:00
|
|
|
content = file.readline()
|
2021-08-08 12:43:02 +00:00
|
|
|
content = content.replace('\n', '')
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def analyse_qrcode(qrcode: str, additional_info: bool = False,
|
|
|
|
check_signature: bool = True) -> None:
|
|
|
|
"""
|
|
|
|
Analyse les données du QR code pour extraire les données
|
|
|
|
et vérifier la signature.
|
|
|
|
|
|
|
|
Si `additional_info` est vrai, les informations de
|
|
|
|
vaccination/test sont affichées.
|
2021-08-16 15:41:07 +00:00
|
|
|
|
|
|
|
Renvoie la validité du pass sous forme de booléen.
|
2021-08-08 12:43:02 +00:00
|
|
|
"""
|
|
|
|
|
2021-09-01 13:33:19 +00:00
|
|
|
certificate = GreenCertificate.load(qrcode)
|
2021-08-08 12:43:02 +00:00
|
|
|
|
|
|
|
if check_signature:
|
|
|
|
# Récupération du certificat utilisé
|
2021-09-01 13:33:19 +00:00
|
|
|
valid = certificate.check_signature()
|
2021-08-08 12:43:02 +00:00
|
|
|
else:
|
|
|
|
valid = True
|
|
|
|
print("Attention : la signature du QR code n'a pas été vérifiée.")
|
|
|
|
|
2021-09-01 13:33:19 +00:00
|
|
|
valid = valid and certificate.check()
|
2021-08-08 12:43:02 +00:00
|
|
|
|
|
|
|
if valid:
|
|
|
|
print("Pass sanitaire valide")
|
|
|
|
|
|
|
|
|
2021-09-01 13:33:19 +00:00
|
|
|
print("Nom :", certificate.family_name)
|
|
|
|
print("Prénom :", certificate.given_name)
|
|
|
|
print("Date de naissance :", certificate.date_of_birth)
|
2021-08-16 15:41:07 +00:00
|
|
|
|
2021-08-08 12:43:02 +00:00
|
|
|
if additional_info:
|
2021-08-16 15:41:07 +00:00
|
|
|
# TODO Meilleur affichage
|
2021-09-01 13:33:19 +00:00
|
|
|
print("Informations supplémentaires :",
|
|
|
|
json.dumps(certificate.result.__dict__, indent=2))
|
2021-08-08 12:43:02 +00:00
|
|
|
else:
|
|
|
|
print("Pass sanitaire invalide")
|
|
|
|
|
2021-08-16 15:41:07 +00:00
|
|
|
return valid
|
|
|
|
|
2021-08-08 12:43:02 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
|
|
|
parser.add_argument('file', nargs='?', type=argparse.FileType('r'),
|
|
|
|
default=sys.stdin,
|
|
|
|
help="QR Code à lire, en format texte.")
|
|
|
|
parser.add_argument('--full', '-f', action='store_true',
|
|
|
|
help="Affiche toutes les informations.")
|
|
|
|
parser.add_argument('--dontcheck', action='store_true',
|
|
|
|
help="Ne pas vérifier la signature.")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
qrcode = read_qrcode(args.file)
|
2021-08-17 18:10:49 +00:00
|
|
|
valid = analyse_qrcode(qrcode, args.full, not args.dontcheck)
|
|
|
|
|
|
|
|
exit(0 if valid else 2)
|