Skip to content

Updating to use tokens to replace cookies in bugzilla >= 4.4.3 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions lib/bugzilla/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,54 @@ class User < APITemplate
=end

def session(user, password)
# Using cookies prior to 4.4.3
fname = File.join(ENV['HOME'], '.ruby-bugzilla-cookie.yml')
if File.exist?(fname) && File.lstat(fname).mode & 0600 == 0600 then
conf = YAML.load(File.open(fname).read)
host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host)
cookie = conf[host]
unless cookie.nil? then
@iface.cookie = cookie
print "Using cookie\n"
yield
conf[host] = @iface.cookie
File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)}
return
end
end

# Using tokens for > 4.4.3
fname = File.join(ENV['HOME'], '.ruby-bugzilla-token.yml')
if File.exist?(fname) && File.lstat(fname).mode & 0600 == 0600 then
conf = YAML.load(File.open(fname).read)
host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host)
token = conf[host]
unless token.nil? then
@iface.token = token
yield
return
end
end

if user.nil? || password.nil? then
yield
else
login({'login'=>user, 'password'=>password, 'remember'=>true})
yield
logout

# Using cookies prior to 4.4.3
unless @iface.cookie.nil?
fname = File.join(ENV['HOME'], '.ruby-bugzilla-cookie.yml')
host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host)
conf = { host => @iface.cookie }
File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)}
end

# Using tokens for > 4.4.3
unless @iface.token.nil?
fname = File.join(ENV['HOME'], '.ruby-bugzilla-token.yml')
host = @iface.instance_variable_get(:@xmlrpc).instance_variable_get(:@host)
conf = { host => @iface.token }
File.open(fname, 'w') {|f| f.chmod(0600); f.write(conf.to_yaml)}
end
end

end # def session

=begin rdoc
Expand Down Expand Up @@ -92,7 +118,13 @@ def session(user, password)
def _login(cmd, *args)
raise ArgumentError, "Invalid parameters" unless args[0].kind_of?(Hash)

@iface.call(cmd,args[0])
res = @iface.call(cmd,args[0])
unless res['token'].nil?
@iface.token = res['token']
end

return res

end # def _login

def _logout(cmd, *args)
Expand Down
2 changes: 1 addition & 1 deletion lib/bugzilla/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

module Bugzilla

VERSION = "0.5.1"
VERSION = "0.5.3"

end # module Bugzilla
21 changes: 21 additions & 0 deletions lib/bugzilla/xmlrpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def call(cmd, params = {}, user = nil, password = nil)
params = {} if params.nil?
params['Bugzilla_login'] = user unless user.nil? || password.nil?
params['Bugzilla_password'] = password unless user.nil? || password.nil?
params['Bugzilla_token'] = @token unless @token.nil?
@xmlrpc.call(cmd, params)
end # def call

Expand All @@ -74,6 +75,26 @@ def cookie=(val)
@xmlrpc.cookie = val
end # def cookie

=begin rdoc

==== Bugzilla::XMLRPC#token

=end

def token
@token
end # def token

=begin rdoc

==== Bugzilla::XMLRPC#token=(val)

=end

def token=(val)
@token = val
end # def token

end # class XMLRPC

end # module Bugzilla