Sha256: 7458f54b167af6e465d4b202639f02a16746606af622dc45627936dc2fd8fc81
Contents?: true
Size: 1.48 KB
Versions: 18
Compression:
Stored size: 1.48 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
18 entries across 18 versions & 1 rubygems