cassiopeia 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -7,6 +7,7 @@
7
7
  - Yet another custom CAS client/server implementation. This plugin allows you to perform single-server authorization between two different rails applications.
8
8
 
9
9
  = Changelog:
10
+ - 0.0.8: Full request saving support via rack middleware.
10
11
  - 0.0.7: Some code for redirection added. Added webpath prefix support.
11
12
  - 0.0.6: Tiny refactoring.
12
13
  - 0.0.5: Some important fixes for configuration exceptions handling.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*' ]
10
10
 
11
11
  spec = Gem::Specification.new do |s|
12
12
  s.name = "cassiopeia"
13
- s.version = "0.0.7"
13
+ s.version = "0.0.8"
14
14
  s.author = "smecsia"
15
15
  s.email = "smecsia@gmail.com"
16
16
  #s.homepage = ""
@@ -35,16 +35,16 @@ module Cassiopeia
35
35
  end
36
36
  def cas_request_ticket_id
37
37
  cas_store_location
38
- redirect_to Cassiopeia::Client::instance.cas_check_url(session)
38
+ redirect_to Cassiopeia::Client::instance.cas_check_url(session, params)
39
39
  end
40
40
  def cas_request_current_user
41
41
  session[CAS_TICKET_ID_KEY] = cas_current_ticket_id
42
- @ticket = Cassiopeia::Client::instance.cas_current_ticket(session)
42
+ @ticket = Cassiopeia::Client::instance.cas_current_ticket(session, params)
43
+ raise Cassiopeia::Exception::AccessDenied.new "Cannot identify current user" unless (@ticket.include? :user)
43
44
  @current_user = Cassiopeia::User.new(@ticket[:user])
44
45
  logger.debug "\nCurrent user identified (#{@current_user.login}), storing to session\n" + "="*50
45
46
  cas_store_current_user(@ticket, @current_user)
46
- logger.debug "\nShould redirect user to #{session[:return_to]}\n" + "="*50
47
- cas_redirect_back_or_default root_path
47
+ logger.debug "\nTicket_id is in request, should process the old request... #{session[:return_to]}\n" + "="*50
48
48
  end
49
49
  def cas_required_roles
50
50
  self.class.cas_required_roles if self.class.respond_to? :cas_required_roles
@@ -94,7 +94,7 @@ module Cassiopeia
94
94
  end
95
95
 
96
96
  def cas_proceed_auth
97
- service_url = Cassiopeia::Server::instance.service_url(session)
97
+ service_url = Cassiopeia::Server::instance.service_url(session, params)
98
98
  if cas_current_ticket_valid? && current_user
99
99
  logger.debug "\nCurrentTicketValid, current_user exists redirecting to service...\n" + "="*50
100
100
  return cas_redirect_to service_url
@@ -8,17 +8,24 @@ require 'rexml/document'
8
8
  ##################
9
9
  module Cassiopeia
10
10
  class Client < Base
11
+ SERVICE_URL = Cassiopeia::CONFIG[:service_url]
12
+ SERVICE_ID = Cassiopeia::CONFIG[:service_id]
13
+ SERVICE_URL_KEY = Cassiopeia::CONFIG[:service_url_key]
14
+ SERVICE_ID_KEY = Cassiopeia::CONFIG[:service_id_key]
15
+ TICKET_ID_KEY = Cassiopeia::CONFIG[:ticket_id_key]
16
+ REQ_KEY = Cassiopeia::CONFIG[:rack_unique_req_key]
11
17
  private
12
18
  @instance = nil
13
19
  def server_url
14
20
  Cassiopeia::CONFIG[:server_url] + "/" + Cassiopeia::CONFIG[:server_controller] + "." + Cassiopeia::CONFIG[:format]
15
21
  end
16
22
 
17
- def cas_data(session)
23
+ def cas_data(session, params)
18
24
  {
19
- Cassiopeia::CONFIG[:service_url_key] => Cassiopeia::CONFIG[:service_url],
20
- Cassiopeia::CONFIG[:service_id_key] => Cassiopeia::CONFIG[:service_id],
21
- Cassiopeia::CONFIG[:ticket_id_key] => session[Cassiopeia::CONFIG[:ticket_id_key]]
25
+ SERVICE_URL_KEY => SERVICE_URL,
26
+ SERVICE_ID_KEY => SERVICE_ID,
27
+ TICKET_ID_KEY => session[TICKET_ID_KEY],
28
+ REQ_KEY => params[REQ_KEY]
22
29
  }
23
30
  end
24
31
 
@@ -38,8 +45,8 @@ module Cassiopeia
38
45
  @instance = Cassiopeia::Client.new
39
46
  end
40
47
 
41
- def cas_current_ticket(session)
42
- res = do_post(server_url, cas_data(session))
48
+ def cas_current_ticket(session, request)
49
+ res = do_post(server_url, cas_data(session, request))
43
50
  case res
44
51
  when Net::HTTPSuccess
45
52
  begin
@@ -50,8 +57,8 @@ module Cassiopeia
50
57
  return {}
51
58
  end
52
59
 
53
- def cas_check_url(session)
54
- server_url + "?" + hash_to_query(cas_data(session))
60
+ def cas_check_url(session, params)
61
+ server_url + "?" + hash_to_query(cas_data(session, params))
55
62
  end
56
63
 
57
64
  end
@@ -9,13 +9,19 @@ module Cassiopeia
9
9
  :ticket_id_key => "cas_ticket_id",
10
10
  :service_id_key => "cas_service_id",
11
11
  :service_url_key => "cas_service_url",
12
- :server_url => "https://localhost/cassiopeia",
13
- :service_url => "https://localhost/",
12
+ :server_url => nil,
13
+ :service_url => nil,
14
14
  :webpath_prefix => "",
15
15
  :return_to_key => "cas_return_to",
16
- :service_id => "casclient",
16
+ :service_id => nil,
17
17
  :current_user_key => "current_user",
18
- :format => "js"
18
+ :format => "js",
19
+ :rack_session_store => "cas_rack_session",
20
+ :rack_session_key => "rack.session",
21
+ :rack_request_uri_key => "REQUEST_URI",
22
+ :rack_query_string_key => "QUERY_STRING",
23
+ :rack_save_keys => "REQUEST_METHOD QUERY_STRING REQUEST_URI",
24
+ :rack_unique_req_key => "cas_req_key"
19
25
  }
20
26
  CONFIG_PATH = "#{RAILS_ROOT}/config/cassiopeia.yml"
21
27
  @@conf = {}
@@ -0,0 +1,90 @@
1
+ module Cassiopeia
2
+ class RackRestoreRequest
3
+ CAS_RACK_SESSION_STORE = Cassiopeia::CONFIG[:rack_session_store]
4
+ CAS_RACK_SESSION_KEY = Cassiopeia::CONFIG[:rack_session_key]
5
+ CAS_TICKET_ID_KEY = Cassiopeia::CONFIG[:ticket_id_key]
6
+ CAS_REQUEST_URI_KEY = Cassiopeia::CONFIG[:rack_request_uri_key]
7
+ CAS_QUERY_STRING_KEY = Cassiopeia::CONFIG[:rack_query_string_key]
8
+ CAS_SAVE_KEYS = Cassiopeia::CONFIG[:rack_save_keys]
9
+ CAS_UNIQUE_REQ_KEY = Cassiopeia::CONFIG[:rack_unique_req_key]
10
+
11
+ def initialize( app )
12
+ @app = app
13
+ end
14
+
15
+ def call( env )
16
+ if restore_headers_required?(env)
17
+ env = restore_headers(env)
18
+ else
19
+ save_headers(env)
20
+ end
21
+ @status, @headers, @body = @app.call env
22
+ [@status, @headers, @body]
23
+ end
24
+
25
+ def query_to_hash(query)
26
+ CGI.parse(query)
27
+ end
28
+
29
+ def hash_to_query(hash)
30
+ pairs = []
31
+ hash.each do |k, vals|
32
+ vals = [vals] unless vals.kind_of? Array
33
+ vals.each {|v| pairs << "#{CGI.escape(k.to_s)}=#{(v)?CGI.escape(v.to_s):''}"}
34
+ end
35
+ pairs.join("&")
36
+ end
37
+
38
+ def restore_headers_required?(env)
39
+ env[CAS_QUERY_STRING_KEY] && env[CAS_QUERY_STRING_KEY].match(CAS_TICKET_ID_KEY.to_s) && env[CAS_RACK_SESSION_KEY] && env[CAS_RACK_SESSION_KEY][CAS_RACK_SESSION_STORE]
40
+ end
41
+
42
+ def save_headers(env)
43
+ if(env[CAS_RACK_SESSION_KEY])
44
+ req_key = store_req_key(env)
45
+ env[CAS_RACK_SESSION_KEY][CAS_RACK_SESSION_STORE] = { req_key => {}}
46
+ env.each do |key,value|
47
+ if env[key] && (key.is_a? String) && (key.match("HTTP_") || CAS_SAVE_KEYS.match(key))
48
+ env[CAS_RACK_SESSION_KEY][CAS_RACK_SESSION_STORE][req_key][key] = value
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ def add_ticket_id_to_req(env, key, value)
55
+ newparams = query_to_hash(value)
56
+ params = query_to_hash(env[key])
57
+ newparams[CAS_TICKET_ID_KEY] = params[CAS_TICKET_ID_KEY]
58
+ newparams.delete CAS_UNIQUE_REQ_KEY
59
+ env[key] = hash_to_query(newparams)
60
+ end
61
+
62
+ def restore_req_key(env)
63
+ newparams = query_to_hash(env[CAS_QUERY_STRING_KEY])
64
+ newparams[CAS_UNIQUE_REQ_KEY]
65
+ end
66
+
67
+ def store_req_key(env)
68
+ params = query_to_hash(env[CAS_QUERY_STRING_KEY])
69
+ params[CAS_UNIQUE_REQ_KEY] = UUIDTools::UUID.timestamp_create.to_s
70
+ env[CAS_QUERY_STRING_KEY] = hash_to_query(params)
71
+ params[CAS_UNIQUE_REQ_KEY]
72
+ end
73
+
74
+ def restore_headers(env)
75
+ current_req_key = restore_req_key(env)
76
+ stored_keys = env[CAS_RACK_SESSION_KEY][CAS_RACK_SESSION_STORE][current_req_key.to_s]
77
+ if(env[CAS_RACK_SESSION_KEY] && stored_keys)
78
+ stored_keys.each do |key,value|
79
+ if(key.match(CAS_QUERY_STRING_KEY))
80
+ add_ticket_id_to_req(env,key,value)
81
+ else
82
+ env[key] = value
83
+ end
84
+ end
85
+ env[CAS_RACK_SESSION_KEY][CAS_RACK_SESSION_STORE].delete current_req_key.to_s
86
+ end
87
+ env
88
+ end
89
+ end
90
+ end
@@ -2,11 +2,13 @@ module Cassiopeia
2
2
  class Server < Base
3
3
  SERVICE_KEY = Cassiopeia::CONFIG[:service_url_key]
4
4
  TICKET_KEY = Cassiopeia::CONFIG[:ticket_id_key]
5
+ REQ_KEY = Cassiopeia::CONFIG[:rack_unique_req_key]
5
6
  private
6
7
  @instance = nil
7
- def cas_data(session)
8
+ def cas_data(session, params)
8
9
  {
9
- TICKET_KEY => session[TICKET_KEY]
10
+ TICKET_KEY => session[TICKET_KEY],
11
+ REQ_KEY => params[REQ_KEY]
10
12
  }
11
13
  end
12
14
  public
@@ -15,9 +17,9 @@ module Cassiopeia
15
17
  @instance = Cassiopeia::Server.new
16
18
  end
17
19
 
18
- def service_url(session)
20
+ def service_url(session, params)
19
21
  if session && session[SERVICE_KEY] && session[TICKET_KEY]
20
- session[SERVICE_KEY] + "?" + hash_to_query(cas_data(session))
22
+ session[SERVICE_KEY] + "?" + hash_to_query(cas_data(session, params))
21
23
  end
22
24
  end
23
25
 
data/lib/cassiopeia.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
  module Cassiopeia
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.8'
5
5
  autoload :User, 'cassiopeia/user'
6
6
  autoload :Base, 'cassiopeia/base'
7
7
  autoload :Exception, 'cassiopeia/base'
@@ -11,12 +11,15 @@ module Cassiopeia
11
11
  autoload :ActiveRecordServerMixin, 'cassiopeia/active_record_server_mixin'
12
12
  autoload :ActionControllerServerMixin, 'cassiopeia/action_controller_server_mixin'
13
13
  autoload :ActionControllerClientMixin, 'cassiopeia/action_controller_client_mixin'
14
+ autoload :RackRestoreRequest, 'cassiopeia/rack_restore_request'
14
15
 
15
16
  class << self
16
17
  def enable
17
18
  ActionController::Base.send :extend, ActionControllerServerMixin
18
19
  ActiveRecord::Base.send :extend, ActiveRecordServerMixin
19
20
  ActionController::Base.send :extend, ActionControllerClientMixin
21
+ Rails.configuration.middleware.use RackRestoreRequest if CONFIG[:service_id]
22
+ puts "Cassiopeia 0.0.8 enabled"
20
23
  end
21
24
  end
22
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassiopeia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - smecsia
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-04-07 00:00:00 +04:00
12
+ date: 2010-04-08 00:00:00 +04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -59,6 +59,7 @@ files:
59
59
  - lib/cassiopeia/config.rb
60
60
  - lib/cassiopeia/server.rb
61
61
  - lib/cassiopeia/user.rb
62
+ - lib/cassiopeia/rack_restore_request.rb
62
63
  - lib/cassiopeia/client.rb
63
64
  - lib/cassiopeia/tickets_controller_config.rb
64
65
  - lib/cassiopeia/action_controller_client_mixin.rb