penn_extranet_adapter 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/penn_extranet_adapter.rb +118 -0
  2. metadata +59 -0
@@ -0,0 +1,118 @@
1
+ # Usage:
2
+ # Creation
3
+ # p = PennExtranetAdapter.new( user, pw )
4
+ #
5
+ # Getting a page
6
+ # source = p.get( url, params )
7
+ # source = p.post( url, params )
8
+
9
+ # Directly accessing the authenticated mechanize object
10
+ # o = p.authenticated_agent
11
+ #
12
+ class PennExtranetAdapter
13
+ @authenticated_agent = nil
14
+
15
+ def initialize(username, pw)
16
+ @authenticated_agent = nil
17
+ @username = username
18
+ @pw = pw
19
+ end
20
+
21
+ # goes to the home extranet page and tries to detect if this is a valid
22
+ # session or not. After a prolonged time period, a valid session may
23
+ # become invalid
24
+ def valid_agent?( agent = authenticated_agent )
25
+ agent.get("https://extranet.uphs.upenn.edu")
26
+ main_page_body = agent.page.body
27
+ if main_page_body.include? "Please sign in to begin your secure session"
28
+ puts "==CHECKING VALIDITY==...Invalid agent"
29
+ false
30
+ elsif main_page_body.include? "Welcome to the Secure Access SSL VPN"
31
+ puts "==CHECKING VALIDITY==...Valid agent!"
32
+ true
33
+ else
34
+ puts "==CHECKING VALIDITY==...Probably an invalid agent"
35
+ false
36
+ end
37
+ end
38
+
39
+ # returns the already-authenticated agent OR creates a new one
40
+ def authenticated_agent
41
+ #returns an agent that is authenticated or a new agent if you are on the VPN
42
+
43
+ # step 1: try to load a saved agent to avoid having to re-auth
44
+ if @authenticated_agent == nil and Rails.env.development? #
45
+ load_agent
46
+
47
+ # check to see if agent is valid, otherwise clear it
48
+ @authenticated_agent = nil unless valid_agent?( @authenticated_agent )
49
+ end
50
+
51
+ # Step 2: if loading failed then will authenticate a new session
52
+ @authenticated_agent ||= authenticate
53
+ end
54
+
55
+ def post( url, param_hash={} )
56
+ begin
57
+ res = authenticated_agent.post(url, param_hash)
58
+ res.body
59
+ rescue
60
+ raise "Failed to connect. Are you on VPN?"
61
+ nil
62
+ end
63
+ end
64
+
65
+ def get( url, param_hash={} )
66
+ begin
67
+ res = authenticated_agent.get(url, param_hash)
68
+ res.body
69
+ rescue
70
+ raise "Failed to connect. Are you on VPN?"
71
+ nil
72
+ end
73
+ end
74
+
75
+ # private
76
+ # returns the newly authenticated agent
77
+ def authenticate
78
+ puts "==AUTHENTICATING EXTRANET=="
79
+ agent = new_secure_agent
80
+ page = agent.get('https://extranet.uphs.upenn.edu') #connect to extranet
81
+ agent.page.forms.first.username = @username # login username for extranet
82
+ agent.page.forms.first.password = @pw # login pw
83
+ agent.page.forms.first.submit # submits login request
84
+
85
+ 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
86
+ agent.page.forms.first.checkbox.check
87
+ end
88
+ btn = agent.page.forms.first.submit_button?('btnContinue') #finds the continue button
89
+ agent.page.forms.first.submit(btn) # submits it to confirm login
90
+ # save_agent
91
+ return agent
92
+ end
93
+
94
+ def new_secure_agent
95
+ Mechanize.new{|a| a.ssl_version, a.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}
96
+ end
97
+
98
+ def save_agent
99
+ authenticated_agent.cookie_jar.save_as 'penn_extranet_cookie_jar', :session => true, :format => :yaml
100
+ end
101
+
102
+ # mainly for testing purposes, because in production it should not need to be called explicitly
103
+ def load_agent
104
+ @authenticated_agent = saved_agent
105
+ end
106
+
107
+ def saved_agent
108
+ if File.exists?("penn_extranet_cookie_jar")
109
+ puts "==Loaded saved agent=="
110
+ agent = new_secure_agent
111
+ agent.cookie_jar.load('penn_extranet_cookie_jar')
112
+ agent
113
+ else
114
+ puts "No saved agent that is valid"
115
+ nil
116
+ end
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: penn_extranet_adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Do
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: &70094755109720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.7.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70094755109720
25
+ description: Logs in to University of Pennsylvania's extranet given a valid username
26
+ and password
27
+ email:
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/penn_extranet_adapter.rb
33
+ homepage: http://rethinkhealth.blogspot.com/
34
+ licenses:
35
+ - MIT
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Logs in to University of Pennsylvania's extranet given a valid username and
58
+ password
59
+ test_files: []