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.
101 102 103 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 101 def initialize(app, = {}) @app = app end |
Instance Attribute Details
#app ⇒ Object
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 |
#env ⇒ Object
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
50 51 52 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 50 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 |
# 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) else pass_through env end end |
#callback_uri(env) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 64 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
96 97 98 99 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 96 def pass_through env status, headers, response = app.call(env) [status, headers, response] end |
#redirect_to_callback(env, form_hash) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/sq_auth/sq_auth_integration/sq_auth_rack.rb', line 54 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 |