class DBus::Authentication::DBusCookieSHA1

Implements the AUTH DBUS_COOKIE_SHA1 mechanism. dbus.freedesktop.org/doc/dbus-specification.html#auth-mechanisms-sha

Public Instance Methods

call(challenge) click to toggle source

First we are called with nil and we reply with our username. Then we prove that we can read that user’s cookie file.

# File lib/dbus/auth.rb, line 78
def call(challenge)
  if challenge.nil?
    require "etc"
    # number of retries we have for auth
    @retries = 1
    return [:MechContinue, Etc.getlogin]
  end

  require "digest/sha1"
  # name of cookie file, id of cookie in file, servers random challenge
  context, id, s_challenge = challenge.split(" ")
  # Random client challenge
  c_challenge = 1.upto(s_challenge.bytesize / 2).map { rand(255).to_s }.join
  # Search cookie file for id
  path = File.join(ENV["HOME"], ".dbus-keyrings", context)
  DBus.logger.debug "path: #{path.inspect}"
  File.foreach(path) do |line|
    if line.start_with?(id)
      # Right line of file, read cookie
      cookie = line.split(" ")[2].chomp
      DBus.logger.debug "cookie: #{cookie.inspect}"
      # Concatenate and encrypt
      to_encrypt = [s_challenge, c_challenge, cookie].join(":")
      sha = Digest::SHA1.hexdigest(to_encrypt)
      # Return response
      response = [:MechOk, "#{c_challenge} #{sha}"]
      return response
    end
  end
  return if @retries <= 0

  # a little rescue magic
  puts "ERROR: Could not auth, will now exit."
  puts "ERROR: Unable to locate cookie, retry in 1 second."
  @retries -= 1
  sleep 1
  call(challenge)
end
name() click to toggle source

returns the modules name

# File lib/dbus/auth.rb, line 72
def name
  "DBUS_COOKIE_SHA1"
end