1 #
2 # httpauth.rb -- HTTP access authentication
3 #
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
6 # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
7 # reserved.
8 #
9 # $IPR: httpauth.rb,v 1.14 2003/07/22 19:20:42 gotoyuzo Exp $
10
11 require 'webrick/httpauth/basicauth'
12 require 'webrick/httpauth/digestauth'
13 require 'webrick/httpauth/htpasswd'
14 require 'webrick/httpauth/htdigest'
15 require 'webrick/httpauth/htgroup'
16
17 module WEBrick
18 module HTTPAuth
19 module_function
20
21 def _basic_auth(req, res, realm, req_field, res_field, err_type, block)
22 user = pass = nil
23 if /^Basic\s+(.*)/o =~ req[req_field]
24 userpass = $1
25 user, pass = userpass.unpack("m*")[0].split(":", 2)
26 end
27 if block.call(user, pass)
28 req.user = user
29 return
30 end
31 res[res_field] = "Basic realm=\"#{realm}\""
32 raise err_type
33 end
34
35 def basic_auth(req, res, realm, &block)
36 _basic_auth(req, res, realm, "Authorization", "WWW-Authenticate",
37 HTTPStatus::Unauthorized, block)
38 end
39
40 def proxy_basic_auth(req, res, realm, &block)
41 _basic_auth(req, res, realm, "Proxy-Authorization", "Proxy-Authenticate",
42 HTTPStatus::ProxyAuthenticationRequired, block)
43 end
44 end
45 end