Sha256: 71626a7108a1e7290b46153747946f8c405e88ffe2d879a64c930ff69d859b49

Contents?: true

Size: 1.23 KB

Versions: 66

Compression:

Stored size: 1.23 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.
    #
    # See also: <tt>example/protectedlobster.rb</tt>

    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

66 entries across 51 versions & 14 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rack-2.2.9/lib/rack/auth/basic.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/auth/basic.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/auth/basic.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rack-2.2.7/lib/rack/auth/basic.rb
rack-2.2.9 lib/rack/auth/basic.rb
rack-2.2.8.1 lib/rack/auth/basic.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.7/lib/rack/auth/basic.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rack-2.2.4/lib/rack/auth/basic.rb
rack-2.2.8 lib/rack/auth/basic.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.4/lib/rack/auth/basic.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.7/lib/rack/auth/basic.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/auth/basic.rb
rack-2.2.7 lib/rack/auth/basic.rb
rack-2.2.6.4 lib/rack/auth/basic.rb
rack-2.2.6.3 lib/rack/auth/basic.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/auth/basic.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/auth/basic.rb
rack-2.2.6.2 lib/rack/auth/basic.rb