Sha256: 2c3330b09ca8330aa98812c1e1b3bc694381c67de8d6a8591a1a009b82341584

Contents?: true

Size: 1.74 KB

Versions: 25

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env ruby

$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

require 'webrick'
require 'httpauth/digest'
require 'yaml'

include WEBrick

s = HTTPServer.new :Port => 2000, :AccessLog => [[File.open('/dev/null', 'w'), AccessLog::COMMON_LOG_FORMAT],
  [File.open('/dev/null', 'w'), AccessLog::REFERER_LOG_FORMAT]]

class AuthenticationServlet < HTTPServlet::AbstractServlet
  include HTTPAuth::Digest
  def do_GET(request, response)
    puts '-'*79
    puts "request: Authorization: " + (request['Authorization'] || '')
    
    credentials = Credentials.from_header(request['Authorization']) unless request['Authorization'].nil?
    if !credentials.nil? and credentials.validate :password => 'secret', :method => 'GET'
      response.status = 200
      auth_info = AuthenticationInfo.from_credentials credentials
      response['Authentication-Info'] = auth_info.to_header
      response['Content-Type'] = 'text/plain; charset=utf-8'
      response.body  = 'You are authorized'
      puts "response: Authentication-Info: " + response['Authentication-Info']
    else
      if credentials
        puts '[!] FAILED: ' + credentials.reason
      else
        puts '[!] FAILED: No credentials specified'
      end
      response.status = 401
      challenge = Challenge.new :realm => 'admin@httpauth.example.com', :qop => ['auth']
      response['WWW-Authenticate'] = challenge.to_header
      response['Content-Type'] = 'text/plain; charset=utf-8'
      response.body  = 'You are not authorized'
      puts "response: WWW-Authenticate: " + response['WWW-Authenticate']
    end
  end
end

puts "\n>>> Open http://localhost:2000/ and login with password 'secret', any username should work\n\n"
s.mount '/', AuthenticationServlet
trap('INT') { s.shutdown }
s.start

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
abiquo-etk-0.6.4 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.6.3 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.6.2 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.6.1 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.6.0 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.5.9 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.5.8 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.5.3 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.42 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.33 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.32 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.29 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.25 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.24 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.23 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.22 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.20 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.19 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.18 vendor/httpauth-0.1/examples/server_digest_secure
abiquo-etk-0.4.17 vendor/httpauth-0.1/examples/server_digest_secure