I am currently trying to start a python server side by side with existing Apache / php. The special solution I came up with was to save $ _SESSION as an encrypted cookie, letting php authentication work as before, and then share the private key between the two servers.
Two questions:
- It's up to you how to handle session expiration events.
- I did not bother with the Initialization vector, believing that the timestamp from my expiration is enough. See fooobar.com/questions/7346 / ... why I might be too weak ...
Anyway, my encrypted cookie function php:
session_start(); $encryptToCookie = function($varToEncode,$cookieName,$privateKey){ $iv = $privateKey; $pass = $privateKey; $method = 'aes-128-cbc'; $encryptedString = openssl_encrypt(json_encode($varToEncode), $method, $pass, true, $iv); setcookie($cookieName,bin2hex($encryptedString)); }; $encryptToCookie($_SESSION,"sessionEncrypted","yohoyohoyohoyoho");
And my python side decryption:
from subprocess import Popen, PIPE import binascii def decrypt(encryptedString,privateKey): encryptedString = binascii.unhexlify(encryptedString) pathToOpenSSL = 'C:\pysrc\openssl\openssl.exe'
Hope this helps someone, remember all the usual things about creating private keys, and then be careful with them!
source share