Class: U3dCore::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/u3d_core/credentials.rb

Constant Summary collapse

MAC_U3D_SERVER =
'u3d'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(user: nil, password: nil) ⇒ Credentials

Returns a new instance of Credentials.



30
31
32
33
34
# 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

Instance Method Details

#forget_credentials(force: false) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
# 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

#loginObject

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/u3d_core/credentials.rb', line 71

def 
  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

#passwordObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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
      
    end
  end

  return @password
end

#remember_credentialsObject



89
90
91
92
93
94
95
96
97
98
# 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_nameObject



113
114
115
# File 'lib/u3d_core/credentials.rb', line 113

def server_name
  MAC_U3D_SERVER
end

#userObject



36
37
38
39
40
41
42
43
44
45
46
# 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