Created
May 24, 2014 15:44
-
-
Save tompave/99725525b9e7cb8a372b to your computer and use it in GitHub Desktop.
An example of auto expiring user tokens
This file contains 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
require 'digest/md5' | |
class User | |
attr_accessor :name, :token_valid_for | |
attr_reader :pwd_hash | |
def initialize(name, pwd) | |
self.name = name | |
self.password = pwd | |
self.token_valid_for = 7 | |
end | |
def password=(pwd) | |
@pwd_hash = digest(pwd) | |
end | |
def api_token | |
digest(name + expirable_salt + time_dependent_salt) | |
end | |
private | |
def digest(string) | |
Digest::MD5.hexdigest string | |
end | |
def expirable_salt | |
pwd_hash[0,10] | |
end | |
def time_dependent_salt | |
t = Time.now | |
t.year.to_s + (t.yday / token_valid_for).to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment