class Rack::BearerAuth::Middleware

Public Class Methods

new(app, &block) click to toggle source
# File lib/rack/bearer_auth/middleware.rb, line 9
def initialize(app, &block)
  raise ArgumentError, "Block argument is required." unless block_given?

  @app = app
  @match_patterns = []

  instance_exec(&block)
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/bearer_auth/middleware.rb, line 18
def call(env)
  req = Request.new(env)

  handle(req) || @app.call(env)
end
match(path: nil, via: nil, token: nil, &block) click to toggle source
# File lib/rack/bearer_auth/middleware.rb, line 24
def match(path: nil, via: nil, token: nil, &block)
  if block_given?
    warn "Token paramter is ignored." if token
    token = block
  end

  @match_patterns << MatchPattern.new(path, via, token)
end

Private Instance Methods

handle(req) click to toggle source
# File lib/rack/bearer_auth/middleware.rb, line 35
def handle(req) # rubocop:disable Metrics/MethodLength
  @match_patterns.each do |pattern|
    case pattern.match(req)
    when :skip
      next
    when :ok
      break
    when :token_required
      return [401,
              { "WWW-Authenticate" => 'Bearer realm="token_required"',
                "Content-Type"     => "text/plain; charset=utf-8",
                "Content-Length"   => "0" },
              []]
    when :invalid_token
      return [401,
              { "WWW-Authenticate" => 'Bearer error="invalid_token"',
                "Content-Type"     => "text/plain; charset=utf-8",
                "Content-Length"   => "0" },
              []]
    else
      warn "A pattern is ignored."
    end
  end
  nil
end