1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

F #4714: Use always the same filename for a token

This commit is contained in:
Jaime Melis 2016-09-09 10:57:18 +02:00
parent c6a64ce61d
commit 58d44f6327
2 changed files with 19 additions and 20 deletions

View File

@ -17,6 +17,8 @@
require 'one_helper'
require 'one_helper/onequota_helper'
require 'digest/md5'
# Interface for OpenNebula generated tokens.
class TokenAuth
def login_token(username, expire)
@ -26,7 +28,7 @@ end
class OneUserHelper < OpenNebulaHelper::OneHelper
ONE_AUTH = ENV['HOME']+'/.one/one_auth'
ONE_AUTH = ENV['HOME']+'/.one/one_auth'
def self.rname
"USER"
@ -40,6 +42,11 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
"User password"
end
def self.auth_file(auth_string)
auth_filename = Digest::MD5.hexdigest(auth_string)
ENV['HOME'] + "/.one/#{auth_filename}.token"
end
def self.password_to_str(arg, options)
if options[:read_file]
begin
@ -502,6 +509,4 @@ class OneUserHelper < OpenNebulaHelper::OneHelper
helper = OneQuotaHelper.new
helper.format_quota(user_hash['USER'], default_quotas, user.id)
end
end

View File

@ -32,7 +32,6 @@ require 'one_helper/oneuser_helper'
require 'one_helper/onequota_helper'
require 'uri'
require 'uuidtools'
cmd=CommandParser::CmdParser.new(ARGV) do
usage "`oneuser` <command> [<args>] [<options>]"
@ -601,33 +600,32 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end
token = token[0]
egid = user["LOGIN_TOKEN[TOKEN='#{token}']/EGID"]
# The token will be written to a file in $HOME/.one/<uuid>.token
auth_string = "#{user['NAME']}:#{token}"
auth_file = OneUserHelper::auth_file(auth_string)
# generate a random uuid
uuid = UUIDTools::UUID.random_create.to_s
auth_file = ENV['HOME'] + "/.one/#{uuid}.token"
begin
FileUtils.mkdir_p(File.dirname(auth_file))
rescue Errno::EEXIST
end
file = File.open(auth_file, "w")
file.write("#{user['NAME']}:#{token}")
file.write(auth_string)
file.close
File.chmod(0600, auth_file)
msg ="export ONE_AUTH=" + ENV['HOME'] + "/.one/#{uuid}.token"
msg ="export ONE_AUTH=" + auth_file
msg << "; export ONE_EGID=#{egid}" if egid
exit_with_code 0, msg
elsif options[:delete]
token = helper.find_token(user, options[:delete], true)
auth_string = "#{user['NAME']}:#{token}"
auth_file = OneUserHelper::auth_file(auth_string)
if token.count > 1
exit_with_code 1, "More than one token starting with '#{options[:delete]}' found."
elsif token.count == 0
@ -644,14 +642,10 @@ cmd=CommandParser::CmdParser.new(ARGV) do
puts "Token removed."
end
# Remove the token files
Dir[ENV['HOME'] + "/.one/*.token"].each do |f|
auth = File.read(f).strip
if auth == "#{user['NAME']}:#{token}"
puts "Removing #{f}"
File.unlink(f)
end
begin
File.unlink(auth_file)
puts "Removing #{auth_file}"
rescue Errno::ENOENT
end
0