Created
June 30, 2011 16:40
-
-
Save dchapman1988/1056623 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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