-
-
Save arasmussen/02bb4ea19bff0e56861663ff4058c759 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 binascii | |
import hashlib | |
import json | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes | |
from django.conf import settings | |
from pinecast.helpers import gravatar | |
def get_canny_token(req): | |
if not req.user: | |
return None | |
user_data = { | |
'avatarURL': gravatar(req.user.email), | |
'email': req.user.email, | |
'id': req.user.id, | |
'name': req.user.email, | |
} | |
plaintext = json.dumps(user_data) | |
def pad_input(input): | |
block_size = 16 | |
if (len(input) % block_size == 0): | |
return input | |
padding_required = block_size - (len(input) % block_size) | |
return input.encode('utf-8') + padding_required * bytes([padding_required]) | |
dig = hashlib.md5(settings.CANNY_SSO_KEY.encode('utf-8')).digest() | |
cipher = Cipher(algorithms.AES(dig), modes.ECB(), backend=default_backend()) | |
encryptor = cipher.encryptor() | |
encrypted = encryptor.update(pad_input(plaintext)) + encryptor.finalize() | |
return binascii.hexlify(bytearray(encrypted)).decode('utf-8') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment