Class: SqAuth::SqAuthIntegration::SqAuthRack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sq_auth/sq_auth_integration/sq_auth_rack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



112
113
114
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 112

def initialize(app, options = {})
  @app = app
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



36
37
38
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 36

def app
  @app
end

#envObject

Returns the value of attribute env.



36
37
38
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 36

def env
  @env
end

Instance Method Details

#auth_request?(env, form_hash) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 52

def auth_request? env, form_hash
  env["REQUEST_METHOD"] == "POST" && form_hash.keys.include?("sqauthsession") && form_hash.keys.include?("callback")
end

#call(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 37

def call(env)
  req = Rack::Request.new(env)
  SqAuth.access.set_callback(callback_uri(env))
  SqAuth.access.current_user_ip = env["HTTP_X_FORWARDED_FOR"] || env["REMOTE_ADDR"]
  SqAuth.access.save_session_for_current_user (env["rack.session"]||{})[:sqauthsession]
  form_hash = req.params||{}
  if auth_request?(env, form_hash)
    redirect_to_callback(env, form_hash)
  elsif revoke_session_request?(env, form_hash)
    revoke_session(env, form_hash)
  else
    pass_through env
  end
end

#callback_uri(env) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 75

def callback_uri env
  if URI.parse(env["REQUEST_URI"]).host
    env["REQUEST_URI"]
  else
    uri_type = URI.const_get(env["HTTP_X_URL_SCHEME"].upcase) rescue nil
    uri_type ||= URI.const_get(env["rack.url_scheme"].upcase) rescue nil
    uri_type ||= URI::HTTP
    if env["HTTP_HOST"].is_a? String
      host, port = env["HTTP_HOST"].split(":")
    end
    host ||= env["SERVER_NAME"]
    port ||= env["SERVER_PORT"]
    path = env["REQUEST_PATH"].to_s
    if path.empty?
      path = URI::parse(env["PATH_INFO"]).path rescue ""
    end
    if path.empty?
      path = URI::parse(env["REQUEST_URI"]).path
    end
    query = env["QUERY_STRING"]
    if port
      uri = uri_type.build(host: host, port: port.to_i, path: path, query: query)
    else
      uri = uri_type.build(host: host, path: path, query: query)
    end
    uri.to_s
  end
rescue Exception => ex
  p ex.message
  return ""
end

#pass_through(env) ⇒ Object



107
108
109
110
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 107

def pass_through env
  status, headers, response = app.call(env)
  [status, headers, response]
end

#redirect_to_callback(env, form_hash) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 60

def redirect_to_callback env, form_hash
  env["rack.session"][:sqauthsession] = form_hash["sqauthsession"] if env["rack.session"]
  env["rack.session"][:current_user] = form_hash["current_user"] if env["rack.session"]
  SqAuth.access.current_user_name = form_hash["current_user"]
  SqAuth.access.save_session_for_current_user form_hash["sqauthsession"]
  SqAuth.access.save_username_for_current_user form_hash["current_user"]

  [302, {'Content-Type'=>'text/plain', 'Location' => form_hash["callback"]}, ['Authenticated']]
end

#revoke_session(env, form_hash) ⇒ Object



70
71
72
73
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 70

def revoke_session env, form_hash
  SqAuth.access.revoke_session(form_hash["sqauthsession"])
  [200, {'Content-Type'=>'text/plain'}, ['Session revoked']]
end

#revoke_session_request?(env, form_hash) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 56

def revoke_session_request? env, form_hash
  env["REQUEST_METHOD"] == "POST" && form_hash.keys.include?("sqauthsession") && form_hash.keys.include?("revoke_session")
end