Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set and read raw cookie in Ruby/Sinatra

I have a bunch of domains such as store.example.com, user.example.com, intranet.example.com and each one can read cookies. However, I have a Sinatra app that is writing the cookies by doing something like session[:field] = value

When I inspect the cookie, I get something like this: BAh7CUkiD3Nlc3Npb25faWQGOgZFVEkiRTcwNmUwYTU1MjBmMzUyMTZiYzQ0%0ANjZhZDBkOGFmNmFhN2M3OGIxZTM3NGNjMDZiYmRkNDE2MmVlMDU2MWY2MTQG%0AOwBGSSIJY3NyZgY7AEZJIiVkMWUyYzI0NmEwZThiNjVhM2FlZGJjNWFhMzlk%0AYzc0YQY7AEZJIg10cmFja2luZwY7AEZ7B0kiFEhUVFBfVVNFUl9BR0VOVAY7%0AAFRJIi0xZDQ3MmI3NDZiMjFhNmJlNmYyMmUxMGU1MzI3N2Q5MmVlYmQ1N2Qx%0ABjsARkkiGUhUVFBfQUNDRVBUX0xBTkdVQUdFBjsAVEkiLTY2ZWFlOTcxNDky%0AOTM4YzJkY2MyZmIxZGRjOGQ3ZWMzMTk2MDM3ZGEGOwBGSSIQdXNlcl9zZWNy%0AZXQGOwBGSSIlMjg3NjdkMGVmNmZlOGUwMDIxMDRmODhiZDcwZTI1OTYGOwBU%0A--e8ca14be9bbe63226fd2d6e0f8c0b3946cc66fd0 which appears to be both base64 encoded and encrypted.

My question is how can I write a 'raw' cookie that can be easily read by all subdomains trying to consume it? It doesn't need to be encrypted and/or encoded.

like image 433
Noah Clark Avatar asked Jul 25 '14 17:07

Noah Clark


1 Answers

Use Sinatra Cookies http://www.sinatrarb.com/contrib/cookies.html

require "sinatra/cookies"
cookies[:something] = 'foobar'

You can also read raw cookies in the request object:

request.cookies 

As described in Accessing the Request Object

There is also the set_cookie method of the response object

response.set_cookie("my_cookie", :value => "value_of_cookie",
                    :domain => myDomain,
                    :path => myPath,
                    :expires => Date.new(2020,1,1))
  • https://stackoverflow.com/a/5078196/156746
  • http://craig-russell.co.uk/2013/01/31/persistent-cookies-sinatra.html#.U9KVFIBdWUA
like image 197
DiegoSalazar Avatar answered Sep 29 '22 13:09

DiegoSalazar