简体   繁体   English

通过 cookie 在 php 和 node.js 之间安全地共享数据

[英]Sharing data between php and node.js via cookie securely

I have a PHP site, and for real time updates and chat I have installed Node.js and its running fine.我有一个 PHP 站点,为了实时更新和聊天,我安装了 Node.js 并且运行良好。

Both PHP and Node.js have access to the same MySQL database. PHP 和 Node.js 都可以访问相同的 MySQL 数据库。

But the problem is to verify the identity of an user, who is already logged in to the PHP site.但问题是要验证已经登录到 PHP 站点的用户的身份。

I don't want to talk to the PHP app via any means, REST or not.我不想通过任何方式与 PHP 应用程序交谈,REST 与否。 As, to me this will defeat the same purpose of using Node.js, as then each Node.js request, a new PHP page request would be made.因为,对我来说,这将破坏使用 Node.js 的相同目的,因为每个 Node.js 请求都会发出一个新的 PHP 页面请求。

What I want is, a encryption and decryption method which is understood by both PHP and node.js我想要的是,PHP 和 node.js 都理解的加密和解密方法

So that I can set a cookie with the encrypted value for Node.js request, which will be at updates.mydomain.com subdomain.这样我就可以为 Node.js 请求设置一个带有加密值的 cookie,该请求将位于 updates.mydomain.com 子域中。 By reading the cookie, Node.js can decrypt its value and verify the user's identity.通过读取 cookie,Node.js 可以解密其值并验证用户身份。

So, my question: is there any encrypt and corresponding decrypt method that is supported via both PHP and Node.js, using same encryption key?所以,我的问题:是否有任何加密和相应的解密方法通过 PHP 和 Node.js 支持,使用相同的加密密钥?

Updates更新

Actually im not looking forward to decrypt it on client side:D as then the whole point of decryption is pointless.实际上我不期待在客户端解密它:D,因为那样解密的全部意义是没有意义的。 What i want to do is-我想做的是——

1) PHP to generate a cookie encrypted user info and use that cookie for a specific domain like updates.mydomain.com 1) PHP 生成 cookie 加密的用户信息并将该 cookie 用于特定域,例如 updates.mydomain.com

2) Then node.js will get the cookie for each subsequent request, and decrypt the data on server side, using the same encryption key. 2) 然后 node.js 将获取每个后续请求的 cookie,并使用相同的加密密钥在服务器端解密数据。

As u can see, that is why i wanted to know, if there is a common encryption/decryption system between PHP and node.js, so that encrypted data by one can be decrypted by the other and vice versa.如您所见,这就是为什么我想知道,PHP 和 node.js 之间是否存在通用的加密/解密系统,以便一个加密的数据可以被另一个解密,反之亦然。

This way i can securly transfer the current logged in users identity from PHP to node.js and i don't have to worry about session management of other kinds:)这样我可以安全地将当前登录的用户身份从 PHP 转移到 node.js 并且我不必担心 session 其他类型的管理:)

So in short, Encrypt by PHP -> Decrypt by Node.js -> get back same data.简而言之,通过 PHP 加密 -> 通过 Node.js 解密 -> 取回相同的数据。 Possible?可能的?

Thanks,谢谢,
Anjan安詹

The best approach here (imho) would be to store the session information in the database , and then make sure that Node can read the session cookie set by the PHP app.这里最好的方法(恕我直言)是将 session 信息存储在数据库中,然后确保 Node 可以读取 Z2FEC392304A5C23AC178DA22847F9 设置的 session cookie。

Then it can just check the session cookie against the database to make sure the user is logged in.然后它可以检查 session cookie 与数据库,以确保用户已登录。

Encryption example加密示例

If you really really want to use encryption, be aware that this'll probably be less secure and take more time to do than simply changing PHPs session backend, but here's an example that could probably work:如果您真的想使用加密,请注意,与简单地更改 PHP session 后端相比,这可能不太安全并且需要更多时间,但这里有一个可能可行的示例:

In PHP, encrypt the data:在 PHP 中,加密数据:

<?php
$encryption_key = 'somethingverysecretandpreferrablylong';
$vector = 'anotherlongwindedstring';
mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $encryption_key, 'My secret message', MCRYPT_MODE_CBC, $vector);
?>

And to decrypt in Node.js;并在 Node.js 中解密;

var crypto = require('crypto');
var decipher = crypto.createDecipher('aes-256-cbc','InmbuvP6Z8');
decipher.update(crypted_string_from_cookie,'hex','utf8');
decipher.final('utf8');

And please, please be careful with this code.请注意这段代码。 I am by no means a security expert, so if you want to encrypt anything sensitive, you should get peer review from someone who is:)我绝不是安全专家,所以如果你想加密任何敏感的东西,你应该得到同行评审:)

Another approach would be to use node.js as a the PHP session store itself.另一种方法是使用 node.js 作为 PHP session 存储本身。 Gonzalo Ayuso has an interesting article on it: Gonzalo Ayuso 有一篇有趣的文章:

http://gonzalo123.wordpress.com/2011/07/25/using-node-js-to-store-php-sessions/ http://gonzalo123.wordpress.com/2011/07/25/using-node-js-to-store-php-sessions/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM