Sha256: e36bbd320a9acdb7da4ddd716927cf1f231909057a779a0fd26992b1c02df02e

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module Flydata
  class Credentials
    include Helpers
    attr_reader :user, :password
    def initialize(user=nil, password=nil)
      read_credentials
      unless @authenticated
        @user = user || ENV['FLYDATA_LOGIN']
        @password = password || ENV['FLYDATA_PASSWORD']
      end
    end
    def authenticate!
      @authenticated = true
      write_credentials
    end
    def authenticated?
      @authenticated
    end
    def write_credentials
      dir = File.dirname(credentials_file)
      FileUtils.mkdir_p(dir)
      File.delete(credentials_file) if FileTest.exists?(credentials_file)
      File.open(credentials_file, 'w', 0400) do |out|
        out.puts @user
        out.puts encode(@password)
      end
      FileUtils.chmod(0700, dir)
    end
    def read_credentials
      if FileTest.exist?(credentials_file)
        File.open(credentials_file, 'r') do |f|
          @user = f.gets.chomp
          @password = decode(f.gets.chomp)
          @authenticated = true if @user and @password
        end
      end
    end
    private
    def credentials_file
      File.join(home_directory, '.flydata', 'credentials')
    end
    def encode(str)
      str.unpack('H*') # want to change more complecated? still useful for shoulder hacking.
    end
    def decode(str)
      [str].pack('H*')
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flydata-0.0.1.2011102401 lib/flydata/credentials.rb
flydata-0.0.1.2011102303 lib/flydata/credentials.rb
flydata-0.0.1.2011102302 lib/flydata/credentials.rb
flydata-0.0.1.2011102301 lib/flydata/credentials.rb
flydata-0.0.1.2011101801 lib/flydata/credentials.rb