Class: Userbin::Authentication
- Inherits:
-
Object
- Object
- Userbin::Authentication
- Defined in:
- lib/userbin/authentication.rb
Constant Summary collapse
- CLOSING_HEAD_TAG =
%r{</head>}- CLOSING_BODY_TAG =
%r{</body>}
Instance Method Summary collapse
- #call(env) ⇒ Object
- #generate_response(env, signature, data) ⇒ Object
-
#initialize(app, options = {}) ⇒ Authentication
constructor
A new instance of Authentication.
- #inject_tags(body, login_path = restrict) ⇒ Object
- #link_tags(login_path) ⇒ Object
- #render_gateway(current_path) ⇒ Object
- #restrict ⇒ Object
- #script_tag ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ Authentication
Returns a new instance of Authentication.
7 8 9 |
# File 'lib/userbin/authentication.rb', line 7 def initialize(app, = {}) @app = app end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/userbin/authentication.rb', line 11 def call(env) request = Rack::Request.new(env) begin if env["PATH_INFO"] == "/userbin" && env["REQUEST_METHOD"] == "POST" signature, data = Userbin.authenticate_events!(request) MultiJson.decode(data)['events'].each do |event| Userbin::Events.trigger(event) end [ 200, { 'Content-Type' => 'text/html', 'Content-Length' => '2' }, ['OK'] ] else signature, data = Userbin.authenticate!(request) if restrict && env["PATH_INFO"].start_with?(restrict) && !Userbin.authenticated? return render_gateway(env["REQUEST_PATH"]) end generate_response(env, signature, data) end rescue Userbin::SecurityError = 'Userbin::SecurityError: Invalid signature. Refresh to try again.' headers = { 'Content-Type' => 'text/text', 'Content-Length' => .length.to_s } Rack::Utils.( headers, '_ubs', value = {}) Rack::Utils.( headers, '_ubd', value = {}) [ 400, headers, [] ] end end |
#generate_response(env, signature, data) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/userbin/authentication.rb', line 101 def generate_response(env, signature, data) status, headers, response = @app.call(env) if headers['Content-Type'] && headers['Content-Type']['text/html'] if response.respond_to?(:body) body = [*response.body] else body = response end if Userbin.config. body = body.each.map do |chunk| (chunk) end end if response.respond_to?(:body) response.body = body else response = body end end if signature && data Rack::Utils.( headers, '_ubs', value: signature, path: '/') Rack::Utils.( headers, '_ubd', value: data, path: '/') else Rack::Utils.( headers, '_ubs', value = {}) Rack::Utils.( headers, '_ubd', value = {}) end [status, headers, response] end |
#inject_tags(body, login_path = restrict) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/userbin/authentication.rb', line 91 def (body, login_path = restrict) if body[CLOSING_HEAD_TAG] body = body.gsub(CLOSING_HEAD_TAG, (login_path) + '\\0') end if body[CLOSING_BODY_TAG] body = body.gsub(CLOSING_BODY_TAG, script_tag + '\\0') end body end |
#link_tags(login_path) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/userbin/authentication.rb', line 56 def (login_path) <<-LINK_TAGS <link rel="userbin:root" href="/" /> <link rel="userbin:login" href="#{login_path}" /> LINK_TAGS end |
#render_gateway(current_path) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/userbin/authentication.rb', line 72 def render_gateway(current_path) login_page = <<-LOGIN_PAGE <html> <head> <title>Log in</title> </head> <body> <a class="ub-login-form"></a> </body> </html> LOGIN_PAGE login_page = (login_page, current_path) [ 403, { 'Content-Type' => 'text/html', 'Content-Length' => login_page.length.to_s }, [login_page] ] end |