Commit 61e9feef authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Signing code now takes text from stdin

parent 352815f5
Loading
Loading
Loading
Loading
Loading

presigned.json

0 → 100644
+1 −0
Original line number Diff line number Diff line
{"@xmlns": "http://uri.etsi.org/03120/common/2019/10/Core", "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", "@xmlns:common": "http://uri.etsi.org/03120/common/2016/02/Common", "@xmlns:task": "http://uri.etsi.org/03120/common/2020/09/Task", "@xmlns:auth": "http://uri.etsi.org/03120/common/2020/09/Authorisation", "Header": {"SenderIdentifier": {"CountryCode": "XX", "UniqueIdentifier": "ACTOR01"}, "ReceiverIdentifier": {"CountryCode": "XX", "UniqueIdentifier": "ACTOR02"}, "TransactionIdentifier": "c02358b2-76cf-4ba4-a8eb-f6436ccaea2e", "Timestamp": "2015-09-01T12:00:00.000000Z", "Version": {"ETSIVersion": "V1.13.1", "NationalProfileOwner": "XX", "NationalProfileVersion": "v1.0"}}, "Payload": {"RequestPayload": {"ActionRequests": {"ActionRequest": [{"ActionIdentifier": 0, "CREATE": {"HI1Object": {"@xsi:type": "{http://uri.etsi.org/03120/common/2020/09/Authorisation}AuthorisationObject", "ObjectIdentifier": "7dbbc880-8750-4d3c-abe7-ea4a17646045", "CountryCode": "XX", "OwnerIdentifier": "ACTOR01", "auth:AuthorisationReference": "W000001", "auth:AuthorisationTimespan": {"auth:StartTime": "2015-09-01T12:00:00Z", "auth:EndTime": "2015-12-01T12:00:00Z"}}}}, {"ActionIdentifier": 1, "CREATE": {"HI1Object": {"@xsi:type": "{http://uri.etsi.org/03120/common/2020/09/Task}LITaskObject", "ObjectIdentifier": "2b36a78b-b628-416d-bd22-404e68a0cd36", "CountryCode": "XX", "OwnerIdentifier": "ACTOR01", "AssociatedObjects": {"AssociatedObject": ["7dbbc880-8750-4d3c-abe7-ea4a17646045"]}, "task:Reference": "LIID1", "task:TargetIdentifier": {"task:TargetIdentifierValues": {"task:TargetIdentifierValue": [{"task:FormatType": {"task:FormatOwner": "ETSI", "task:FormatName": "InternationalE164"}, "task:Value": "442079460223"}]}}, "task:DeliveryType": {"common:Owner": "ETSI", "common:Name": "TaskDeliveryType", "common:Value": "IRIandCC"}, "task:DeliveryDetails": {"task:DeliveryDestination": [{"task:DeliveryAddress": {"task:IPv4Address": "192.0.2.0"}}]}, "task:CSPID": {"CountryCode": "XX", "UniqueIdentifier": "RECVER01"}}}}]}}}, "signature": {"protected_header": "", "signature": ""}}
 No newline at end of file
+19 −2
Original line number Diff line number Diff line

import argparse
import logging
import sys
from jose import jws
from pathlib import Path

@@ -14,8 +16,23 @@ def insert_sig_block (j):
    return j

if __name__ == "__main__":
    json_path = Path("103120/examples/json/request1.json")
    json_text = json_path.read_text()
    parser = argparse.ArgumentParser()
    parser.add_argument('-v', '--verbose', action='count', help='Verbose logging (can be specified multiple times)')
    parser.add_argument('-i', '--input', type=argparse.FileType('r'), default=sys.stdin, help="Path to input file (if absent, stdin is used)")
    args = parser.parse_args()

    match args.verbose:
        case v if v and v >= 2:
            logging.basicConfig(level=logging.DEBUG)
        case 1:
            logging.basicConfig(level=logging.INFO)
        case _:
            logging.basicConfig(level=logging.WARNING)

    logging.debug(f"Arguments: {args}")

    json_text = args.input.read()
    args.input.close()
    
    j = json.loads(json_text)
    j = insert_sig_block(j)
+0 −6
Original line number Diff line number Diff line
@@ -39,9 +39,6 @@ if __name__ == "__main__":
    if signed_json_text.endswith('\n'): signed_json_text = signed_json_text[:-1]
    signed_json_text = signed_json_text.replace(protected_header, "").replace(signature, "")
    
    print ("\n\nPayload for verification ================================")
    print(signed_json_text)

    payload_bytes = signed_json_text.encode('utf-8')
    payload_token = base64.b64encode(payload_bytes).decode('ascii')

@@ -50,9 +47,6 @@ if __name__ == "__main__":
    payload_token = payload_token.replace('+','-')
    payload_token = payload_token.replace('/','_')

    print ("Payload bytes:", payload_bytes)
    print ("Payload token:", payload_token)

    token = protected_header + "." + payload_token + "." + signature
    result = jws.verify(token, key="secret_key", algorithms=['HS256'])