Sha256: 9d4eb405b609d5c26f36fe6703e7edab29ee97952f6842bf0b42d7c90692f511

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# After sinatra-authorization...

module CloudCrowd
  module Helpers
    module Authorization
      
      # Ensure that the request includes the correct credentials.
      def login_required
        return if authorized?
        unauthorized! unless auth.provided?
        bad_request!  unless auth.basic?
        unauthorized! unless authorize(*auth.credentials)
        request.env['REMOTE_USER'] = auth.username
      end
      
      # Has the request been authenticated?
      def authorized?
        !!request.env['REMOTE_USER']
      end
      
      # A request is authorized if its login and password match those stored
      # in config.yml, or if authentication is disabled.
      def authorize(login, password)
        return true unless CloudCrowd.config[:use_http_authentication]
        return CloudCrowd.config[:login] == login &&
               CloudCrowd.config[:password] == password
      end
      
      
      private
      
      def auth
        @auth ||= Rack::Auth::Basic::Request.new(request.env)
      end
      
      def unauthorized!(realm = App.authorization_realm)
        response['WWW-Authenticate'] = "Basic realm=\"#{realm}\""
        halt 401, 'Authorization Required'
      end
      
      def bad_request!
        halt 400, 'Bad Request'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
documentcloud-cloud-crowd-0.0.4 lib/cloud_crowd/helpers/authorization.rb