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
 78 def call(challenge)
 79   if challenge.nil?
 80     require "etc"
 81     # number of retries we have for auth
 82     @retries = 1
 83     return [:MechContinue, Etc.getlogin]
 84   end
 85 
 86   require "digest/sha1"
 87   # name of cookie file, id of cookie in file, servers random challenge
 88   context, id, s_challenge = challenge.split(" ")
 89   # Random client challenge
 90   c_challenge = 1.upto(s_challenge.bytesize / 2).map { rand(255).to_s }.join
 91   # Search cookie file for id
 92   path = File.join(ENV["HOME"], ".dbus-keyrings", context)
 93   DBus.logger.debug "path: #{path.inspect}"
 94   File.foreach(path) do |line|
 95     if line.start_with?(id)
 96       # Right line of file, read cookie
 97       cookie = line.split(" ")[2].chomp
 98       DBus.logger.debug "cookie: #{cookie.inspect}"
 99       # Concatenate and encrypt
100       to_encrypt = [s_challenge, c_challenge, cookie].join(":")
101       sha = Digest::SHA1.hexdigest(to_encrypt)
102       # Return response
103       response = [:MechOk, "#{c_challenge} #{sha}"]
104       return response
105     end
106   end
107   return if @retries <= 0
108 
109   # a little rescue magic
110   puts "ERROR: Could not auth, will now exit."
111   puts "ERROR: Unable to locate cookie, retry in 1 second."
112   @retries -= 1
113   sleep 1
114   call(challenge)
115 end
name() click to toggle source

returns the modules name

   # File lib/dbus/auth.rb
72 def name
73   "DBUS_COOKIE_SHA1"
74 end