casual 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/casual.gemspec +4 -4
  3. data/lib/casual.rb +41 -12
  4. data/test/test_casual.rb +11 -10
  5. metadata +11 -4
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{casual}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Zach Holman"]
12
- s.date = %q{2010-06-10}
12
+ s.date = %q{2010-08-06}
13
13
  s.description = %q{A tiny CAS client for Ruby}
14
14
  s.email = %q{github.com@zachholman.com}
15
15
  s.extra_rdoc_files = [
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.homepage = %q{http://github.com/holman/casual}
32
32
  s.rdoc_options = ["--charset=UTF-8"]
33
33
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.6}
34
+ s.rubygems_version = %q{1.3.7}
35
35
  s.summary = %q{A tiny CAS client for Ruby}
36
36
  s.test_files = [
37
37
  "test/helper.rb",
@@ -42,7 +42,7 @@ Gem::Specification.new do |s|
42
42
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
43
  s.specification_version = 3
44
44
 
45
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
46
  s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
47
47
  else
48
48
  s.add_dependency(%q<nokogiri>, [">= 0"])
@@ -3,12 +3,14 @@ require 'hpricot'
3
3
 
4
4
  module Casual
5
5
  class Client
6
- attr_accessor :server_path, :callback_url, :port
6
+ attr_accessor :hostname, :path, :https, :port, :callback_url, :jasig
7
7
 
8
8
  def initialize(config)
9
- @server_path = config[:server_path]
9
+ @hostname = config[:hostname]
10
+ @path = config[:path]
11
+ @https = config[:https]
12
+ @port = config[:port] || 443
10
13
  @callback_url = config[:callback_url]
11
- @port = config[:port] || 443
12
14
  end
13
15
 
14
16
  def authorization_url
@@ -18,32 +20,59 @@ module Casual
18
20
  def authenticate(username,password)
19
21
  ticket = acquire_ticket
20
22
  params = "username=#{username}&password=#{password}&lt=#{ticket}"
21
- status,response = connection.post('/login', params)#.last
22
- (status.code == '200') ? username : nil
23
+ params << '&_eventId=submit&submit=LOGIN' if jasig
24
+ status,response =
25
+ connection.post("/#{no_slash_path}/login#{jasig}", params)
26
+
27
+ if jasig
28
+ (response =~ /Log In Successful/) ? username : nil
29
+ else
30
+ status.code == '200' ? username : nil
31
+ end
23
32
  end
24
33
 
25
34
  def acquire_ticket
26
- login_page = connection.get('/login')
27
- Hpricot(login_page.body).search('input[@id=lt]').first['value']
35
+ login_page = connection.get("/#{no_slash_path}/login")
36
+ ticket = Hpricot(login_page.body).search('input[@name=lt]').first
37
+ jasig_eats_babies(login_page.body)
38
+ ticket ? ticket['value'] : nil
39
+ end
40
+
41
+ def jasig_eats_babies(body)
42
+ @jasig = ';'
43
+ @jasig << Hpricot(body).search('form[@id=fm1]').
44
+ first['action'].split(';').last
28
45
  end
29
46
 
30
47
  def connection
31
- http = Net::HTTP.new(server_path, port)
32
- http.use_ssl = true
48
+ http = Net::HTTP.new(hostname, port)
49
+ http.use_ssl = @https
33
50
  http
34
51
  end
35
52
 
36
53
  def authenticate_ticket(ticket)
37
- connection.get("/serviceValidate?service=#{callback_url}&ticket=#{ticket}").body
54
+ connection.
55
+ get("/#{no_slash_path}/serviceValidate?service=#{callback_url}" +
56
+ "&ticket=#{ticket}").
57
+ body
38
58
  end
39
59
 
40
60
  def user_login(ticket)
41
- user = Hpricot::XML(authenticate_ticket(ticket)).search('//cas:authenticationSuccess //cas:user').text
61
+ user = Hpricot::XML(authenticate_ticket(ticket)).
62
+ search('//cas:authenticationSuccess //cas:user').text
42
63
  user.strip != '' ? user : nil
43
64
  end
44
65
 
45
66
  def server_url
46
- "https://#{server_path}:#{port}"
67
+ "#{protocol}://#{hostname}:#{port}/#{no_slash_path}"
68
+ end
69
+
70
+ def protocol
71
+ https ? 'http' : 'https'
72
+ end
73
+
74
+ def no_slash_path
75
+ path[0] == 47 ? path[1..path.size] : path
47
76
  end
48
77
 
49
78
  end
@@ -1,18 +1,19 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestCasual < Test::Unit::TestCase
4
-
4
+
5
5
  def setup
6
- @client = Casual::Client.new(:server_path => 'localhost',
7
- :port => 443,
6
+ @client = Casual::Client.new(:hostname => 'localhost',
7
+ :path => '/cas',
8
+ :port => '8080',
8
9
  :callback_url => 'http://localhost')
9
10
  end
10
-
11
+
11
12
  def test_authorization_url
12
- assert_equal @client.authorization_url,
13
- "https://localhost:443/login?service=http://localhost"
13
+ assert_equal @client.authorization_url,
14
+ "https://localhost:8080/cas/login?service=http://localhost"
14
15
  end
15
-
16
+
16
17
  def test_user_login_success
17
18
  mock(@client).authenticate_ticket.with('GOLDEN_TICKET_LOL') {
18
19
  '<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">'+
@@ -21,7 +22,7 @@ class TestCasual < Test::Unit::TestCase
21
22
  }
22
23
  assert_equal @client.user_login('GOLDEN_TICKET_LOL'), 'holman'
23
24
  end
24
-
25
+
25
26
  def test_user_login_fail
26
27
  mock(@client).authenticate_ticket.with('GOLDEN_TICKET_FAIL') {
27
28
  '<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">'+
@@ -30,9 +31,9 @@ class TestCasual < Test::Unit::TestCase
30
31
  }
31
32
  assert_equal @client.user_login('GOLDEN_TICKET_FAIL'), nil
32
33
  end
33
-
34
+
34
35
  def test_authenticate_success
35
- connection_mock = mock('').post.with_any_args {
36
+ connection_mock = mock('').post.with_any_args {
36
37
  [Net::HTTPSuccess.new('','200',''), '<div class="messagebox confirmation">You have successfully logged in.</div>']
37
38
  }
38
39
  mock(@client).connection { connection_mock }
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casual
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 19
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
- - 2
8
+ - 3
8
9
  - 0
9
- version: 0.2.0
10
+ version: 0.3.0
10
11
  platform: ruby
11
12
  authors:
12
13
  - Zach Holman
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-06-10 00:00:00 -04:00
18
+ date: 2010-08-06 00:00:00 -07:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: nokogiri
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -59,23 +62,27 @@ rdoc_options:
59
62
  require_paths:
60
63
  - lib
61
64
  required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
62
66
  requirements:
63
67
  - - ">="
64
68
  - !ruby/object:Gem::Version
69
+ hash: 3
65
70
  segments:
66
71
  - 0
67
72
  version: "0"
68
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
69
75
  requirements:
70
76
  - - ">="
71
77
  - !ruby/object:Gem::Version
78
+ hash: 3
72
79
  segments:
73
80
  - 0
74
81
  version: "0"
75
82
  requirements: []
76
83
 
77
84
  rubyforge_project:
78
- rubygems_version: 1.3.6
85
+ rubygems_version: 1.3.7
79
86
  signing_key:
80
87
  specification_version: 3
81
88
  summary: A tiny CAS client for Ruby