Class DBus::DBusCookieSHA1
In: lib/dbus/auth.rb
Parent: Authenticator

Authentication class using SHA1 crypto algorithm

Class for ‘CookieSHA1’ type authentication. Implements the AUTH DBUS_COOKIE_SHA1 mechanism.

Methods

Public Instance methods

the autenticate method (called in stage one of authentification)

[Source]

# File lib/dbus/auth.rb, line 48
    def authenticate
      require 'etc'
      #number of retries we have for auth
      @retries = 1
      return "#{hex_encode(Etc.getlogin)}" #server expects it to be binary
    end

handles the interesting crypto stuff, check the rbus-project for more info: rbus.rubyforge.org/

[Source]

# File lib/dbus/auth.rb, line 61
    def data(hexdata)
      require 'digest/sha1'
      data = hex_decode(hexdata)
      # name of cookie file, id of cookie in file, servers random challenge  
      context, id, s_challenge = data.split(' ')
      # Random client challenge        
      c_challenge = Array.new(s_challenge.bytesize/2).map{|obj|obj=rand(255).to_s}.join
      # Search cookie file for id
      path = File.join(ENV['HOME'], '.dbus-keyrings', context)
      puts "DEBUG: path: #{path.inspect}" if $debug
      File.foreach(path) do |line|
        if line.index(id) == 0
          # Right line of file, read cookie
          cookie = line.split(' ')[2].chomp
          puts "DEBUG: cookie: #{cookie.inspect}" if $debug
          # Concatenate and encrypt
          to_encrypt = [s_challenge, c_challenge, cookie].join(':')
          sha = Digest::SHA1.hexdigest(to_encrypt)
          #the almighty tcp server wants everything hex encoded
          hex_response = hex_encode("#{c_challenge} #{sha}")
          # Return response
          response = [:AuthOk, hex_response]
          return response
        end
      end
      #a little rescue magic
      unless @retries <= 0
        puts "ERROR: Could not auth, will now exit." 
        puts "ERROR: Unable to locate cookie, retry in 1 second."
        @retries -= 1
        sleep 1
        data(hexdata)
      end
    end

decode hex to plain

[Source]

# File lib/dbus/auth.rb, line 103
    def hex_decode(encoded)
      encoded.scan(/[[:xdigit:]]{2}/).map{|h|h.hex.chr}.join
    end

encode plain to hex

[Source]

# File lib/dbus/auth.rb, line 97
    def hex_encode(plain)
      return nil if plain.nil?
      plain.to_s.unpack('H*')[0]
    end

returns the modules name

[Source]

# File lib/dbus/auth.rb, line 56
    def name
      return 'DBUS_COOKIE_SHA1'
    end

[Validate]