-
-
Save zackkitzmiller/2fe1cc612cd77c31ad5e8b2ff8ea3703 to your computer and use it in GitHub Desktop.
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 hashlib | |
from urllib.parse import urlparse, urlencode, parse_qsl, unquote | |
SECRET = "s3kr3t!" | |
# Generation: | |
url_to_sign = "https://example.com/verify?email=zkitzmiller@grabango.com&auth_level=4" | |
def generate_signature(url): | |
hasher = hashlib.new('sha256') | |
hasher.update(SECRET.encode() + url.encode()) | |
return hasher.hexdigest() | |
def sign_url(url): | |
return url + "&sig=" + generate_signature(url) | |
# Verification | |
def verify_signature(signed_url): | |
# parse the url | |
parsed = urlparse(signed_url) | |
params = dict(parse_qsl(parsed.query)) | |
# get the signature | |
sig = params.pop('sig') | |
# generate a signature from provided url (remove signature) | |
param_str = unquote(urlencode(params)) | |
_url = parsed.scheme + "://" + parsed.netloc + parsed.path + "?" + param_str | |
# verify against generate_signature | |
return generate_signature(_url) == sig |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stub