Right way to share session across rails app to other service
Closed this issue · 2 comments
I've been able to reliably use this gem with Mongoid, MongoMapper and without an ODM for a single rails project. I am interested in being able to share the session object created in my rails app with another service (nodejs app).
I am unable to convert the BSON::Binary
data object to a JSON representation consistently. Is it possible for the data
object saved in Mongo to represent the document as a deserialized document, using this gem?
Querying mongo directly I get the following:
db.sessions.findOne();
{
"_id" : "AcXl2BJrZxJm92p3A9Pe8dHgZtc",
"data" : BinData(0,"BAh7CUkiDHVzZXJfaWQGOgZFRmkGSSIMdGVhbV9pZAY7AEZJIgYxBjsARkkiCmZsYXNoBjsARm86JUFjdGlvbkRpc3BhdGNoOjpGbGFzaDo6Rmxhc2hIYXNoCToKQHVzZWRvOghTZXQGOgpAaGFzaHsGOgxzdWNjZXNzVDoMQGNsb3NlZEY6DUBmbGFzaGVzewY7CkkiD0hleSEgSm9zdWUGOwBGOglAbm93bzokQWN0aW9uRGlzcGF0Y2g6OkZsYXNoOjpGbGFzaE5vdwY6C0BmbGFzaEAKSSIQX2NzcmZfdG9rZW4GOwBGSSIxVVpTVVZETktQeGJNY1ZtMG1LTmZYL01FcHdPQW1BR2oxSHFwcnRQUllSWT0GOwBG"),
"created_at" : ISODate("2012-12-04T20:25:42.459Z"),
"updated_at" : ISODate("2012-12-04T20:25:42.490Z")
}
In MongoMapper I fetch the same object:
f = MongoMapperStore::Session.last
=> #<ActionDispatch::Session::MongoMapperStore::Session _id: "AcXl2BJrZxJm92p3A9Pe8dHgZtc", created_at: Tue, 04 Dec 2012 20:25:42 UTC +00:00, data: BSON::Binary:70350084844240, updated_at: Tue, 04 Dec 2012 20:25:42 UTC +00:00>
And follow that up with just referencing the data object.
data = f[:data]
=> BSON::Binary:70350084844240
Things get confusing here:
str = data.to_s
=> "\x04\b{\tI"\fuser_id\x06:\x06EFi\x06I"\fteam_id\x06;\x00FI"\x061\x06;\x00FI"\nflash\x06;\x00Fo:%ActionDispatch::Flash::FlashHash\t:\n@usedo:\bSet\x06:\n@hash{\x06:\fsuccessT:\f@closedF:\r@flashes{\x06;\nI"\x0FHey! Josue\x06;\x00F:\t@nowo:$ActionDispatch::Flash::FlashNow\x06:\v@flash@\nI"\x10_csrf_token\x06;\x00FI"1UZSUVDNKPxbMcVm0mKNfX/MEpwOAmAGj1HqprtPRYRY=\x06;\x00F"
Leading to a DecodeError
ActiveSupport::JSON.decode str
MultiJson::DecodeError: 795: unexpected token
Ideally I need to determine a way to share sessions, but it would also be nice to understand what is going on.
The reason the storage is not in JSON is because we have to be able to store arbitrary Ruby objects the easiest and most robust way to do that is with Ruby's marshaling support. http://www.skorks.com/2010/04/serializing-and-deserializing-objects-with-ruby/
Try...
s = MongoMapperStore::Session.last
s.send(:unpack, s.data) # unfortunately this method is private due to the session store API
Thanks for the insight.