class PennExtranetAdapter
Directly accessing the authenticated mechanize object
o = p.authenticated_agent
Public Class Methods
new(username, pw)
click to toggle source
# File lib/penn_extranet_adapter.rb, line 15 def initialize(username, pw) @authenticated_agent = nil @username = username @pw = pw end
Public Instance Methods
authenticate()
click to toggle source
private returns the newly authenticated agent
# File lib/penn_extranet_adapter.rb, line 77 def authenticate puts "==AUTHENTICATING EXTRANET==" agent = new_secure_agent page = agent.get('https://extranet.uphs.upenn.edu') #connect to extranet agent.page.forms.first.username = @username # login username for extranet agent.page.forms.first.password = @pw # login pw agent.page.forms.first.submit # submits login request if agent.page.forms.first.checkbox_with(:name =>'postfixSID') #if another extranet session is open, it will ask you to close it or continue. If tow are open, you have to close one. This line looks for checkboxes and closes one session if they are present agent.page.forms.first.checkbox.check end btn = agent.page.forms.first.submit_button?('btnContinue') #finds the continue button agent.page.forms.first.submit(btn) # submits it to confirm login save_agent if Rails.env.development? return agent end
authenticated_agent()
click to toggle source
returns the already-authenticated agent OR creates a new one
# File lib/penn_extranet_adapter.rb, line 40 def authenticated_agent #returns an agent that is authenticated or a new agent if you are on the VPN # step 1: try to load a saved agent to avoid having to re-auth if @authenticated_agent == nil and Rails.env.development? # load_agent # check to see if agent is valid, otherwise clear it @authenticated_agent = nil unless valid_agent?( @authenticated_agent ) end # Step 2: if loading failed then will authenticate a new session @authenticated_agent ||= authenticate end
get( url, param_hash={} )
click to toggle source
# File lib/penn_extranet_adapter.rb, line 65 def get( url, param_hash={} ) begin res = authenticated_agent.get(url, param_hash) res.body rescue raise "Failed to connect. Are you on VPN?" nil end end
load_agent()
click to toggle source
mainly for testing purposes, because in production it should not need to be called explicitly
# File lib/penn_extranet_adapter.rb, line 103 def load_agent @authenticated_agent = saved_agent end
new_secure_agent()
click to toggle source
# File lib/penn_extranet_adapter.rb, line 94 def new_secure_agent Mechanize.new{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE} end
post( url, param_hash={} )
click to toggle source
# File lib/penn_extranet_adapter.rb, line 55 def post( url, param_hash={} ) begin res = authenticated_agent.post(url, param_hash) res.body rescue raise "Failed to connect. Are you on VPN?" nil end end
save_agent()
click to toggle source
# File lib/penn_extranet_adapter.rb, line 98 def save_agent authenticated_agent.cookie_jar.save_as 'penn_extranet_cookie_jar', :session => true, :format => :yaml end
saved_agent()
click to toggle source
# File lib/penn_extranet_adapter.rb, line 107 def saved_agent if File.exists?("penn_extranet_cookie_jar") puts "==Loaded saved agent==" agent = new_secure_agent agent.cookie_jar.load('penn_extranet_cookie_jar') agent else puts "No saved agent that is valid" nil end end
valid_agent?( agent = authenticated_agent )
click to toggle source
goes to the home extranet page and tries to detect if this is a valid session or not. After a prolonged time period, a valid session may become invalid
# File lib/penn_extranet_adapter.rb, line 24 def valid_agent?( agent = authenticated_agent ) agent.get("https://extranet.uphs.upenn.edu") main_page_body = agent.page.body if main_page_body.include? "Please sign in to begin your secure session" puts "==CHECKING VALIDITY==...Invalid agent" false elsif main_page_body.include? "Welcome to the Secure Access SSL VPN" puts "==CHECKING VALIDITY==...Valid agent!" true else puts "==CHECKING VALIDITY==...Probably an invalid agent" false end end