Skip to content

Instantly share code, notes, and snippets.

@dchapman1988
Created June 30, 2011 16:40
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 dchapman1988/1056623 to your computer and use it in GitHub Desktop.
Save dchapman1988/1056623 to your computer and use it in GitHub Desktop.
module Sorcery
module Model
module Submodules
# The API Token submodule automatically generates a token that can
# be used to login to the website on every visit.
module APIToken
def self.included(base)
base.sorcery_config.class_eval do
attr_accessor :api_token_attribute_name, # the attribute in the model class.
:api_token_expires_at_attribute_name, # the expires attribute in the model class.
:api_token_good_for # how long (in seconds) the API token is good for.
end
base.sorcery_config.instance_eval do
@defaults.merg!(:@api_token_attribute_name => :api_token,
:@api_token_expires_at_attribute_name => :api_token_expires_at,
:@api_token_good_for => 3.154 * 10**8)
reset!
end
base.send(:include, InstanceMethods)
end
module InstanceMethods
def api_token!
config = sorcery_config
self.send(:"#{config.api_token_attribute_name}=", generate_random_token)
self.send(:"#{config.api_token_expires_at_attribute_name}=", Time.now + config.api_token_good_for)
self.save!(:validate => false)
end
def kill_api_token!
config = sorcery_config
self.send("#{config.api_token_attribute_name}=", nil)
self.send("#{config.api_token_expires_at_attribute_name}=", nil)
self.save!(:validate => false)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment