Class: AtreaControl::Duplex::Login

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/atrea_control/duplex/login.rb

Overview

Process login into RD5 with selenium to get ‘sid` ( auth_token ) for direct API communication

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(login:, password:) ⇒ Login

Returns a new instance of Login.

Parameters:

  • login (String)
  • password (String)


25
26
27
28
# File 'lib/atrea_control/duplex/login.rb', line 25

def initialize(login:, password:)
  @login = 
  @password = password
end

Class Method Details

.user_tokens(login:, password:) ⇒ Hash

Returns - user_id, unit_id, sid.

Returns:

  • (Hash)
    • user_id, unit_id, sid



14
15
16
17
18
19
20
21
# File 'lib/atrea_control/duplex/login.rb', line 14

def self.user_tokens(login:, password:)
  i = new(login: , password: password)
  tokens = i.user

  tokens
ensure
  i.close
end

Instance Method Details

#closeObject

quit selenium browser



101
102
103
104
105
106
107
108
109
110
# File 'lib/atrea_control/duplex/login.rb', line 101

def close
  begin
    driver.quit
  rescue StandardError
    nil
  end
  logger.debug "driver closed & destroyed"
ensure
  remove_instance_variable :@driver
end

#driverSelenium::WebDriver::Firefox::Driver

Returns:

  • (Selenium::WebDriver::Firefox::Driver)


53
54
55
56
57
58
59
60
61
62
# File 'lib/atrea_control/duplex/login.rb', line 53

def driver
  return @driver if defined?(@driver)

  # options = Selenium::WebDriver::Firefox::Options.new
  # options.headless! unless ENV["NO_HEADLESS"]
  # @driver ||= Selenium::WebDriver.for :firefox, capabilities: [options]
  options = Selenium::WebDriver::Firefox::Options.new
  options.add_argument "-headless" unless ENV["NO_HEADLESS"]
  @driver ||= Selenium::WebDriver::Firefox::Driver.new options: options
end

#loginObject

Login into control



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/atrea_control/duplex/login.rb', line 65

def 
  return driver if @logged

  @login_in_progress = true
  logger.debug "start new login..."
  driver.get "#{AtreaControl::Duplex::CONTROL_URI}?action=logout"
  
  
  driver
ensure
  @login_in_progress = false
end

#open_dashboardObject

Retrieve dashboard URI from object tag and open it again



92
93
94
95
96
97
98
# File 'lib/atrea_control/duplex/login.rb', line 92

def open_dashboard
  uri = driver.find_element(tag_name: "object").attribute "data"
  # Open "iframe" with atrea dashboard - it propagate window objects...
  driver.get uri
  logger.debug "login success"
  @logged = true
end

#submit_login_formObject

Submit given credentials and proceed login



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/atrea_control/duplex/login.rb', line 79

def 
  form = driver.find_element(id: "loginFrm")
  username = form.find_element(name: "username")
  username.send_keys @login
  password = form.find_element(name: "password")
  password.send_keys @password
  logger.debug "Submit login form..."

  submit = form.find_element(css: "input[type=submit]")
  submit.click
end

#userObject

def crypto_password

md5 = Digest::MD5.new
md5 << "\r\n"
md5 << @password
md5.hexdigest

end

def token

RestClient.get "#{AtreaControl::Duplex::CONTROL_URI}/config/login.cgi", params: { magic: crypto_password }

end



41
42
43
44
45
46
47
48
49
50
# File 'lib/atrea_control/duplex/login.rb', line 41

def user
  raise AtreaControl::Error, "Must be logged in" unless 

  logger.debug "refresh user data based on session"
  @user_id = driver.execute_script("return window._user")
  @unit_id = driver.execute_script("return window._unit")
  @auth_token = driver.execute_script("return window.user")&.[]("auth") # sid

  { user_id: @user_id, unit_id: @unit_id, sid: @auth_token }
end