Class: Gleis::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/gleis/authentication.rb

Overview

This class implements all authentication related commands of the CLI

Class Method Summary collapse

Class Method Details

.login(username, password = nil, skip_keygen = false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gleis/authentication.rb', line 4

def self.(username, password = nil, skip_keygen = false)
  puts 'Login to Gleis'
  username = username.downcase
  password ||= Utils.prompt_password

  body = API.request('post', 'login', nil, 'username': username, 'password': password)
  token = body['token']

  Token.save(token)
  puts 'Authentication successful'
  puts "\nNEWS: #{body['data']}\n\n" unless body['data'].nil?
  # Generate SSH key pair if not found and upload pub key
  ssh_key_filename = Config::SSH_KEY_FILE_BASE + '_' + Utils.convert_username_to_filename(username)
  AuthKey.add("#{ssh_key_filename}.pub", nil, true) \
    if skip_keygen == false && SSH.generate_key(ssh_key_filename, username)
  puts "\nINFO: New Gleis CLI version available, please update by running \"gem update gleis\"\n\n" \
    if Utils.update_available?(Params.get_cli_parameters(token)['version'])
end

.logoutObject



58
59
60
61
62
63
64
# File 'lib/gleis/authentication.rb', line 58

def self.logout
  token = Token.check
  puts 'Logout of Gleis'
  API.request('post', 'logout', token, {})
  Token.delete
  puts 'Logout successful'
end

.register(email) ⇒ Object



23
24
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
# File 'lib/gleis/authentication.rb', line 23

def self.register(email)
  abort('Invalid e-mail address.') unless email.match(URI::MailTo::EMAIL_REGEXP)
  puts "In order to register for a free Gleis account please enter your details below.\n\n"
  print "Email: #{email}\n"
  print 'First name: '
  firstname = $stdin.gets.chomp
  print 'Last name: '
  lastname = $stdin.gets.chomp
  print 'Organisation or company name (optional): '
  organisation = $stdin.gets.chomp
  puts "\n"
  abort('Please provide your firstname and lastname.') if firstname.empty? || lastname.empty?
  return unless Utils.prompt_yes_no('Are your details above correct?')

  req_body = { 'email': email,
               'firstname': firstname,
               'lastname': lastname,
               'organisation': organisation }
  body = API.request('post', 'register', nil, req_body)
  if body['success'] == 1
    puts 'Successfully sent registration. We will manually review your request and send you a confirmation ' \
      "e-mail within the next 24 hours.\n"
    puts 'In the meantime please save and keep secure your Gleis password shown below in your password manager:'
    puts "\n\n\t\t#{body['data']}\n\n"
  else
    puts "Failed to register: #{body['message']}"
  end
end

.whoamiObject



52
53
54
55
56
# File 'lib/gleis/authentication.rb', line 52

def self.whoami
  token = Token.check
  body = API.request('get', 'whoami', token)
  puts "You are currently logged in as #{body['data']}"
end