Skip to content

Instantly share code, notes, and snippets.

@ckraybill
Created March 25, 2011 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckraybill/887332 to your computer and use it in GitHub Desktop.
Save ckraybill/887332 to your computer and use it in GitHub Desktop.
Grab the user_id either from a param ?s=<encoded user id> || from the Rails session
module G5
module Strategies
class CookieAuthenticable < ::Warden::Strategies::Base
def valid?
request.params['s'] || request.cookies['_g5search_session']
end
def authenticate!
user_id = if request.params['s']
user_id = Util.decode(request.params['s'])[:user_id]
else
session = load_g5_session
session['warden.user.user.key'].last rescue 0
end
user = User.find_by_id(user_id)
user.nil? ? pass : success!(user)
end
protected
def load_g5_session
session_data = request.cookies['_g5search_session']
data, digest = session_data.split('--')
session = Marshal.load(decode64(data)) rescue {}
end
def decode64(data)
data.unpack("m").first
end
end
end
end
Warden::Strategies.add(:cookie_authenticatable, G5::Strategies::CookieAuthenticable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment