Module: Terjira::Client::AuthOptionBuilder

Included in:
Base
Defined in:
lib/terjira/client/auth_option_builder.rb

Constant Summary collapse

AUTH_CACHE_KEY =
'auth'.freeze

Instance Method Summary collapse

Instance Method Details

#auth_file_cacheObject



57
58
59
# File 'lib/terjira/client/auth_option_builder.rb', line 57

def auth_file_cache
  @auth_file_cache ||= Terjira::FileCache.new('profile')
end

#build_auth_options(options = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/terjira/client/auth_option_builder.rb', line 9

def build_auth_options(options = {})
  cache_key = options[:cache_key] || AUTH_CACHE_KEY
  auth_file_cache.fetch cache_key do
    build_auth_options_by_tty(options)
  end
end

#build_auth_options_by_cached(options = {}) ⇒ Object



16
17
18
19
# File 'lib/terjira/client/auth_option_builder.rb', line 16

def build_auth_options_by_cached(options = {})
  cache_key = options[:cache_key] || AUTH_CACHE_KEY
  auth_file_cache.get(cache_key)
end

#build_auth_options_by_tty(options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/terjira/client/auth_option_builder.rb', line 25

def build_auth_options_by_tty(options = {})
  puts 'Login will be required...'
  prompt = TTY::Prompt.new

  result = prompt.collect do
    key(:site).ask('Site (ex: https://myjira.atlassian.net):', required: true)
    key(:context_path).ask('Jira path in your site (just press enter if you don\'t have):', default: '')
    key(:username).ask('Username:', required: true)
    key(:password).mask('Password:', required: true)

    if options['ssl-config']
      key(:use_ssl).yes?('Use SSL?')
      key(:ssl_verify_mode).select('Verify mode:') do |menu|
        menu.choice 'Verify peer', OpenSSL::SSL::VERIFY_PEER
        menu.choice 'Verify client once', OpenSSL::SSL::VERIFY_CLIENT_ONCE
        menu.choice 'Verify fail if no peer cert', OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
        menu.choice 'Verify none', OpenSSL::SSL::VERIFY_NONE
      end
    end

    if options['proxy-config']
      key(:proxy_address).ask("Proxy address: ", default: nil)
      key(:proxy_port).ask("Proxy port: ", default: nil)
    end
  end

  result[:auth_type] = :basic
  result[:use_ssl] ||= false if result[:site] =~ /http\:\/\//

  result
end

#expire_auth_optionsObject



21
22
23
# File 'lib/terjira/client/auth_option_builder.rb', line 21

def expire_auth_options
  Terjira::FileCache.clear_all
end