Skip to content

Commit a3bee1b

Browse files
committed
Use Bundler in stand-alone mode to install required Ruby Gems
1 parent c5dd7f3 commit a3bee1b

18 files changed

+565
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.bundle/

Gemfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'https://rubygems.org/'
2+
gem 'net-http-digest_auth'

Gemfile.lock

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
net-http-digest_auth (1.4)
5+
6+
PLATFORMS
7+
x64-mingw32
8+
9+
DEPENDENCIES
10+
net-http-digest_auth

bundle/bundler/setup.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rbconfig'
2+
# ruby 1.8.7 doesn't define RUBY_ENGINE
3+
ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'
4+
ruby_version = RbConfig::CONFIG["ruby_version"]
5+
path = File.expand_path('..', __FILE__)
6+
$:.unshift File.expand_path("#{path}/../#{ruby_engine}/#{ruby_version}/gems/net-http-digest_auth-1.4/lib")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- ruby -*-
2+
3+
require 'autotest/restart'
4+
5+
Autotest.add_hook :initialize do |at|
6+
at.testlib = 'minitest/unit'
7+
end
8+

bundle/ruby/2.0.0/gems/net-http-digest_auth-1.4/.gemtest

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== 1.4 / 2013-07-23
2+
3+
* Minor enhancements
4+
* Relaxed parser to accept quoted algorithm to work with Linksys SPA922.
5+
Pull request #8 by Ismail Hanli, Issue #5 by bearded
6+
7+
=== 1.3 / 2012-03-28
8+
9+
* Minor enhancements
10+
* The cnonce is regenerated for every request to improve security.
11+
* SecureRandom is used to generate the cnonce instead of Kernel#rand
12+
* Bug fix
13+
* cnonce and nonce-count are no longer sent when qop was not provided per
14+
RFC 2617 section 3.2.2.
15+
16+
=== 1.2.1 / 2012-05-18
17+
18+
* Bug fix
19+
* Fixed -sess authentication. This also fixes pull request #4 by joe81
20+
21+
=== 1.2 / 2011-11-22
22+
23+
* Minor enhancement
24+
* Now thread safe. Issue #2 by chrisochs.
25+
26+
=== 1.1.1 / 2011-04-03
27+
28+
* Bug fix
29+
* Fix syntax error on ruby 1.8
30+
31+
=== 1.1 / 2011-03-29
32+
33+
* Minor enhancements
34+
* Add support for SHA1, SHA2, SHA256, SHA384, SHA512, RMD160 algorithms
35+
* Bug fixes
36+
* Support opaque per RFC 2617 3.2.1
37+
* Support MD5-sess per RFC 2617 3.2.2.2
38+
* Support unspecified qop for RFC 2069 compatibility per RFC 2617 3.2.2.1
39+
40+
=== 1.0 / 2010-09-10
41+
42+
* Major enhancements
43+
* Birthday!
44+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.autotest
2+
History.txt
3+
Manifest.txt
4+
README.txt
5+
Rakefile
6+
lib/net/http/digest_auth.rb
7+
sample/auth_server.rb
8+
sample/net_http_example.rb
9+
test/test_net_http_digest_auth.rb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
= net-http-digest_auth
2+
3+
code :: http://github.com/drbrain/net-http-digest_auth
4+
rdoc :: http://docs.seattlerb.org/net-http-digest_auth
5+
other :: http://www.rfc-editor.org/rfc/rfc2617.txt
6+
7+
== DESCRIPTION:
8+
9+
An implementation of RFC 2617 - Digest Access Authentication. At this time
10+
the gem does not drop in to Net::HTTP and can be used for with other HTTP
11+
clients.
12+
13+
In order to use net-http-digest_auth you'll need to perform some request
14+
wrangling on your own. See the class documentation at Net::HTTP::DigestAuth
15+
for an example.
16+
17+
== INSTALL:
18+
19+
gem install net-http-digest_auth
20+
21+
== LICENSE:
22+
23+
(The MIT License)
24+
25+
Copyright (c) Eric Hodel
26+
27+
Permission is hereby granted, free of charge, to any person obtaining
28+
a copy of this software and associated documentation files (the
29+
'Software'), to deal in the Software without restriction, including
30+
without limitation the rights to use, copy, modify, merge, publish,
31+
distribute, sublicense, and/or sell copies of the Software, and to
32+
permit persons to whom the Software is furnished to do so, subject to
33+
the following conditions:
34+
35+
The above copyright notice and this permission notice shall be
36+
included in all copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
39+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
42+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
43+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- ruby -*-
2+
3+
require 'rubygems'
4+
require 'hoe'
5+
6+
Hoe.plugin :git
7+
Hoe.plugin :minitest
8+
Hoe.plugin :travis
9+
10+
Hoe.spec 'net-http-digest_auth' do
11+
developer 'Eric Hodel', '[email protected]'
12+
13+
rdoc_locations <<
14+
'docs.seattlerb.org:/data/www/docs.seattlerb.org/net-http-digest_auth/'
15+
rdoc_locations <<
16+
'rubyforge.org:/var/www/gforge-projects/seattlerb/net-http-digest_auth/'
17+
18+
self.spec_extras[:required_ruby_version] = '>= 1.8.7'
19+
end
20+
21+
# vim: syntax=Ruby
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
require 'cgi'
2+
require 'digest'
3+
require 'monitor'
4+
require 'net/http'
5+
require 'securerandom'
6+
7+
##
8+
# An implementation of RFC 2617 Digest Access Authentication.
9+
#
10+
# http://www.rfc-editor.org/rfc/rfc2617.txt
11+
#
12+
# Here is a sample usage of DigestAuth on Net::HTTP:
13+
#
14+
# require 'uri'
15+
# require 'net/http'
16+
# require 'net/http/digest_auth'
17+
#
18+
# digest_auth = Net::HTTP::DigestAuth.new
19+
#
20+
# uri = URI.parse 'http://localhost:8000/'
21+
# uri.user = 'username'
22+
# uri.password = 'password'
23+
#
24+
# h = Net::HTTP.new uri.host, uri.port
25+
#
26+
# req = Net::HTTP::Get.new uri.request_uri
27+
#
28+
# res = h.request req
29+
# # res is a 401 response with a WWW-Authenticate header
30+
#
31+
# auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'
32+
#
33+
# # create a new request with the Authorization header
34+
# req = Net::HTTP::Get.new uri.request_uri
35+
# req.add_field 'Authorization', auth
36+
#
37+
# # re-issue request with Authorization
38+
# res = h.request req
39+
40+
class Net::HTTP::DigestAuth
41+
42+
include MonitorMixin
43+
44+
##
45+
# DigestAuth error class
46+
47+
class Error < RuntimeError; end
48+
49+
##
50+
# Version of Net::HTTP::DigestAuth you are using
51+
52+
VERSION = '1.4'
53+
54+
##
55+
# Creates a new DigestAuth header creator.
56+
57+
def initialize ignored = :ignored
58+
mon_initialize
59+
@nonce_count = -1
60+
end
61+
62+
##
63+
# Creates a digest auth header for +uri+ from the +www_authenticate+ header
64+
# for HTTP method +method+.
65+
#
66+
# The result of this method should be sent along with the HTTP request as
67+
# the "Authorization" header. In Net::HTTP this will look like:
68+
#
69+
# request.add_field 'Authorization', digest_auth.auth_header # ...
70+
#
71+
# See Net::HTTP::DigestAuth for a complete example.
72+
#
73+
# IIS servers handle the "qop" parameter of digest authentication
74+
# differently so you may need to set +iis+ to true for such servers.
75+
76+
def auth_header uri, www_authenticate, method, iis = false
77+
nonce_count = next_nonce
78+
79+
user = CGI.unescape uri.user
80+
password = CGI.unescape uri.password
81+
82+
www_authenticate =~ /^(\w+) (.*)/
83+
84+
challenge = $2
85+
86+
params = {}
87+
challenge.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
88+
89+
challenge =~ /algorithm="?(.*?)"?([, ]|$)/
90+
91+
params['algorithm'] = $1 || 'MD5'
92+
93+
if params['algorithm'] =~ /(.*?)(-sess)?$/
94+
algorithm = case $1
95+
when 'MD5' then Digest::MD5
96+
when 'SHA1' then Digest::SHA1
97+
when 'SHA2' then Digest::SHA2
98+
when 'SHA256' then Digest::SHA256
99+
when 'SHA384' then Digest::SHA384
100+
when 'SHA512' then Digest::SHA512
101+
when 'RMD160' then Digest::RMD160
102+
else raise Error, "unknown algorithm \"#{$1}\""
103+
end
104+
sess = $2
105+
end
106+
107+
qop = params['qop']
108+
cnonce = make_cnonce if qop or sess
109+
110+
a1 = if sess then
111+
[ algorithm.hexdigest("#{user}:#{params['realm']}:#{password}"),
112+
params['nonce'],
113+
cnonce,
114+
].join ':'
115+
else
116+
"#{user}:#{params['realm']}:#{password}"
117+
end
118+
119+
ha1 = algorithm.hexdigest a1
120+
ha2 = algorithm.hexdigest "#{method}:#{uri.request_uri}"
121+
122+
request_digest = [ha1, params['nonce']]
123+
request_digest.push(('%08x' % nonce_count), cnonce, qop) if qop
124+
request_digest << ha2
125+
request_digest = request_digest.join ':'
126+
127+
header = [
128+
"Digest username=\"#{user}\"",
129+
"realm=\"#{params['realm']}\"",
130+
"algorithm=#{params['algorithm']}",
131+
if qop.nil? then
132+
elsif iis then
133+
"qop=\"#{qop}\""
134+
else
135+
"qop=#{qop}"
136+
end,
137+
"uri=\"#{uri.request_uri}\"",
138+
"nonce=\"#{params['nonce']}\"",
139+
if qop then
140+
[
141+
"nc=#{'%08x' % @nonce_count}",
142+
"cnonce=\"#{cnonce}\"",
143+
]
144+
end,
145+
"response=\"#{algorithm.hexdigest(request_digest)[0, 32]}\"",
146+
if params.key? 'opaque' then
147+
"opaque=\"#{params['opaque']}\""
148+
end
149+
].compact
150+
151+
header.join ', '
152+
end
153+
154+
##
155+
# Creates a client nonce value that is used across all requests based on the
156+
# current time, process id and a random number
157+
158+
def make_cnonce
159+
Digest::MD5.hexdigest [
160+
Time.now.to_i,
161+
$$,
162+
SecureRandom.random_number(2**32),
163+
].join ':'
164+
end
165+
166+
def next_nonce
167+
synchronize do
168+
@nonce_count += 1
169+
end
170+
end
171+
172+
end
173+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'webrick'
2+
require 'tempfile'
3+
4+
class AuthServlet < WEBrick::HTTPServlet::AbstractServlet
5+
6+
@instance = nil
7+
8+
def self.get_instance server, *options
9+
@instance ||= new(server, *options)
10+
end
11+
12+
def initialize server
13+
super server
14+
15+
config = {}
16+
config[:Realm] = 'net-http-digest_auth'
17+
config[:UseOpaque] = false
18+
config[:AutoReloadUserDB] = false
19+
20+
passwd_file = Tempfile.new 'net-http-digest_auth'
21+
passwd_file.close
22+
23+
htpasswd = WEBrick::HTTPAuth::Htpasswd.new passwd_file.path
24+
htpasswd.auth_type = WEBrick::HTTPAuth::DigestAuth
25+
htpasswd.set_passwd config[:Realm], 'username', 'password'
26+
htpasswd.flush
27+
28+
config[:UserDB] = htpasswd
29+
30+
@digest_auth = WEBrick::HTTPAuth::DigestAuth.new config
31+
end
32+
33+
def do_GET req, res
34+
@digest_auth.authenticate req, res
35+
36+
res.body = 'worked!'
37+
end
38+
39+
end
40+
41+
s = WEBrick::HTTPServer.new :Port => 8000
42+
s.mount '/', AuthServlet
43+
44+
trap 'INT' do s.shutdown end
45+
46+
s.start
47+

0 commit comments

Comments
 (0)