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) ⇒ Object



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

def self.(username)
  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)
  upload_ssh_public_key(ssh_key_filename, token) if SSH.generate_key(ssh_key_filename, username)
end

.logoutObject



26
27
28
29
30
31
32
# File 'lib/gleis/authentication.rb', line 26

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

.upload_ssh_public_key(filename, token) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gleis/authentication.rb', line 34

def self.upload_ssh_public_key(filename, token)
  ssh_public_key = SSH.load_public_key(filename)
  body = API.request('post', 'upload_ssh_pub_key', token, 'ssh_public_key' => ssh_public_key)
  if body['success'] == 1
    puts "Successfully uploaded your SSH public key #{filename}.pub"
    # Get run server name
    params = Params.get_cli_parameters(token)
    SSH.add_host_to_config(body['git_server'], params['run_server'], filename)
  else
    puts "Failed to upload SSH public key: #{body['message']}"
  end
end

.whoamiObject



20
21
22
23
24
# File 'lib/gleis/authentication.rb', line 20

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