Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / test / controller / http_digest_authentication_test.rb
1 require 'abstract_unit'
2
3 class HttpDigestAuthenticationTest < ActionController::TestCase
4 class DummyDigestController < ActionController::Base
5 before_filter :authenticate, :only => :index
6 before_filter :authenticate_with_request, :only => :display
7
8 USERS = { 'lifo' => 'world', 'pretty' => 'please',
9 'dhh' => ::Digest::MD5::hexdigest(["dhh","SuperSecret","secret"].join(":"))}
10
11 def index
12 render :text => "Hello Secret"
13 end
14
15 def display
16 render :text => 'Definitely Maybe'
17 end
18
19 private
20
21 def authenticate
22 authenticate_or_request_with_http_digest("SuperSecret") do |username|
23 # Return the password
24 USERS[username]
25 end
26 end
27
28 def authenticate_with_request
29 if authenticate_with_http_digest("SuperSecret") { |username| USERS[username] }
30 @logged_in = true
31 else
32 request_http_digest_authentication("SuperSecret", "Authentication Failed")
33 end
34 end
35 end
36
37 AUTH_HEADERS = ['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION', 'REDIRECT_X_HTTP_AUTHORIZATION']
38
39 tests DummyDigestController
40
41 AUTH_HEADERS.each do |header|
42 test "successful authentication with #{header.downcase}" do
43 @request.env[header] = encode_credentials(:username => 'lifo', :password => 'world')
44 get :index
45
46 assert_response :success
47 assert_equal 'Hello Secret', @response.body, "Authentication failed for request header #{header}"
48 end
49 end
50
51 AUTH_HEADERS.each do |header|
52 test "unsuccessful authentication with #{header.downcase}" do
53 @request.env[header] = encode_credentials(:username => 'h4x0r', :password => 'world')
54 get :index
55
56 assert_response :unauthorized
57 assert_equal "HTTP Digest: Access denied.\n", @response.body, "Authentication didn't fail for request header #{header}"
58 end
59 end
60
61 test "authentication request without credential" do
62 get :display
63
64 assert_response :unauthorized
65 assert_equal "Authentication Failed", @response.body
66 credentials = decode_credentials(@response.headers['WWW-Authenticate'])
67 assert_equal 'SuperSecret', credentials[:realm]
68 end
69
70 test "authentication request with invalid password" do
71 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'foo')
72 get :display
73
74 assert_response :unauthorized
75 assert_equal "Authentication Failed", @response.body
76 end
77
78 test "authentication request with invalid nonce" do
79 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please', :nonce => "xxyyzz")
80 get :display
81
82 assert_response :unauthorized
83 assert_equal "Authentication Failed", @response.body
84 end
85
86 test "authentication request with invalid opaque" do
87 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'foo', :opaque => "xxyyzz")
88 get :display
89
90 assert_response :unauthorized
91 assert_equal "Authentication Failed", @response.body
92 end
93
94 test "authentication request with invalid realm" do
95 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'foo', :realm => "NotSecret")
96 get :display
97
98 assert_response :unauthorized
99 assert_equal "Authentication Failed", @response.body
100 end
101
102 test "authentication request with valid credential" do
103 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
104 get :display
105
106 assert_response :success
107 assert assigns(:logged_in)
108 assert_equal 'Definitely Maybe', @response.body
109 end
110
111 test "authentication request with valid credential and nil session" do
112 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
113
114 # session_id = "" in functional test, but is +nil+ in real life
115 @request.session.session_id = nil
116 get :display
117
118 assert_response :success
119 assert assigns(:logged_in)
120 assert_equal 'Definitely Maybe', @response.body
121 end
122
123 test "authentication request with request-uri that doesn't match credentials digest-uri" do
124 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please')
125 @request.env['REQUEST_URI'] = "/http_digest_authentication_test/dummy_digest/altered/uri"
126 get :display
127
128 assert_response :unauthorized
129 assert_equal "Authentication Failed", @response.body
130 end
131
132 test "authentication request with absolute uri" do
133 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:uri => "http://test.host/http_digest_authentication_test/dummy_digest/display",
134 :username => 'pretty', :password => 'please')
135 @request.env['REQUEST_URI'] = "http://test.host/http_digest_authentication_test/dummy_digest/display"
136 get :display
137
138 assert_response :success
139 assert assigns(:logged_in)
140 assert_equal 'Definitely Maybe', @response.body
141 end
142
143 test "authentication request with password stored as ha1 digest hash" do
144 @request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'dhh',
145 :password => ::Digest::MD5::hexdigest(["dhh","SuperSecret","secret"].join(":")),
146 :password_is_ha1 => true)
147 get :display
148
149 assert_response :success
150 assert assigns(:logged_in)
151 assert_equal 'Definitely Maybe', @response.body
152 end
153
154 private
155
156 def encode_credentials(options)
157 options.reverse_merge!(:nc => "00000001", :cnonce => "0a4f113b", :password_is_ha1 => false)
158 password = options.delete(:password)
159
160 # Set in /initializers/session_store.rb. Used as secret in generating nonce
161 # to prevent tampering of timestamp
162 ActionController::Base.session_options[:secret] = "session_options_secret"
163
164 # Perform unauthenticated GET to retrieve digest parameters to use on subsequent request
165 get :index
166
167 assert_response :unauthorized
168
169 credentials = decode_credentials(@response.headers['WWW-Authenticate'])
170 credentials.merge!(options)
171 credentials.reverse_merge!(:uri => "#{@request.env['REQUEST_URI']}")
172 ActionController::HttpAuthentication::Digest.encode_credentials("GET", credentials, password, options[:password_is_ha1])
173 end
174
175 def decode_credentials(header)
176 ActionController::HttpAuthentication::Digest.decode_credentials(@response.headers['WWW-Authenticate'])
177 end
178 end