Class: FbRails::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/fb_rails/cookie.rb

Class Method Summary collapse

Class Method Details

.decode(cookie_string, secret) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fb_rails/cookie.rb', line 6

def decode(cookie_string, secret)
  return if cookie_string.blank?
  cookie_string = Rack::Utils.unescape(cookie_string)

  hash = Rack::Utils.parse_query(cookie_string.gsub(/^\"|\"$/, ''))

  payload = ''
  hash.sort.each do |key, value|
    payload += "#{key}=#{value}" if key != 'sig'
  end

  md5 = Digest::MD5.hexdigest("#{payload}#{secret}")
  if md5 == hash['sig']
    hash
  end
end

.encode(attributes, secret) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/fb_rails/cookie.rb', line 23

def encode(attributes, secret)
  sorted_pairs = attributes.sort.map { |key, value| "#{key}=#{value}" }
  param_string = sorted_pairs.join('&')
  payload = sorted_pairs.join('')
  sig = Digest::MD5.hexdigest("#{payload}#{secret}")
  "\"#{param_string}&sig=#{sig}\""
end