【发布时间】:2015-09-04 05:53:29
【问题描述】:
我正在使用Clojure / Friend,我想从相邻的 node.js 服务器劫持一个会话 :)
我的意思是有一个node.js + Express 3 服务器使用connect-redis 将会话存储到redis 数据库中。 我可以访问 node.js 源代码,因此可以访问传递给用于生成 sid 的快速会话的秘密。我将连接部分修改为如下所示(之前使用了内置的内存存储):
var session = require('express-session')
, RedisStore = require('connect-redis')(session)
, redis = require('redis')
, client = redis.createClient();
...
app.use(session({
secret: "super-secret-key",
store: new RedisStore({client : client,
ttl : 60*60*24,
prefix : 'sess:'})
}));
我可以看到存储在 Redis 中的会话:
// using redis-cli
KEYS *
1) "sess:yNV3MQOOTZSEgzl0RH2lyWVW"
登录页面位于应用程序的 node.js 端。但是 API 的路由在 Node.js 和 Clojure 之间共享(通过 nginx),所以我在 Compojure / Friend 应用程序的传入请求中接收到 cookie。
我正在努力让朋友自定义凭据功能正确:
(defn custom-credential [{:keys [username password] :as trial}]
(let [cookie (...)
redis-session (...)
is-signature-valid (...)]
;; I can get all the above bindings just fine
;; ie. I extract the cookie from the request
;; I read the cookie ID and fetch the cookie content from Redis
;; I extract the signature of the cookie and check it (I replicated [node-cookie-signature][8])
(when is-signature-valid
{:identity "cookie-id"})))
(defn secured-routes [unsecured-routes]
(friend/authenticate
unsecured-routes
{:allow-anon? true
:credential-fn custom-credential
:workflows [(workflows/http-basic)]}))
我有几个问题:
即使我的身份验证函数返回
{:identity ..}映射,我也会被重定向到登录页面但我不知道如何将其插入 Friend 以使 cookie 也能正常工作。使用默认的
memory-store然后 cookie 被默认的 ring-cookie 替换(显然不是我想要的)。我尝试使用 Redis 作为后端编写自定义 cookie 存储,但我需要编写 JSON 以保持节点兼容性,并且我丢失了::user信息。
注意:此时我非常灵活,所以如果我应该使用 Friend 或 Compojure 以外的其他东西,我也会接受。
抱歉,帖子太长了,我真的希望有人能提供帮助。
【问题讨论】:
-
更新评论:我做了一个自定义的 Redis-store 实现,当存储的数据正确时返回一个朋友会话映射。这意味着我不需要任何工作流程。这对我来说听起来不对,有人可以对此发表评论吗?
标签: node.js session cookies clojure