Class: SqAuth::SqAuthIntegration::SqAuthRack::Middleware
- Inherits:
-
Object
- Object
- SqAuth::SqAuthIntegration::SqAuthRack::Middleware
- Defined in:
- lib/sq_auth/sq_auth_integration/sq_auth_rack.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#env ⇒ Object
Returns the value of attribute env.
Instance Method Summary collapse
- #auth_request?(env, form_hash) ⇒ Boolean
- #call(env) ⇒ Object
- #callback_uri(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Middleware
constructor
A new instance of Middleware.
- #pass_through(env) ⇒ Object
- #redirect_to_callback(env, form_hash) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Middleware
Returns a new instance of Middleware.
89 90 91 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 89 def initialize(app, = {}) @app = app end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
26 27 28 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 26 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
26 27 28 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 26 def env @env end |
Instance Method Details
#auth_request?(env, form_hash) ⇒ Boolean
40 41 42 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 40 def auth_request? env, form_hash env["REQUEST_METHOD"] == "POST" && form_hash.keys.include?("sqauthsession") && form_hash.keys.include?("callback") end |
#call(env) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 27 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) else pass_through env end end |
#callback_uri(env) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 52 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. return "" end |
#pass_through(env) ⇒ Object
84 85 86 87 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 84 def pass_through env status, headers, response = app.call(env) [status, headers, response] end |
#redirect_to_callback(env, form_hash) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 44 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.save_session_for_current_user form_hash["sqauthsession"] [302, {'Location' => form_hash["callback"]}, ['Authenticated']] end |