在 Node.js 和 Clojure 之间共享用户会话

share user sessions between Node.js and Clojure

我正在使用 Clojure / Friend,我想劫持邻近 node.js 服务器的会话 :)

我的意思是有一个node.js + Express 3 server that stores sessions into a redis database, using connect-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)]}))

我有几个问题:

注意:在这一点上我非常灵活,所以如果我应该使用 FriendCompojure 以外的其他东西,我也会接受。

抱歉了这么久post,真心希望有人能帮上忙。

How do I get the cookie contents on my validating function ?

环请求将包含客户端发送的任何 cookie,但如果您的 Clojure 服务器位于不同的域中,它可能不会发送这些 cookie。

how do I validate the user from the content of the cookie ? Ie. how can I make sure the cookie has not been altered (ie. replicate what the connect-redis is doing with the secret) ?

最佳做法是将 cookie 设为随机数,并将会话详细信息存储在 redis 中。如果这就是您的节点服务器的工作方式,那么您只需在 Redis 中查找详细信息即可。或者,如果客户端提供了一个加密的会话 cookie,那么您将需要以与 Node 相同的方式对其进行解密。

或者,您可能想要查看 JWT 身份验证令牌,它们旨在由许多不同的服务器共享和验证。