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
rack-2.2.6.1 lib/rack/auth/basic.rb
rack-2.2.6 lib/rack/auth/basic.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rack-2.2.5/lib/rack/auth/basic.rb
rack-2.2.5 lib/rack/auth/basic.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/rack-3.0.1/lib/rack/auth/basic.rb
rack-3.0.1 lib/rack/auth/basic.rb
rack-3.0.0 lib/rack/auth/basic.rb
rack-3.0.0.rc1 lib/rack/auth/basic.rb
diy_rails-0.1.0 vendor/bundle/ruby/3.0.0/gems/rack-2.2.4/lib/rack/auth/basic.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/auth/basic.rb
rack-3.0.0.beta1 lib/rack/auth/basic.rb
rack-2.2.4 lib/rack/auth/basic.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/rack-2.2.3.1/lib/rack/auth/basic.rb
rack-2.2.3.1 lib/rack/auth/basic.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/2.6.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
devcycle-ruby-server-sdk-1.1.0 examples/sinatra/vendor/bundle/ruby/3.1.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/auth/basic.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/rack-2.2.3/lib/rack/auth/basic.rb