Class: Rhoconnect::Middleware::XDomainSessionWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rhoconnect/middleware/x_domain_session_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) {|_self| ... } ⇒ XDomainSessionWrapper

Returns a new instance of XDomainSessionWrapper.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
13
# File 'lib/rhoconnect/middleware/x_domain_session_wrapper.rb', line 7

def initialize(app, opts={})
  @app = app
  @session_cookie = opts[:session_cookie] || 'rhoconnect_session'
  @api_uri_regexp = opts[:api_uri_regexp] || /\A\/api\/application/
  @login_uri_regexp = opts[:login_uri_regexp] || /\A\/api\/application\/clientlogin/
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rhoconnect/middleware/x_domain_session_wrapper.rb', line 20

def call(env)
  if is_sync_protocol(env)
    env['HTTP_COOKIE'] = env['HTTP_COOKIE'] || CGI.unescape(get_session_from_url(env))
  end
  #puts "and here #{@app.inspect} #{env.inspect}"
  status, headers, body = @app.call(env)

  if is_sync_protocol(env)
    cookies = headers['Set-Cookie'].to_s
    #puts "<----- Cookies: #{cookies}"
    # put cookies to body as JSON on login success
    if @login_uri_regexp.match(env['PATH_INFO']) && status == 200
      body = session_json_from(cookies)
      headers['Content-Length'] = body.length.to_s
    end
  end

  # The Body itself should not be an instance of String,as this will break in Ruby 1.9
  body = ["#{body}"] if body.is_a?(String)
  [status, headers, body]
end

#get_session_from_url(env) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/rhoconnect/middleware/x_domain_session_wrapper.rb', line 48

def get_session_from_url(env)
  rexp = Regexp.new(@session_cookie +'=.*\Z')
  qs = env['QUERY_STRING'].to_s.slice rexp
  qs = qs.to_s.split(/&/)[0]
  nv = qs.to_s.split(/=/)
  return nv[1] if nv.length > 1
  ''
end

#is_sync_protocol(env) ⇒ Object



15
16
17
18
# File 'lib/rhoconnect/middleware/x_domain_session_wrapper.rb', line 15

def is_sync_protocol(env)
  # if it is rhoconnect protocol URI
  @api_uri_regexp.match(env['PATH_INFO'])
end

#session_json_from(cookies) ⇒ Object



42
43
44
45
46
# File 'lib/rhoconnect/middleware/x_domain_session_wrapper.rb', line 42

def session_json_from(cookies)
  rexp = Regexp.new(@session_cookie +'=[^\s]*')
  sc = cookies.to_s.slice rexp
  "{\"" +@session_cookie +"\": \"#{CGI.escape sc.to_s}\"}"
end