Class: Userbin::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/userbin/authentication.rb

Constant Summary collapse

CLOSING_BODY_TAG =
%r{</body>}

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Authentication.



6
7
8
# File 'lib/userbin/authentication.rb', line 6

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

Instance Method Details

#call(env) ⇒ Object



10
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
# File 'lib/userbin/authentication.rb', line 10

def call(env)
  if !Userbin.config.app_id || !Userbin.config.api_secret
    raise ConfigurationError, "app_id and api_secret must be present"
  end

  Thread.current[:userbin] = nil
  env['userbin.unauthenticated'] = false

  request = Rack::Request.new(env)

  begin
    jwt = Userbin.authenticate!(request)

    if !Userbin.authenticated? && Userbin.config.protected_path &&
      env["PATH_INFO"].start_with?(Userbin.config.protected_path)

      return render_gateway(env["PATH_INFO"])
    end

    generate_response(env, jwt)
  rescue Userbin::SecurityError
    message =
      'Userbin::SecurityError: Invalid session. Refresh to try again.'
    headers = {
      'Content-Type' => 'text/text'
    }

    Rack::Utils.delete_cookie_header!(
      headers, '_ubt', value = {})

    [ 400, headers, [message] ]
  end
end

#generate_response(env, jwt) ⇒ Object



94
95
96
97
98
99
100
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
# File 'lib/userbin/authentication.rb', line 94

def generate_response(env, jwt)
  status, headers, response = @app.call(env)

  # application stack is responsible for setting userbin.authenticated
  return render_gateway(env["PATH_INFO"]) if env['userbin.unauthenticated']

  if headers['Content-Type'] && headers['Content-Type']['text/html']
   if response.respond_to?(:body)
     body = [*response.body]
   else
     body = response
   end
   if !Userbin.config.skip_script_injection
     body = body.each.map do |chunk|
       inject_tags(chunk)
     end
   end
   if response.respond_to?(:body)
     response.body = body
   else
     response = body
   end
    unless body.empty?
      headers['Content-Length'] = Rack::Utils.bytesize(body.flatten[0]).to_s
    end
  end

  if jwt
    Rack::Utils.set_cookie_header!(
      headers, '_ubt', value: jwt, path: '/')
  else
    Rack::Utils.delete_cookie_header!(
      headers, '_ubt', value = {})
  end

  [status, headers, response]
end

#inject_tags(body, login_path = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/userbin/authentication.rb', line 62

def inject_tags(body,  = nil)
  if body[CLOSING_BODY_TAG]
    body = body.gsub(CLOSING_BODY_TAG, script_tag() + '\\0')
  end
  body
end

#render_gateway(current_path) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/userbin/authentication.rb', line 69

def render_gateway(current_path)
  script_url = ENV["USERBIN_SCRIPT_URL"] || '//js.userbin.com'

   = <<-LOGIN_PAGE
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Log in</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <a class="ub-login-form"></a>
</body>
</html>
  LOGIN_PAGE

   = inject_tags(, current_path)

  headers = { 'Content-Type' => 'text/html' }

  [403, headers, []]
end

#script_tag(login_path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/userbin/authentication.rb', line 44

def script_tag()
  script_url = ENV.fetch('USERBIN_SCRIPT_URL') {
    "//js.userbin.com"
  }
  path =  || Userbin.config.protected_path

  tag =  "<script src='#{script_url}?#{Userbin.config.app_id}'></script>\n"
  tag += "<script type='text/javascript'>\n"
  tag += "  Userbin.config({\n"
  if Userbin.config.root_path
    tag += "    logoutRedirectUrl: '#{Userbin.config.root_path}',\n"
  end
  tag += "    loginRedirectUrl: '#{path}',\n" if path
  tag += "    reloadOnSuccess: true\n"
  tag += "  });\n"
  tag += "</script>\n"
end