Class: DBus::DBusCookieSHA1
- Inherits:
-
Authenticator
- Object
- Authenticator
- DBus::DBusCookieSHA1
- Defined in:
- lib/dbus/auth.rb
Overview
Authentication class using SHA1 crypto algorithm
Class for ‘CookieSHA1’ type authentication. Implements the AUTH DBUS_COOKIE_SHA1 mechanism.
Instance Method Summary collapse
-
#authenticate ⇒ Object
the autenticate method (called in stage one of authentification).
-
#data(hexdata) ⇒ Object
handles the interesting crypto stuff, check the rbus-project for more info: rbus.rubyforge.org/.
-
#hex_decode(encoded) ⇒ Object
decode hex to plain.
-
#hex_encode(plain) ⇒ Object
encode plain to hex.
-
#name ⇒ Object
returns the modules name.
Instance Method Details
#authenticate ⇒ Object
the autenticate method (called in stage one of authentification)
47 48 49 50 51 52 |
# File 'lib/dbus/auth.rb', line 47 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 |
#data(hexdata) ⇒ Object
handles the interesting crypto stuff, check the rbus-project for more info: rbus.rubyforge.org/
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dbus/auth.rb', line 60 def data(hexdata) require 'digest/sha1' data = hex_decode(hexdata) dlog "context, id, s_challenge: #{data.inspect}" # 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.length/2).map{|obj|obj=rand(255).to_s}.join # Search cookie file for id path = File.join(ENV['HOME'], '.dbus-keyrings', context) dlog "path: #{path.inspect}" #if $debug File.foreach(path) do |line| if line.index(id) == 0 # Right line of file, read cookie = line.split(' ')[2].chomp dlog "cookie: #{.inspect}" if $debug # Concatenate and encrypt to_encrypt = [s_challenge, c_challenge, ].join(':') dlog "s_challenge, c_challenge, cookie: #{to_encrypt.inspect}" 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 elog "Could not auth, will now exit." unless @retries <= 0 elog "Unable to locate cookie, retry in 1 second." @retries -= 1 sleep 1 data(hexdata) end |
#hex_decode(encoded) ⇒ Object
decode hex to plain
102 103 104 |
# File 'lib/dbus/auth.rb', line 102 def hex_decode(encoded) encoded.scan(/[[:xdigit:]]{2}/).map{|h|h.hex.chr}.join end |
#hex_encode(plain) ⇒ Object
encode plain to hex
96 97 98 99 |
# File 'lib/dbus/auth.rb', line 96 def hex_encode(plain) return nil if plain.nil? plain.to_s.unpack('H*')[0] end |
#name ⇒ Object
returns the modules name
55 56 57 |
# File 'lib/dbus/auth.rb', line 55 def name return 'DBUS_COOKIE_SHA1' end |