Class: Rack::Contrib::Nonce

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/nonce.rb

Constant Summary collapse

VERSION =
'0.0.2'

Instance Method Summary collapse

Constructor Details

#initialize(app, opts) ⇒ Nonce

Returns a new instance of Nonce.



6
7
8
9
10
11
# File 'lib/rack/contrib/nonce.rb', line 6

def initialize app, opts
  @app = app
  @logger = opts[:logger] || Logger.new('/dev/null')
  @seen = opts[:seen] || []
  @header = opts[:header] || 'Nonce'
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/contrib/nonce.rb', line 17

def call env
  unless env[header_name]
    @logger.error "Denied: #{@header} not present."
    return [401, {}, []]
  end

  if @seen.include? env[header_name]
    @logger.error "Denied: #{@header} not unique."
    return [401, {}, []]
  end

  @seen << env[header_name]

  @app.call(env)
end

#header_nameObject



13
14
15
# File 'lib/rack/contrib/nonce.rb', line 13

def header_name
  'HTTP_' + @header.upcase.gsub(/-/, '_')
end