Created
January 14, 2013 19:04
-
-
Save jedp/4532412 to your computer and use it in GitHub Desktop.
pretty-print a browserid assertion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
import json | |
from datetime import datetime | |
def decodeB64(b64input): | |
""" | |
Add padding to b64input if necessary. I think python's base64 | |
implementation is broken and this should not be necessary. | |
""" | |
out = b64input.strip() | |
lastBytesLen = len(out) % 4 | |
if lastBytesLen == 0: | |
pass | |
elif lastBytesLen == 3: | |
out += '=' | |
elif lastBytesLen == 2: | |
out += '==' | |
else: | |
print out, lastBytesLen | |
raise Exception("Bad base64 input; last group contained weird number of bytes.") | |
return base64.b64decode(out) | |
def decode(encoded): | |
return json.loads(decodeB64(encoded)) | |
def unpackAssertion(assertion): | |
# ignore signatures; we're not a verifier | |
header, claim, _, payload, _ = assertion.split('.'); | |
return {"header": decode(header), | |
"claim": decode(claim), | |
"payload": decode(payload)} | |
def printAssertionSummary(data): | |
email = data['claim']['principal'].get('email', '') | |
unverified_email = data['claim']['principal'].get('unverified-email', '') | |
exp = datetime.fromtimestamp(int(data['payload']['exp']) / 1000.0).strftime('%Y-%m-%d %H:%M:%S') | |
iss = data['claim']['iss'] | |
aud = data['payload']['aud'] | |
print """\ | |
Issuer: %(iss)s | |
Audience: %(aud)s | |
Email: %(email)s | |
Unverified Email: %(unverified_email)s | |
Expires: %(exp)s | |
""" % (vars()) | |
# Example: | |
# | |
# printAssertionSummary(unpackAssertion(assertion)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment