Sha256: bcd642223f3da04beb23cecc6ed746812bcdd1bc4ae5b51bfe24c5eb6c81b49d

Contents?: true

Size: 1.17 KB

Versions: 21

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require_relative 'abstract/handler'
require_relative 'abstract/request'
require 'base64'

module Rack
  module Auth
    # Rack::Auth::Basic implements HTTP Basic Authentication, as per RFC 2617.
    #
    # Initialize with the Rack application that you want protecting,
    # and a block that checks if a username and password pair are valid.

    class Basic < AbstractHandler

      def call(env)
        auth = Basic::Request.new(env)

        return unauthorized unless auth.provided?

        return bad_request unless auth.basic?

        if valid?(auth)
          env['REMOTE_USER'] = auth.username

          return @app.call(env)
        end

        unauthorized
      end


      private

      def challenge
        'Basic realm="%s"' % realm
      end

      def valid?(auth)
        @authenticator.call(*auth.credentials)
      end

      class Request < Auth::AbstractRequest
        def basic?
          "basic" == scheme && credentials.length == 2
        end

        def credentials
          @credentials ||= Base64.decode64(params).split(':', 2)
        end

        def username
          credentials.first
        end
      end

    end
  end
end

Version data entries

21 entries across 20 versions & 5 rubygems

Version Path
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/rack-3.0.11/lib/rack/auth/basic.rb
rack-3.0.10 lib/rack/auth/basic.rb
rack-3.0.9.1 lib/rack/auth/basic.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.9/lib/rack/auth/basic.rb
rack-3.0.9 lib/rack/auth/basic.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rack-3.0.8/lib/rack/auth/basic.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.8/lib/rack/auth/basic.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rack-3.0.7/lib/rack/auth/basic.rb
rack-3.0.8 lib/rack/auth/basic.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/rack-3.0.7/lib/rack/auth/basic.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/rack-3.0.7/lib/rack/auth/basic.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/rack-3.0.7/lib/rack/auth/basic.rb
rack-3.0.7 lib/rack/auth/basic.rb
rack-3.0.6.1 lib/rack/auth/basic.rb
rack-3.0.6 lib/rack/auth/basic.rb
rack-3.0.5 lib/rack/auth/basic.rb
rack-3.0.4.2 lib/rack/auth/basic.rb
rack-3.0.4.1 lib/rack/auth/basic.rb
rack-3.0.4 lib/rack/auth/basic.rb
rack-3.0.3 lib/rack/auth/basic.rb