class RHC::Auth::TokenStore

Public Class Methods

new(dir) click to toggle source
# File lib/rhc/auth/token_store.rb, line 3
def initialize(dir)
  @dir = dir
end

Public Instance Methods

clear() click to toggle source
# File lib/rhc/auth/token_store.rb, line 15
def clear
  Dir[File.join(@dir, "token_*")].
    each{ |f| File.delete(f) unless File.directory?(f) }.
    present?
end
get(login, server) click to toggle source
# File lib/rhc/auth/token_store.rb, line 7
def get(login, server)
  self[key(login,server)]
end
put(login, server, token) click to toggle source
# File lib/rhc/auth/token_store.rb, line 11
def put(login, server, token)
  self[key(login,server)] = token
end

Private Instance Methods

[](key) click to toggle source
# File lib/rhc/auth/token_store.rb, line 38
def [](key)
  s = IO.read(path(key)).presence
  s = s.strip.gsub(/[\n\r\t]/,'') if s
  s
rescue Errno::ENOENT
  nil
end
[]=(key, value) click to toggle source
# File lib/rhc/auth/token_store.rb, line 30
def []=(key, value)
  file = path(key)
  FileUtils.mkdir_p File.dirname(file)
  File.open(file, 'w'){ |f| f.write(value) }
  File.chmod(0600, file)
  value
end
filename(key) click to toggle source
# File lib/rhc/auth/token_store.rb, line 26
def filename(key)
  "token_#{Base64.encode64(Digest::MD5.digest(key)).gsub(/[^\w\@]/,'')}"
end
key(login, server) click to toggle source
# File lib/rhc/auth/token_store.rb, line 46
def key(login, server)
  "#{login || ''}@#{server}"
end
path(key) click to toggle source
# File lib/rhc/auth/token_store.rb, line 22
def path(key)
  File.join(@dir, filename(key))
end