Sha256: 5e3c6dca6f143702d1ec81f50788fbbe9c6c62610a2651925a162e783ceb02be

Contents?: true

Size: 1.21 KB

Versions: 35

Compression:

Stored size: 1.21 KB

Contents

require 'sinatra/base'

# Lazy Basic HTTP authentication. Authentication is only forced when the
# credentials are actually needed.
module Sinatra
  module LazyAuth
    class LazyCredentials
      def initialize(app)
        @app = app
        @provided = false
      end

      def user
        credentials!
        @user
      end

      def password
        credentials!
        @password
      end

      def provided?
        @provided
      end

      private
      def credentials!
        unless provided?
          auth = Rack::Auth::Basic::Request.new(@app.request.env)
          unless auth.provided? && auth.basic? && auth.credentials
            @app.authorize!
          end
          @user = auth.credentials[0]
          @password = auth.credentials[1]
          @provided = true
        end
      end

    end

    def authorize!
      r = "#{DRIVER}-deltacloud@#{HOSTNAME}"
      response['WWW-Authenticate'] = %(Basic realm="#{r}")
      throw(:halt, [401, "Not authorized\n"])
    end

    # Request the current user's credentials. Actual credentials are only
    # requested when an attempt is made to get the user name or password
    def credentials
      LazyCredentials.new(self)
    end
  end

  helpers LazyAuth
end

Version data entries

35 entries across 35 versions & 5 rubygems

Version Path
wakame-vdc-agents-11.12.0 lib/sinatra/lazy_auth.rb
wakame-vdc-dcmgr-11.12.0 lib/sinatra/lazy_auth.rb
wakame-vdc-agents-11.06.0 lib/sinatra/lazy_auth.rb
wakame-vdc-dcmgr-11.06.0 lib/sinatra/lazy_auth.rb
wakame-vdc-dcmgr-10.12.0 lib/sinatra/lazy_auth.rb
wakame-vdc-agents-10.12.0 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.1.1.3 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.1.1.2 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.1.1.1 lib/sinatra/lazy_auth.rb
deltacloud-core-0.1.1 lib/sinatra/lazy_auth.rb
deltacloud-core-0.1.0 lib/sinatra/lazy_auth.rb
wakame-vdc-dcmgr-10.11.0 lib/sinatra/lazy_auth.rb
wakame-vdc-agents-10.11.0 lib/sinatra/lazy_auth.rb
deltacloud-core-0.0.9 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.0.8.1-java lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.0.8.1 lib/sinatra/lazy_auth.rb
deltacloud-core-0.0.8 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.0.7.2 lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.0.7.2-java lib/sinatra/lazy_auth.rb
steamcannon-deltacloud-core-0.0.7.1 lib/sinatra/lazy_auth.rb