http_authentication.rb 13.2 KB
Newer Older
J
Jeremy Kemper 已提交
1 2
require 'active_support/base64'

3 4 5
module ActionController
  module HttpAuthentication
    # Makes it dead easy to do HTTP Basic authentication.
6
    #
7
    # Simple Basic example:
8
    #
9 10
    #   class PostsController < ApplicationController
    #     USER_NAME, PASSWORD = "dhh", "secret"
11
    #
12
    #     before_filter :authenticate, :except => [ :index ]
13
    #
14 15 16
    #     def index
    #       render :text => "Everyone can see me!"
    #     end
17
    #
18 19
    #     def edit
    #       render :text => "I'm only accessible if you know the password"
20
    #     end
21
    #
22 23
    #     private
    #       def authenticate
24
    #         authenticate_or_request_with_http_basic do |user_name, password|
25 26 27 28
    #           user_name == USER_NAME && password == PASSWORD
    #         end
    #       end
    #   end
29 30 31
    #
    #
    # Here is a more advanced Basic example where only Atom feeds and the XML API is protected by HTTP authentication,
32
    # the regular HTML interface is protected by a session approach:
33
    #
34 35
    #   class ApplicationController < ActionController::Base
    #     before_filter :set_account, :authenticate
36
    #
37 38 39 40
    #     protected
    #       def set_account
    #         @account = Account.find_by_url_name(request.subdomains.first)
    #       end
41
    #
42 43 44 45 46 47 48 49
    #       def authenticate
    #         case request.format
    #         when Mime::XML, Mime::ATOM
    #           if user = authenticate_with_http_basic { |u, p| @account.users.authenticate(u, p) }
    #             @current_user = user
    #           else
    #             request_http_basic_authentication
    #           end
50
    #         else
51 52 53 54 55
    #           if session_authenticated?
    #             @current_user = @account.users.find(session[:authenticated][:user_id])
    #           else
    #             redirect_to(login_url) and return false
    #           end
56 57
    #         end
    #       end
58
    #   end
59
    #
60
    # In your integration tests, you can do something like this:
61
    #
62 63
    #   def test_access_granted_from_xml
    #     get(
64
    #       "/notes/1.xml", nil,
65 66
    #       :authorization => ActionController::HttpAuthentication::Basic.encode_credentials(users(:dhh).name, users(:dhh).password)
    #     )
67
    #
68 69
    #     assert_equal 200, status
    #   end
70 71 72
    #
    # Simple Digest example:
    #
73
    #   require 'digest/md5'
74
    #   class PostsController < ApplicationController
75 76 77
    #     REALM = "SuperSecret"
    #     USERS = {"dhh" => "secret", #plain text password
    #              "dap" => Digest:MD5::hexdigest(["dap",REALM,"secret"].join(":"))  #ha1 digest password
78 79 80 81 82 83 84 85 86 87 88 89 90
    #
    #     before_filter :authenticate, :except => [:index]
    #
    #     def index
    #       render :text => "Everyone can see me!"
    #     end
    #
    #     def edit
    #       render :text => "I'm only accessible if you know the password"
    #     end
    #
    #     private
    #       def authenticate
91
    #         authenticate_or_request_with_http_digest(REALM) do |username|
92 93 94 95 96
    #           USERS[username]
    #         end
    #       end
    #   end
    #
97 98 99 100 101 102
    # NOTE: The +authenticate_or_request_with_http_digest+ block must return the user's password or the ha1 digest hash so the framework can appropriately
    #       hash to check the user's credentials. Returning +nil+ will cause authentication to fail.
    #       Storing the ha1 hash: MD5(username:realm:password), is better than storing a plain password. If
    #       the password file or database is compromised, the attacker would be able to use the ha1 hash to
    #       authenticate as the user at this +realm+, but would not have the user's password to try using at
    #       other sites.
103
    #
104 105
    # On shared hosts, Apache sometimes doesn't pass authentication headers to
    # FCGI instances. If your environment matches this description and you cannot
106
    # authenticate, try this rule in your Apache setup:
107
    #
108
    #   RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
109 110 111 112 113 114 115 116 117
    module Basic
      extend self

      module ControllerMethods
        def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
          authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
        end

        def authenticate_with_http_basic(&login_procedure)
118
          HttpAuthentication::Basic.authenticate(request, &login_procedure)
119 120 121 122 123 124 125
        end

        def request_http_basic_authentication(realm = "Application")
          HttpAuthentication::Basic.authentication_request(self, realm)
        end
      end

126 127 128
      def authenticate(request, &login_procedure)
        unless authorization(request).blank?
          login_procedure.call(*user_name_and_password(request))
129 130 131 132 133 134
        end
      end

      def user_name_and_password(request)
        decode_credentials(request).split(/:/, 2)
      end
135

136 137 138
      def authorization(request)
        request.env['HTTP_AUTHORIZATION']   ||
        request.env['X-HTTP_AUTHORIZATION'] ||
139 140
        request.env['X_HTTP_AUTHORIZATION'] ||
        request.env['REDIRECT_X_HTTP_AUTHORIZATION']
141
      end
142

143
      def decode_credentials(request)
144
        ActiveSupport::Base64.decode64(authorization(request).split(' ', 2).last || '')
145 146 147
      end

      def encode_credentials(user_name, password)
148
        "Basic #{ActiveSupport::Base64.encode64("#{user_name}:#{password}")}"
149 150 151 152
      end

      def authentication_request(controller, realm)
        controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.gsub(/"/, "")}")
153 154
        controller.response_body = "HTTP Basic: Access denied.\n"
        controller.status = 401
155 156
      end
    end
157 158 159 160 161 162 163 164 165 166 167

    module Digest
      extend self

      module ControllerMethods
        def authenticate_or_request_with_http_digest(realm = "Application", &password_procedure)
          authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm)
        end

        # Authenticate with HTTP Digest, returns true or false
        def authenticate_with_http_digest(realm = "Application", &password_procedure)
168
          HttpAuthentication::Digest.authenticate(config.session_options[:secret], request, realm, &password_procedure)
169 170 171 172 173 174 175 176 177
        end

        # Render output including the HTTP Digest authentication header
        def request_http_digest_authentication(realm = "Application", message = nil)
          HttpAuthentication::Digest.authentication_request(self, realm, message)
        end
      end

      # Returns false on a valid response, true otherwise
178 179
      def authenticate(secret_key, request, realm, &password_procedure)
        authorization(request) && validate_digest_response(secret_key, request, realm, &password_procedure)
180 181 182 183 184 185 186 187 188
      end

      def authorization(request)
        request.env['HTTP_AUTHORIZATION']   ||
        request.env['X-HTTP_AUTHORIZATION'] ||
        request.env['X_HTTP_AUTHORIZATION'] ||
        request.env['REDIRECT_X_HTTP_AUTHORIZATION']
      end

189
      # Returns false unless the request credentials response value matches the expected value.
190 191
      # First try the password as a ha1 digest password. If this fails, then try it as a plain
      # text password.
192
      def validate_digest_response(secret_key, request, realm, &password_procedure)
193
        credentials = decode_credentials_header(request)
194
        valid_nonce = validate_nonce(secret_key, request, credentials[:nonce])
195

196
        if valid_nonce && realm == credentials[:realm] && opaque(secret_key) == credentials[:opaque]
197
          password = password_procedure.call(credentials[:username])
198 199
          return false unless password

200
          method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
201
          uri    = credentials[:uri][0,1] == '/' ? request.fullpath : request.url
202 203

         [true, false].any? do |password_is_ha1|
204
           expected = expected_response(method, uri, credentials, password, password_is_ha1)
205 206
           expected == credentials[:response]
         end
207 208 209 210
        end
      end

      # Returns the expected response for a request of +http_method+ to +uri+ with the decoded +credentials+ and the expected +password+
211 212 213 214
      # Optional parameter +password_is_ha1+ is set to +true+ by default, since best practice is to store ha1 digest instead
      # of a plain-text password.
      def expected_response(http_method, uri, credentials, password, password_is_ha1=true)
        ha1 = password_is_ha1 ? password : ha1(credentials, password)
215 216 217 218
        ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':'))
        ::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':'))
      end

219 220 221 222 223 224
      def ha1(credentials, password)
        ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
      end

      def encode_credentials(http_method, credentials, password, password_is_ha1)
        credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password, password_is_ha1)
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        "Digest " + credentials.sort_by {|x| x[0].to_s }.inject([]) {|a, v| a << "#{v[0]}='#{v[1]}'" }.join(', ')
      end

      def decode_credentials_header(request)
        decode_credentials(authorization(request))
      end

      def decode_credentials(header)
        header.to_s.gsub(/^Digest\s+/,'').split(',').inject({}) do |hash, pair|
          key, value = pair.split('=', 2)
          hash[key.strip.to_sym] = value.to_s.gsub(/^"|"$/,'').gsub(/'/, '')
          hash
        end
      end

      def authentication_header(controller, realm)
241 242 243
        secret_key = controller.config.session_options[:secret]
        nonce = self.nonce(secret_key)
        opaque = opaque(secret_key)
244
        controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
245 246 247 248 249
      end

      def authentication_request(controller, realm, message = nil)
        message ||= "HTTP Digest: Access denied.\n"
        authentication_header(controller, realm)
250 251
        controller.response_body = message
        controller.status = 401
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
      end

      # Uses an MD5 digest based on time to generate a value to be used only once.
      #
      # A server-specified data string which should be uniquely generated each time a 401 response is made.
      # It is recommended that this string be base64 or hexadecimal data.
      # Specifically, since the string is passed in the header lines as a quoted string, the double-quote character is not allowed.
      #
      # The contents of the nonce are implementation dependent.
      # The quality of the implementation depends on a good choice.
      # A nonce might, for example, be constructed as the base 64 encoding of
      #
      # => time-stamp H(time-stamp ":" ETag ":" private-key)
      #
      # where time-stamp is a server-generated time or other non-repeating value,
      # ETag is the value of the HTTP ETag header associated with the requested entity,
      # and private-key is data known only to the server.
      # With a nonce of this form a server would recalculate the hash portion after receiving the client authentication header and
      # reject the request if it did not match the nonce from that header or
      # if the time-stamp value is not recent enough. In this way the server can limit the time of the nonce's validity.
      # The inclusion of the ETag prevents a replay request for an updated version of the resource.
      # (Note: including the IP address of the client in the nonce would appear to offer the server the ability
      # to limit the reuse of the nonce to the same client that originally got it.
      # However, that would break proxy farms, where requests from a single user often go through different proxies in the farm.
      # Also, IP address spoofing is not that hard.)
      #
      # An implementation might choose not to accept a previously used nonce or a previously used digest, in order to
      # protect against a replay attack. Or, an implementation might choose to use one-time nonces or digests for
      # POST or PUT requests and a time-stamp for GET requests. For more details on the issues involved see Section 4
      # of this document.
      #
283 284
      # The nonce is opaque to the client. Composed of Time, and hash of Time with secret
      # key from the Rails session secret generated upon creation of project. Ensures
P
Pratik Naik 已提交
285
      # the time cannot be modified by client.
286
      def nonce(secret_key, time = Time.now)
287
        t = time.to_i
288
        hashed = [t, secret_key]
289
        digest = ::Digest::MD5.hexdigest(hashed.join(":"))
J
Jeremy Kemper 已提交
290
        ActiveSupport::Base64.encode64("#{t}:#{digest}").gsub("\n", '')
291 292
      end

293 294 295 296 297
      # Might want a shorter timeout depending on whether the request
      # is a PUT or POST, and if client is browser or web service.
      # Can be much shorter if the Stale directive is implemented. This would
      # allow a user to use new nonce without prompting user again for their
      # username and password.
298
      def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60)
J
Jeremy Kemper 已提交
299
        t = ActiveSupport::Base64.decode64(value).split(":").first.to_i
300
        nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
301 302
      end

303
      # Opaque based on random generation - but changing each request?
304
      def opaque(secret_key)
305
        ::Digest::MD5.hexdigest(secret_key)
306
      end
307

308
    end
309
  end
310
end