Class: Morpheus::AuthInterface

Inherits:
APIClient show all
Defined in:
lib/morpheus/api/auth_interface.rb

Instance Method Summary collapse

Methods inherited from APIClient

#account_groups, #accounts, #apps, #archive_buckets, #archive_files, #auth, #blueprints, #cloud_datastores, #cloud_folders, #cloud_policies, #cloud_resource_pools, #clouds, #clusters, #containers, #custom_instance_types, #cypher, #dashboard, #default_content_type, #deploy, #deployments, #dry, #dry_run, #environments, #execute, #execute_schedules, #execution_request, #file_copy_request, #group_policies, #groups, #image_builder, #inspect, #instance_types, #instances, #key_pairs, #library_compute_type_layouts, #library_container_scripts, #library_container_templates, #library_container_types, #library_container_upgrades, #library_instance_types, #library_layouts, #license, #load_balancers, #logged_in?, #logs, #monitoring, #network_domain_records, #network_domains, #network_groups, #network_pool_ips, #network_pool_servers, #network_pools, #network_proxies, #network_services, #network_subnet_types, #network_subnets, #network_types, #networks, #old_cypher, #option_type_lists, #option_types, #options, #packages, #policies, #power_schedules, #processes, #provision_types, #refresh_token, #reports, #roles, #security_group_rules, #security_groups, #server_types, #servers, #service_plans, #set_ssl_verification_enabled, #setopts, #setup, #ssl_verification_enabled?, #storage_providers, #task_sets, #tasks, #to_s, #url, #user_groups, #user_settings, #user_sources, #users, #virtual_images, #whoami, #wiki, #withopts

Constructor Details

#initialize(base_url, access_token = nil) ⇒ AuthInterface

attr_reader :access_token, :refresh_token, :expires_at



7
8
9
10
# File 'lib/morpheus/api/auth_interface.rb', line 7

def initialize(base_url, access_token=nil)
  @base_url = base_url
  @access_token = access_token
end

Instance Method Details

#login(username, password) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/morpheus/api/auth_interface.rb', line 12

def (username, password)
  @access_token, @refresh_token, @expires_at = nil, nil, nil
  url = "#{@base_url}/oauth/token"
  params = {grant_type: 'password', scope:'write', client_id: 'morph-cli', username: username}
  payload = {password: password}
  opts = {method: :post, url: url, headers:{ params: params}, payload: payload, timeout: 5}
  response = execute(opts)
  return response if @dry_run
  @access_token = response['access_token']
  @refresh_token = response['refresh_token']
  if response['expires_in'] != nil
    @expires_at = Time.now + response['expires_in']
  end
  return response
end

#logoutObject



45
46
47
48
49
50
51
# File 'lib/morpheus/api/auth_interface.rb', line 45

def logout()
  # super.logout()
  if @access_token
    # todo: expire the token
  end
  raise "#{self}.logout() is not yet implemented"
end

#use_refresh_token(refresh_token) ⇒ Object

this regenerates the access_token and refresh_token



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/morpheus/api/auth_interface.rb', line 29

def use_refresh_token(refresh_token)
  @access_token = nil
  url = "#{@base_url}/oauth/token"
  params = {grant_type: 'refresh_token', scope:'write', client_id: 'morph-cli'}
  payload = {refresh_token: refresh_token}
  opts = {method: :post, url: url, headers:{ params: params}, payload: payload}
  response = execute(opts)
  return response if @dry_run
  @access_token = response['access_token']
  @refresh_token = response['refresh_token']
  if response['expires_in'] != nil
    @expires_at = Time.now + response['expires_in']
  end
  return response
end