class Routemaster::Middleware::Authenticate

Authenticates requests according to the Routemaster spec.

Broadcasts ‘:authenticate` with one of `:missing`, `failed`, or `:succeeded`.

This is very close to ‘Rack::Auth::Basic`, in that HTTP Basic is used; but the password part is ignored. In other words, this performs token authentication using HTTP Basic.

Public Class Methods

new(app, options = {}) click to toggle source

options [Enumerable] a set of accepted authentication tokens

# File lib/routemaster/middleware/authenticate.rb, line 20
def initialize(app, options = {})
  @app  = app
  @uuid = options.fetch(:uuid) { Config.drain_tokens }

  unless @uuid.kind_of?(String) || @uuid.kind_of?(Enumerable)
    raise ArgumentError, ':uuid must be a String or Enumerable'
  end
end

Public Instance Methods

call(env) click to toggle source
# File lib/routemaster/middleware/authenticate.rb, line 29
def call(env)
  unless _has_auth?(env)
    publish(:authenticate, :missing, env)
    return [401, {}, []]
  end

  unless _valid_auth?(env)
    publish(:authenticate, :failed, env)
    return [403, {}, []]
  end

  publish(:authenticate, :succeeded, env)
  @app.call(env)
end

Private Instance Methods

_has_auth?(env) click to toggle source
# File lib/routemaster/middleware/authenticate.rb, line 46
def _has_auth?(env)
  env.has_key?('HTTP_AUTHORIZATION')
end
_valid_auth?(env) click to toggle source
# File lib/routemaster/middleware/authenticate.rb, line 50
def _valid_auth?(env)
  token = Base64.
    decode64(env['HTTP_AUTHORIZATION'].gsub(/^Basic /, '')).
    split(':').first
  @uuid.include?(token)
end