Sha256: 7bc24f3febf5ab5b30d70ffc832501eb426892eea95b66768027bb6275f12893
Contents?: true
Size: 1.47 KB
Versions: 15
Compression:
Stored size: 1.47 KB
Contents
require 'wisper' require 'base64' require 'routemaster/config' module Routemaster module Middleware # 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. # class Authenticate include Wisper::Publisher # @param uuid [Enumerable] a set of accepted authentication tokens def initialize(app, uuid: nil) @app = app @uuid = uuid || Config.drain_tokens unless @uuid.kind_of?(String) || @uuid.kind_of?(Enumerable) raise ArgumentError, ':uuid must be a String or Enumerable' end end 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 def _has_auth?(env) env.has_key?('HTTP_AUTHORIZATION') end def _valid_auth?(env) token = Base64. decode64(env['HTTP_AUTHORIZATION'].gsub(/^Basic /, '')). split(':').first @uuid.include?(token) end end end end
Version data entries
15 entries across 15 versions & 1 rubygems