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) ⇒ Middleware
constructor
A new instance of Middleware.
- #pass_through(env) ⇒ Object
- #redirect_to_callback(env, form_hash) ⇒ Object
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
82 83 84 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 82 def initialize(app) @app = app end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
20 21 22 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 20 def app @app end |
#env ⇒ Object
Returns the value of attribute env.
20 21 22 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 20 def env @env end |
Instance Method Details
#auth_request?(env, form_hash) ⇒ Boolean
34 35 36 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 34 def auth_request? env, form_hash env["REQUEST_METHOD"] == "POST" && form_hash.keys.include?("sqauthsession") && form_hash.keys.include?("callback") end |
#call(env) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 21 def call(env) req = Rack::Request.new(env) SqAuth.access.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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 45 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
77 78 79 80 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 77 def pass_through env status, headers, response = app.call(env) [status, headers, response] end |
#redirect_to_callback(env, form_hash) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 38 def redirect_to_callback env, form_hash env["rack.session"][:sqauthsession] = form_hash["sqauthsession"] if env["rack.session"] SqAuth.access.save_session_for_current_user form_hash["sqauthsession"] [302, {'Location' => form_hash["callback"]}, ['Authenticated']] end |