cas_rest_client 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cas_rest_client/cas_rest_client.rb +66 -0
- data/lib/cas_rest_client.rb +5 -0
- metadata +84 -0
@@ -0,0 +1,66 @@
|
|
1
|
+
class CasRestClient
|
2
|
+
@cas_opts = nil
|
3
|
+
@tgt = nil
|
4
|
+
@cookies = nil
|
5
|
+
DEFAULT_OPTIONS = {:use_cookies => true}
|
6
|
+
|
7
|
+
def initialize(cas_opts)
|
8
|
+
@cas_opts = DEFAULT_OPTIONS.merge(cas_opts)
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(uri, options = {})
|
12
|
+
execute("get", uri, {}, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def post(uri, params = {}, options = {})
|
16
|
+
execute("post", uri, params, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def execute(method, uri, params, options)
|
21
|
+
if @cas_opts[:use_cookies]
|
22
|
+
begin
|
23
|
+
execute_with_cookie(method, uri, params, options)
|
24
|
+
rescue RestClient::Request::Unauthorized => e
|
25
|
+
execute_with_tgt(method, uri, params, options)
|
26
|
+
end
|
27
|
+
else
|
28
|
+
execute_with_tgt(method, uri, params, options)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def execute_with_cookie(method, uri, params, options)
|
33
|
+
return RestClient.send(method, uri, {:cookies => @cookies}.merge(options)) if params.empty?
|
34
|
+
RestClient.send(method, uri, params, {:cookies => @cookies}.merge(options))
|
35
|
+
end
|
36
|
+
|
37
|
+
def execute_with_tgt(method, uri, params, options)
|
38
|
+
get_tgt unless @tgt
|
39
|
+
|
40
|
+
ticket = nil
|
41
|
+
begin
|
42
|
+
ticket = create_ticket(@tgt, :service => @cas_opts[:service] || uri)
|
43
|
+
rescue RestClient::ResourceNotFound => e
|
44
|
+
get_tgt
|
45
|
+
ticket = create_ticket(@tgt, :service => @cas_opts[:service] || uri)
|
46
|
+
end
|
47
|
+
response = RestClient.send(method, "#{uri}#{uri.include?("?") ? "&" : "?"}ticket=#{ticket}", options) if params.empty?
|
48
|
+
response = RestClient.send(method, "#{uri}#{uri.include?("?") ? "&" : "?"}ticket=#{ticket}", params, options) unless params.empty?
|
49
|
+
@cookies = response.cookies
|
50
|
+
response
|
51
|
+
end
|
52
|
+
|
53
|
+
def create_ticket(uri, params)
|
54
|
+
ticket = RestClient.post(uri, params)
|
55
|
+
ticket = ticket.body if ticket.respond_to? 'body'
|
56
|
+
ticket
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_tgt
|
60
|
+
opts = @cas_opts.dup
|
61
|
+
opts.delete(:service)
|
62
|
+
opts.delete(:use_cookies)
|
63
|
+
@tgt = RestClient.post(opts.delete(:uri), opts).headers[:location]
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cas_rest_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Antonio Marques, Roberto Klein
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-28 00:00:00 -03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rest-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 4
|
32
|
+
- 2
|
33
|
+
version: 1.4.2
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description:
|
37
|
+
email:
|
38
|
+
- acmarques@gmail.com
|
39
|
+
- robertokl@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- lib/cas_rest_client/cas_rest_client.rb
|
48
|
+
- lib/cas_rest_client.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/robertokl/cas-rest-client
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.7
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Rest client for services using CAS authentication.
|
83
|
+
test_files: []
|
84
|
+
|