lib/amy.rb
def authenticate_with_amy(username = nil, password = nil)

  # Authentication with AMY involves mimicking the UI form authentication (i.e. mimic what is happening in the UI), since basic authN is not supported.
  # We first need to retrieve csrf_token passed to the client by the server (so we do an initial GET AMY_LOGIN_URL), then POST the csrf_token back alongside username and password (and also pass the csrf_token in headers).
  # In return, we get the session_id and the same csrf_token. We use these two for all subsequent calls to private pages and pass them in headers.
  if username.nil? and password.nil?
    if File.exist?(AMY_LOGIN_CONF_FILE)
      amy_login = YAML.load_file(AMY_LOGIN_CONF_FILE)
      username = amy_login['amy_login']['username']
      password  = amy_login['amy_login']['password']
    else
      puts "Failed to load AMY login details from #{AMY_LOGIN_CONF_FILE}: file does not exist."
      return nil, nil
    end
  end

  puts "\n" + "#" * 80 +"\n\n"
  if username.nil? or password.nil?
    puts "Username or password are blank - cannot authenticate with AMY."
    return nil, nil
  else
    begin
      csrf_token = open(AMY_LOGIN_URL).meta['set-cookie'].scan(/csrftoken=([^;]+)/)[0][0]
      puts "Obtained csrf_token from AMY."

      amy_login_url = URI.parse(AMY_LOGIN_URL)
      headers = HEADERS.merge({
                                  "Referer" => AMY_LOGIN_URL,
                                  "Cookie" => "csrftoken=#{csrf_token}"
                              })

      http = Net::HTTP.new(amy_login_url.host, amy_login_url.port)
      http.use_ssl = true

      request = Net::HTTP::Post.new(amy_login_url.request_uri, headers)
      request.set_form_data("username" => username, "password" => password, "csrfmiddlewaretoken" => csrf_token)

      response = http.request(request)

      session_id = response['set-cookie'].scan(/sessionid=([^;]+)/)[0][0]
      puts "Obtained session_id from AMY."

      return session_id, csrf_token

    rescue Exception => ex
      puts "Failed to authenticate with AMY. An error of type #{ex.class} occurred, the reason being: #{ex.message}."
      puts "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
      return nil, nil
    end
  end
end
# Authenticate with AMY using username and password and Basic AuthN.
# Returns (session_id, csrf_token) that can be reused for the duration of session.
Authenticate with AMY using username and password and Basic AuthN.
Returns (session_id, csrf_token) that can be reused for the duration of session.
Authenticate with AMY using username and password and Basic AuthN . Returns ( session_id csrf_token ) that can be reused for the duration of session .
def authenticate_with_amy(username = nil, password = nil)
  if username.nil? and password.nil?
    if File.exist?(AMY_LOGIN_CONF_FILE)
      amy_login = YAML.load_file(AMY_LOGIN_CONF_FILE)
      username = amy_login['amy_login']['username']
      password  = amy_login['amy_login']['password']
    else
      puts "Failed to load AMY login details from #{AMY_LOGIN_CONF_FILE}: file does not exist."
      return nil, nil
    end
  end
  puts "\n" + "#" * 80 +"\n\n"
  if username.nil? or password.nil?
    puts "Username or password are blank - cannot authenticate with AMY."
    return nil, nil
  else
    begin
      csrf_token = open(AMY_LOGIN_URL).meta['set-cookie'].scan(/csrftoken=([^;]+)/)[0][0]
      puts "Obtained csrf_token from AMY."
      amy_login_url = URI.parse(AMY_LOGIN_URL)
      headers = HEADERS.merge({
                                  "Referer" => AMY_LOGIN_URL,
                                  "Cookie" => "csrftoken=#{csrf_token}"
                              })
      http = Net::HTTP.new(amy_login_url.host, amy_login_url.port)
      http.use_ssl = true
      request = Net::HTTP::Post.new(amy_login_url.request_uri, headers)
      request.set_form_data("username" => username, "password" => password, "csrfmiddlewaretoken" => csrf_token)
      response = http.request(request)
      session_id = response['set-cookie'].scan(/sessionid=([^;]+)/)[0][0]
      puts "Obtained session_id from AMY."
      return session_id, csrf_token
    rescue Exception => ex
      puts "Failed to authenticate with AMY. An error of type #{ex.class} occurred, the reason being: #{ex.message}."
      puts "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
      return nil, nil
    end
  end
end
def authenticate_with_amy ( username = nil , password = nil ) if username . nil? and password . nil? if File . exist? ( AMY_LOGIN_CONF_FILE ) amy_login = YAML . load_file ( AMY_LOGIN_CONF_FILE ) username = amy_login [ 'amy_login' ] [ 'username' ] password = amy_login [ 'amy_login' ] [ 'password' ] else puts "Failed to load AMY login details from #{AMY_LOGIN_CONF_FILE}: file does not exist." return nil , nil end end puts "\n" + "#" * 80 + "\n\n" if username . nil? or password . nil? puts "Username or password are blank - cannot authenticate with AMY." return nil , nil else begin csrf_token = open ( AMY_LOGIN_URL ) . meta [ 'set-cookie' ] . scan ( / csrftoken=([^;]+) / ) [ 0 ] [ 0 ] puts "Obtained csrf_token from AMY." amy_login_url = URI . parse ( AMY_LOGIN_URL ) headers = HEADERS . merge ( { "Referer" => AMY_LOGIN_URL , "Cookie" => "csrftoken=#{csrf_token}" } ) http = Net :: HTTP . new ( amy_login_url . host , amy_login_url . port ) http . use_ssl = true request = Net :: HTTP :: Post . new ( amy_login_url . request_uri , headers ) request . set_form_data ( "username" => username , "password" => password , "csrfmiddlewaretoken" => csrf_token ) response = http . request ( request ) session_id = response [ 'set-cookie' ] . scan ( / sessionid=([^;]+) / ) [ 0 ] [ 0 ] puts "Obtained session_id from AMY." return session_id , csrf_token rescue Exception => ex puts "Failed to authenticate with AMY. An error of type #{ex.class} occurred, the reason being: #{ex.message}." puts "Backtrace:\n\t#{ex.backtrace.join("\n\t")}" return nil , nil end end end
Authenticate with AMY using username and password and Basic AuthN.
Authenticate with AMY using username and password and Basic AuthN .
# Authentication with AMY involves mimicking the UI form authentication (i.e. mimic what is happening in the UI), since basic authN is not supported. # We first need to retrieve csrf_token passed to the client by the server (so we do an initial GET AMY_LOGIN_URL), then POST the csrf_token back alongside username and password (and also pass the csrf_token in headers). # In return, we get the session_id and the same csrf_token. We use these two for all subsequent calls to private pages and pass them in headers.
username
password