Skip to content

Instantly share code, notes, and snippets.

@tompave
Created May 24, 2014 15:44
Show Gist options
  • Save tompave/99725525b9e7cb8a372b to your computer and use it in GitHub Desktop.
Save tompave/99725525b9e7cb8a372b to your computer and use it in GitHub Desktop.
An example of auto expiring user tokens
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