Class: SoldierOfCode::TowerGate

Inherits:
Object
  • Object
show all
Defined in:
lib/watch-tower.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, guards = {}) ⇒ TowerGate

Returns a new instance of TowerGate.



101
102
103
104
# File 'lib/watch-tower.rb', line 101

def initialize(app=nil, guards={})
  @app = app
  @guards = guards
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



99
100
101
# File 'lib/watch-tower.rb', line 99

def app
  @app
end

#guardsObject

Returns the value of attribute guards.



99
100
101
# File 'lib/watch-tower.rb', line 99

def guards
  @guards
end

Instance Method Details

#call(env) ⇒ Object



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
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/watch-tower.rb', line 106

def call(env)
  session = env['rack.session']
  result = [401, {"Content-Type"=>"text/html"}, 'You have been arrested by this server.']
  begin
    guard = locate_guard(env)

    if guard then

      # "just need to check your paper work sir..."
      if guard.authenticated?(env) then
        # "looks like your cleared to enter, have a nice day"
        result = @app.call(env)
      elsif guard.authorized?(env) then
        return_path = session['watchtower.return_path']
        return_path = nil if return_path == ''

        # "looks like your cleared to enter, have a nice day"
        unless return_path then
          result = @app.call(env)
        else
          code = 302
          headers = {"Location" => return_path}
          result = [code, headers, []]
          session['watchtower.return_path'] = nil
        end
      else
        # "If you'd like to step over here sir and please remove your shoes..."
        code = 302
        headers = {"Location" => guard.}
        result = [code, headers, []]
      end
    else
      # No guard on duty - "This compound is monitored at all times" - sure...
      result = @app.call(env)
    end
  rescue => e
    puts "#{__FILE__}:#{__LINE__} [#{__method__}] EXCEPTION: #{e}"
    e.backtrace().each_with_index do |l, i|
      puts "\t- EXCEPTION STACK [#{i}]: #{l}"
    end
  end
  result
end

#locate_guard(env) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/watch-tower.rb', line 150

def locate_guard(env)
  guard = nil
  begin
    session = env['rack.session'] ||= {}
    path = env['REQUEST_PATH']
    @guards.each do |proto_path, deligate|
      if path.starts_with?(proto_path)
        keys = deligate.new.token_keys # I really wanted to meta code a class method but...
        token = nil
        keys.each do |k|
          # check session for key
          token = session[k] if session[k]
        end
        unless token
          session['watchtower.return_path'] = path
          env['rack.session'] = session
          guard = deligate.new
        end
      end
    end
  rescue => e
    puts "#{__FILE__}:#{__LINE__} [#{__method__}] EXCEPTION: #{e}"
    e.backtrace().each_with_index do |l, i|
      puts "\t- EXCEPTION STACK [#{i}]: #{l}"
    end
  end
  guard
end