Class: DBus::Authentication::DBusCookieSHA1 Private

Inherits:
Mechanism
  • Object
show all
Defined in:
lib/dbus/auth.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Instance Method Summary collapse

Instance Method Details

#call(challenge) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

returns the modules name



72
73
74
# File 'lib/dbus/auth.rb', line 72

def name
  "DBUS_COOKIE_SHA1"
end