Permalink
Browse files

Added support for cookies used as api key (for post_stream)

  • Loading branch information...
cykod committed May 16, 2011
1 parent 955bfb2 commit 0fc760ff1c26491a406d5ed9a970840bb7ab87e2
Showing with 15 additions and 0 deletions.
  1. +10 −0 app/models/end_user.rb
  2. +5 −0 app/models/end_user_cookie.rb
View
@@ -77,6 +77,7 @@ class EndUser < DomainModel
has_one :end_user_cache, :dependent => :delete, :class_name => 'EndUserCache'
has_many :end_user_cookies, :dependent => :delete_all, :class_name => 'EndUserCookie'
+ has_many :api_tokens, :class_name => 'EndUserCookie', :conditions => 'valid_until IS NULL'
has_many :end_user_actions, :dependent => :delete_all
@@ -169,6 +170,15 @@ def update_verification_string! #:nodoc:
self.update_attribute(:verification_string, Digest::SHA1.hexdigest(Time.now.to_s + rand(1000000000000).to_s)[0..12])
end
+ def self.find_by_api_token(api_token)
+ token = EndUserCookie.find_by_cookie(api_token, :conditions => { :valid_until => nil })
+ token.end_user if token
+ end
+
+ def api_token
+ @api_token ||= (self.api_tokens[0] && self.api_tokens[0].cookie) || EndUserCookie.generate_api_key(self)
+ end
+
# Is this a administrative ClientUser
def client_user?
client_user_id.to_i > 0
@@ -12,6 +12,11 @@ def self.generate_cookie(usr,valid_until=nil)
ck = self.create(:end_user_id => usr.id,:valid_until => valid_until || 2.weeks.from_now,:cookie => DomainModel.generate_hash)
ck.cookie
end
+
+ def self.generate_api_key(usr)
+ ck = self.create(:end_user_id => usr.id,:valid_until => nil,:cookie => DomainModel.generate_hash)
+ ck.cookie
+ end
def self.use_cookie(ck)
cookie = self.find_by_cookie(ck)

0 comments on commit 0fc760f

Please sign in to comment.