- Notifications
You must be signed in to change notification settings - Fork32
AS2 Library for building and parsing Messages and MDNs
License
NotificationsYou must be signed in to change notification settings
abhishek-ram/pyas2-lib
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A pure python library for building and parsing message as part of the AS2 messaging protocol. The message definitions follow the AS2 version 1.2 as defined in theRFC 4130.The library is intended to decouple the message construction/deconstruction from the web server/client implementation. The following functionality is part of this library:
- Compress, Sign and Encrypt the payload to be transmitted.
- Building the MIME Message from the processed payload.
- Building a signed MDN Messages for a received payload.
- Parsing a received MIME data and identifying if it as a Message or MDN.
- Decompress, Decrypt and Verify Signature of the received payload.
- Verify Signature of the received MDN and extract original message status.
Let us take a look at how we can use this library for building and parsing of AS2 Messages.
- First we would need to setup an organization and a partner
frompyas2lib.as2importOrganization,Partnermy_org=Organization(as2_name='my_unique_id',# Unique AS2 Id for this organizationsign_key=b'signature_key_bytes',# PEM/DER encoded private key for signaturesign_key_pass='password',# Password private key for signaturedecrypt_key=b'decrypt_key_bytes',# PEM/DER encoded private key for decryptiondecrypt_key_pass='password'# Password private key for decryption)a_partner=Partner(as2_name='partner_unique_id',# Unique AS2 Id of your partnersign=True,# Set to true for signing the messageverify_cert=b'verify_cert_bytes',# PEM/DER encoded certificate for verifying partner signaturesencrypt=True,# Set to true for encrypting the messageencrypt_cert=b'encrypt_cert_bytes',# PEM/DER encoded certificate for encrypting messagesmdn_mode='SYNC',# Expect to receive synchronous MDNs from this partnermdn_digest_alg='sha256'# Expect signed MDNs to be returned by this partner)
- The partner is now setup we can build an AS2 message
frompyas2lib.as2importMessagemsg=Message(sender=my_org,receiver=a_partner)msg.build(b'data_to_transmit')
- The message is built and now
msg.content
holds the message body andmessage.header
dictionary holds the message headers. These need to be passed to any http library for HTTP POSTing to the partner. - We expect synchronous MDNs so we need to process the response to our HTTP POST
frompyas2lib.as2importMdnmsg_mdn=Mdn()# Initialize an Mdn object# Call the parse method with the HTTP response headers + content and a function that returns the related `pyas2lib.as2.Messsage` object.status,detailed_status=msg_mdn.parse(b'response_data_with_headers',find_message_func)
- We parse the response mdn to get the status and detailed status of the message that was transmitted.
- We need to setup and HTTP server with an endpoint for receiving POST requests fro your partner.
- When a requests is received we need to first check if this is an Async MDN
frompyas2lib.as2importMdnmsg_mdn=Mdn()# Initialize an Mdn object# Call the parse method with the HTTP request headers + content and a function the returns the related `pyas2lib.as2.Messsage` object.status,detailed_status=msg_mdn.parse(request_body,find_message_fumc)
- If this is an Async MDN it will return the status of the original message.
- In case the request is not an MDN then
pyas2lib.exceptions.MDNNotFound
is raised, which needs to be catched and parse the request as a message.
frompyas2lib.as2importMessagemsg=Message()# Call the parse method with the HTTP request headers + content, a function to return the the related `pyas2lib.as2.Organization` object, a function to return the `pyas2lib.as2.Partner` object and a function to check for duplicates.status,exception,mdn=msg.parse(request_body,find_organization,find_partner,check_duplicate_msg)
- The parse function returns a 3 element tuple; the status of parsing, exception if any raised during parsing and an
pyas2lib.as2.Mdn
object for the message. - If the
mdn.mdn_mode
isSYNC
then themdn.content
andmdn.header
must be returned in the response. - If the
mdn.mdn_mode
isASYNC
then the mdn must be saved for later processing.
- Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
- Forkthe repository on GitHub to start making your changes to themaster branch (or branch off of it).
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself toAUTHORS.
About
AS2 Library for building and parsing Messages and MDNs
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published