Class: AtreaControl::Duplex::Login
- Inherits:
-
Object
- Object
- AtreaControl::Duplex::Login
- 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
-
.user_tokens(login:, password:) ⇒ Hash
-
user_id, unit_id, sid.
-
Instance Method Summary collapse
-
#close ⇒ Object
quit selenium browser.
- #driver ⇒ Selenium::WebDriver::Firefox::Driver
-
#initialize(login:, password:) ⇒ Login
constructor
A new instance of Login.
-
#login ⇒ Object
Login into control.
-
#open_dashboard ⇒ Object
Retrieve dashboard URI from object tag and open it again.
-
#submit_login_form ⇒ Object
Submit given credentials and proceed login.
-
#user ⇒ Object
def crypto_password md5 = Digest::MD5.new md5 << “rn” md5 << @password md5.hexdigest end.
Methods included from Logger
Constructor Details
#initialize(login:, password:) ⇒ Login
Returns a new instance of Login.
25 26 27 28 |
# File 'lib/atrea_control/duplex/login.rb', line 25 def initialize(login:, password:) @login = login @password = password end |
Class Method Details
.user_tokens(login:, password:) ⇒ Hash
Returns - 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: login, password: password) tokens = i.user tokens ensure i.close end |
Instance Method Details
#close ⇒ Object
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 |
#driver ⇒ 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] = Selenium::WebDriver::Firefox::Options.new .add_argument "-headless" unless ENV["NO_HEADLESS"] @driver ||= Selenium::WebDriver::Firefox::Driver.new options: end |
#login ⇒ Object
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 login return driver if @logged @login_in_progress = true logger.debug "start new login..." driver.get "#{AtreaControl::Duplex::CONTROL_URI}?action=logout" submit_login_form finish_login driver ensure @login_in_progress = false end |
#open_dashboard ⇒ Object
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_form ⇒ Object
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 submit_login_form 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 |
#user ⇒ Object
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 login 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 |