Created
March 11, 2020 12:55
-
-
Save gillevinz/3706b02d0b22163f3150b30f833b2eab to your computer and use it in GitHub Desktop.
Answers SSO python example
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 hashlib | |
import json | |
import time | |
from Crypto.Cipher import AES | |
from django.http import HttpResponseRedirect | |
def _pad(bs, s): | |
return s + (bs - len(s) % bs) * chr(bs - len(s) % bs) | |
def _encryptUserData(data, key): | |
key = hashlib.sha1(key.encode()).digest()[0:16] | |
message = _pad(AES.block_size, data) | |
cipher = AES.new(key, AES.MODE_ECB) | |
return base64.urlsafe_b64encode(cipher.encrypt(message)).decode('utf-8') | |
def _omitPadding(token): | |
return token.replace('=', '') | |
def _getRedirectUrl(): | |
WIX_SECRET = '<your secret>' | |
WIX_KEY_ID = '<your key>' | |
user_data = { | |
'id': '<the user id>', | |
'email': '<the user email>', | |
'firstName': '<the user first name>', | |
'lastName': '<the user last name>', | |
'timestamp': int(round(time.time() * 1000)) # milliseconds required | |
} | |
answersRedirectUrl = 'redirectUrl' | |
token = _omitPadding(_encryptUserData(json.dumps(user_data), WIX_SECRET)) | |
success_url = answersRedirectUrl + '&token=' + token + '&key=' + WIX_KEY_ID | |
return HttpResponseRedirect(success_url) | |
_getRedirectUrl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment