Class: Sanger::Rack::Authentication::SSO

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_authentication_sso.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, settings = {}) ⇒ SSO

Returns a new instance of SSO.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rack_authentication_sso.rb', line 11

def initialize(app, settings = {})
  @config = OpenStruct.new
  @config.magic_header_name = settings["magic_header_name"]
  @config.cookie_name = settings["cookie_name"]
  @config.validation_url = settings["validation_url"]
  @config.user_agent = settings["user_agent"]
  @config.sso_redirection_url = settings["sso_redirection_url"]
  @config.error_text = settings["error_text"]
  
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack_authentication_sso.rb', line 23

def call(env)
  @request = ::Rack::Request.new(env)
  @request.env.delete(@config.magic_header_name)
  cookie_value = @request.cookies[@config.cookie_name]
  begin
    user = (cookie_value) unless cookie_value.nil?
    if user
      @request.env[@config.magic_header_name] = user
      @app.call(@request.env)
    else
      redirect_to_sso_server
    end
  rescue ::OpenURI::HTTPError
    error_text = @config.error_text.to_s
    [503, {"Content-Type" => "text/plain", "Content-Length" => error_text.length.to_s}, [error_text]]
  end
end

#redirect_to_sso_serverObject



55
56
57
58
59
60
# File 'lib/rack_authentication_sso.rb', line 55

def redirect_to_sso_server
  @response = ::Rack::Response.new
  destination = ::CGI::escape(@request.url)
  @response.redirect([@config.sso_redirection_url, destination].join)
  @response.finish
end


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack_authentication_sso.rb', line 41

def (cookie_value)
   = nil
  ::OpenURI.open_uri(@config.validation_url,
    "Cookie" => "#{@config.cookie_name}=#{cookie_value}",
    "User-Agent" => @config.user_agent) do |http|
     = http.read.strip
  end
  if  == '*'
    return nil
  else
    return 
  end
end