class U3dCore::Credentials

Constants

MAC_U3D_SERVER

Public Class Methods

new(user: nil, password: nil) click to toggle source
# File lib/u3d_core/credentials.rb, line 30
def initialize(user: nil, password: nil)
  @user = user
  @password = password
  @use_keychain = U3dCore::Globals.use_keychain?
end

Public Instance Methods

forget_credentials(force: false) click to toggle source
# File lib/u3d_core/credentials.rb, line 100
def forget_credentials(force: false)
  @password = nil
  ENV['U3D_PASSWORD'] = nil
  if force || UI.interactive?
    if Helper.mac? && @use_keychain && (force || UI.confirm('Remove credentials from the keychain?'))
      UI.message 'Deleting credentials from the keychain'
      Security::InternetPassword.delete(server: server_name)
    end
  elsif Helper.mac?
    UI.verbose 'Keychain may store invalid credentials for u3d'
  end
end
login() click to toggle source
# File lib/u3d_core/credentials.rb, line 71
def login
  UI.verbose 'Attempting to login'

  raise CredentialsError, 'No username specified' unless user

  while @password.nil?
    UI.verbose 'Password does not exist'
    raise CredentialsError, 'Password missing and context is not interactive. Please make sure it is correct' unless UI.interactive?
    @password = UI.password "Password for #{user}:"
  end

  if remember_credentials
    UI.success 'Credentials have been stored'
  else
    UI.important 'No credentials storage available'
  end
end
password() click to toggle source
# File lib/u3d_core/credentials.rb, line 48
def password
  @password ||= ENV['U3D_PASSWORD']

  if Helper.mac? && @use_keychain
    unless @password
      UI.message 'Fetching password from keychain'
      password_holder = Security::InternetPassword.find(server: server_name)
      @password = password_holder.password unless password_holder.nil?
    end
  end

  if @password.nil?
    UI.verbose 'Could not retrieve password'
    if U3dCore::Globals.do_not_login?
      UI.verbose 'Login disabled'
    else
      login
    end
  end

  return @password
end
remember_credentials() click to toggle source
# File lib/u3d_core/credentials.rb, line 89
def remember_credentials
  ENV['U3D_USER'] = @user
  ENV['U3D_PASSWORD'] = @password
  if Helper.mac? && @use_keychain
    UI.message 'Storing credentials to the keychain'
    return Security::InternetPassword.add(server_name, user, password)
  end

  return false
end
server_name() click to toggle source
# File lib/u3d_core/credentials.rb, line 113
def server_name
  MAC_U3D_SERVER
end
user() click to toggle source
# File lib/u3d_core/credentials.rb, line 36
def user
  @user ||= ENV['U3D_USER']

  while @user.to_s.empty?
    UI.verbose 'Username does not exist or is empty'
    raise CredentialsError, 'Username missing and context is not interactive. Please check that the environment variable is correct' unless UI.interactive?
    @user = UI.input 'Username for u3d:'
  end

  return @user
end