http_authentication.rb 20.1 KB
Newer Older
S
Sergey Nartimov 已提交
1
require 'base64'
J
Jeremy Kemper 已提交
2

3
module ActionController
4
  # Makes it dead easy to do HTTP Basic, Digest and Token authentication.
5
  module HttpAuthentication
6
    # Makes it dead easy to do HTTP \Basic authentication.
7
    #
8
    # === Simple \Basic example
9
    #
10
    #   class PostsController < ApplicationController
A
AvnerCohen 已提交
11
    #     http_basic_authenticate_with name: "dhh", password: "secret", except: :index
12
    #
13
    #     def index
14
    #       render plain: "Everyone can see me!"
15
    #     end
16
    #
17
    #     def edit
18
    #       render plain: "I'm only accessible if you know the password"
19
    #     end
20
    #  end
21
    #
22
    # === Advanced \Basic example
23
    #
24
    # Here is a more advanced \Basic example where only Atom feeds and the XML API is protected by HTTP authentication,
25
    # the regular HTML interface is protected by a session approach:
26
    #
27
    #   class ApplicationController < ActionController::Base
28
    #     before_action :set_account, :authenticate
29
    #
30 31
    #     protected
    #       def set_account
32
    #         @account = Account.find_by(url_name: request.subdomains.first)
33
    #       end
34
    #
35 36 37 38 39 40 41 42
    #       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
43
    #         else
44 45 46 47 48
    #           if session_authenticated?
    #             @current_user = @account.users.find(session[:authenticated][:user_id])
    #           else
    #             redirect_to(login_url) and return false
    #           end
49 50
    #         end
    #       end
51
    #   end
52
    #
53
    # In your integration tests, you can do something like this:
54
    #
55
    #   def test_access_granted_from_xml
R
Robin Dupret 已提交
56
    #     @request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(users(:dhh).name, users(:dhh).password)
B
Ben Prew 已提交
57
    #     get "/notes/1.xml"
58
    #
59 60 61 62 63 64
    #     assert_equal 200, status
    #   end
    module Basic
      extend self

      module ControllerMethods
65
        extend ActiveSupport::Concern
66

67 68
        module ClassMethods
          def http_basic_authenticate_with(options = {})
69
            before_action(options.except(:name, :password, :realm)) do
70 71
              authenticate_or_request_with_http_basic(options[:realm] || "Application") do |name, password|
                name == options[:name] && password == options[:password]
72 73 74 75
              end
            end
          end
        end
76

77 78
        def authenticate_or_request_with_http_basic(realm = "Application", message = nil, &login_procedure)
          authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm, message)
79 80 81
        end

        def authenticate_with_http_basic(&login_procedure)
82
          HttpAuthentication::Basic.authenticate(request, &login_procedure)
83 84
        end

85 86
        def request_http_basic_authentication(realm = "Application", message = nil)
          HttpAuthentication::Basic.authentication_request(self, realm, message)
87 88 89
        end
      end

90
      def authenticate(request, &login_procedure)
91
        if has_basic_credentials?(request)
92
          login_procedure.call(*user_name_and_password(request))
93 94 95
        end
      end

96
      def has_basic_credentials?(request)
97
        request.authorization.present? && (auth_scheme(request).downcase == 'basic')
98 99
      end

100
      def user_name_and_password(request)
101
        decode_credentials(request).split(':', 2)
102
      end
103

104
      def decode_credentials(request)
105 106 107 108
        ::Base64.decode64(auth_param(request) || '')
      end

      def auth_scheme(request)
109
        request.authorization.to_s.split(' ', 2).first
110 111 112
      end

      def auth_param(request)
113
        request.authorization.to_s.split(' ', 2).second
114 115 116
      end

      def encode_credentials(user_name, password)
117
        "Basic #{::Base64.strict_encode64("#{user_name}:#{password}")}"
118 119
      end

120 121
      def authentication_request(controller, realm, message)
        message ||= "HTTP Basic: Access denied.\n"
122
        controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.tr('"'.freeze, "".freeze)}")
123
        controller.status = 401
124
        controller.response_body = message
125 126
      end
    end
127

128 129 130 131 132 133 134 135 136 137
    # Makes it dead easy to do HTTP \Digest authentication.
    #
    # === Simple \Digest example
    #
    #   require 'digest/md5'
    #   class PostsController < ApplicationController
    #     REALM = "SuperSecret"
    #     USERS = {"dhh" => "secret", #plain text password
    #              "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":"))}  #ha1 digest password
    #
138
    #     before_action :authenticate, except: [:index]
139 140
    #
    #     def index
141
    #       render plain: "Everyone can see me!"
142 143 144
    #     end
    #
    #     def edit
145
    #       render plain: "I'm only accessible if you know the password"
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    #     end
    #
    #     private
    #       def authenticate
    #         authenticate_or_request_with_http_digest(REALM) do |username|
    #           USERS[username]
    #         end
    #       end
    #   end
    #
    # === Notes
    #
    # 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.
    #
    # In rare instances, web servers or front proxies strip authorization headers before
    # they reach your application. You can debug this situation by logging all environment
    # variables, and check for HTTP_AUTHORIZATION, amongst others.
170 171 172 173
    module Digest
      extend self

      module ControllerMethods
174 175
        def authenticate_or_request_with_http_digest(realm = "Application", message = nil, &password_procedure)
          authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm, message)
176 177 178 179
        end

        # Authenticate with HTTP Digest, returns true or false
        def authenticate_with_http_digest(realm = "Application", &password_procedure)
180
          HttpAuthentication::Digest.authenticate(request, realm, &password_procedure)
181 182 183 184 185 186 187 188 189
        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
190 191
      def authenticate(request, realm, &password_procedure)
        request.authorization && validate_digest_response(request, realm, &password_procedure)
192 193
      end

194
      # Returns false unless the request credentials response value matches the expected value.
195 196
      # First try the password as a ha1 digest password. If this fails, then try it as a plain
      # text password.
197 198
      def validate_digest_response(request, realm, &password_procedure)
        secret_key  = secret_token(request)
199
        credentials = decode_credentials_header(request)
200
        valid_nonce = validate_nonce(secret_key, request, credentials[:nonce])
201

202
        if valid_nonce && realm == credentials[:realm] && opaque(secret_key) == credentials[:opaque]
203
          password = password_procedure.call(credentials[:username])
204 205
          return false unless password

206
          method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
207
          uri    = credentials[:uri]
208

209 210 211 212 213 214 215
          [true, false].any? do |trailing_question_mark|
            [true, false].any? do |password_is_ha1|
              _uri = trailing_question_mark ? uri + "?" : uri
              expected = expected_response(method, _uri, credentials, password, password_is_ha1)
              expected == credentials[:response]
            end
          end
216 217 218 219
        end
      end

      # Returns the expected response for a request of +http_method+ to +uri+ with the decoded +credentials+ and the expected +password+
220 221 222 223
      # 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)
224 225 226 227
        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

228 229 230 231 232 233
      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)
A
Aaron Patterson 已提交
234
        "Digest " + credentials.sort_by {|x| x[0].to_s }.map {|v| "#{v[0]}='#{v[1]}'" }.join(', ')
235 236 237
      end

      def decode_credentials_header(request)
J
José Valim 已提交
238
        decode_credentials(request.authorization)
239 240 241
      end

      def decode_credentials(header)
242
        ActiveSupport::HashWithIndifferentAccess[header.to_s.gsub(/^Digest\s+/, '').split(',').map do |pair|
243
          key, value = pair.split('=', 2)
244
          [key.strip, value.to_s.gsub(/^"|"$/,'').delete('\'')]
245
        end]
246 247 248
      end

      def authentication_header(controller, realm)
249
        secret_key = secret_token(controller.request)
250 251
        nonce = self.nonce(secret_key)
        opaque = opaque(secret_key)
252
        controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
253 254 255 256 257
      end

      def authentication_request(controller, realm, message = nil)
        message ||= "HTTP Digest: Access denied.\n"
        authentication_header(controller, realm)
258
        controller.status = 401
259
        controller.response_body = message
260 261
      end

262
      def secret_token(request)
263 264 265
        key_generator  = request.env["action_dispatch.key_generator"]
        http_auth_salt = request.env["action_dispatch.http_auth_salt"]
        key_generator.generate_key(http_auth_salt)
266 267
      end

268 269 270 271 272 273 274 275 276 277
      # 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
      #
V
Vijay Dev 已提交
278
      #   time-stamp H(time-stamp ":" ETag ":" private-key)
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
      #
      # 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
294
      # POST, PUT, or PATCH requests and a time-stamp for GET requests. For more details on the issues involved see Section 4
295 296
      # of this document.
      #
297 298
      # 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 已提交
299
      # the time cannot be modified by client.
300
      def nonce(secret_key, time = Time.now)
301
        t = time.to_i
302
        hashed = [t, secret_key]
303
        digest = ::Digest::MD5.hexdigest(hashed.join(":"))
304
        ::Base64.strict_encode64("#{t}:#{digest}")
305 306
      end

307
      # Might want a shorter timeout depending on whether the request
308
      # is a PATCH, PUT, or POST, and if client is browser or web service.
309 310 311
      # 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.
312
      def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60)
313
        return false if value.nil?
314
        t = ::Base64.decode64(value).split(":").first.to_i
315
        nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
316 317
      end

S
Sushruth Sivaramakrishnan 已提交
318
      # Opaque based on digest of secret key
319
      def opaque(secret_key)
320
        ::Digest::MD5.hexdigest(secret_key)
321
      end
322

323
    end
324 325 326 327 328 329 330 331

    # Makes it dead easy to do HTTP Token authentication.
    #
    # Simple Token example:
    #
    #   class PostsController < ApplicationController
    #     TOKEN = "secret"
    #
332
    #     before_action :authenticate, except: [ :index ]
333 334
    #
    #     def index
335
    #       render plain: "Everyone can see me!"
336 337 338
    #     end
    #
    #     def edit
339
    #       render plain: "I'm only accessible if you know the password"
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    #     end
    #
    #     private
    #       def authenticate
    #         authenticate_or_request_with_http_token do |token, options|
    #           token == TOKEN
    #         end
    #       end
    #   end
    #
    #
    # Here is a more advanced Token example where only Atom feeds and the XML API is protected by HTTP token authentication,
    # the regular HTML interface is protected by a session approach:
    #
    #   class ApplicationController < ActionController::Base
355
    #     before_action :set_account, :authenticate
356 357 358
    #
    #     protected
    #       def set_account
359
    #         @account = Account.find_by(url_name: request.subdomains.first)
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    #       end
    #
    #       def authenticate
    #         case request.format
    #         when Mime::XML, Mime::ATOM
    #           if user = authenticate_with_http_token { |t, o| @account.users.authenticate(t, o) }
    #             @current_user = user
    #           else
    #             request_http_token_authentication
    #           end
    #         else
    #           if session_authenticated?
    #             @current_user = @account.users.find(session[:authenticated][:user_id])
    #           else
    #             redirect_to(login_url) and return false
    #           end
    #         end
    #       end
    #   end
    #
    #
    # In your integration tests, you can do something like this:
    #
    #   def test_access_granted_from_xml
    #     get(
    #       "/notes/1.xml", nil,
386
    #       'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Token.encode_credentials(users(:dhh).token)
387 388 389 390 391 392 393 394 395 396 397 398
    #     )
    #
    #     assert_equal 200, status
    #   end
    #
    #
    # On shared hosts, Apache sometimes doesn't pass authentication headers to
    # FCGI instances. If your environment matches this description and you cannot
    # authenticate, try this rule in your Apache setup:
    #
    #   RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
    module Token
399
      TOKEN_KEY = 'token='
P
phoet 已提交
400
      TOKEN_REGEX = /^(Token|Bearer) /
401
      AUTHN_PAIR_DELIMITERS = /(?:,|;|\t+)/
402 403 404
      extend self

      module ControllerMethods
405 406
        def authenticate_or_request_with_http_token(realm = "Application", message = nil, &login_procedure)
          authenticate_with_http_token(&login_procedure) || request_http_token_authentication(realm, message)
407 408 409 410 411 412
        end

        def authenticate_with_http_token(&login_procedure)
          Token.authenticate(self, &login_procedure)
        end

413 414
        def request_http_token_authentication(realm = "Application", message = nil)
          Token.authentication_request(self, realm, message)
415 416 417
        end
      end

418 419
      # If token Authorization header is present, call the login
      # procedure with the present token and options.
420
      #
421 422
      # [controller]
      #   ActionController::Base instance for the current request.
423
      #
424
      # [login_procedure]
V
Vijay Dev 已提交
425
      #   Proc to call if a token is present. The Proc should take two arguments:
426 427 428 429 430 431
      #
      #     authenticate(controller) { |token, options| ... }
      #
      # Returns the return value of <tt>login_procedure</tt> if a
      # token is found. Returns <tt>nil</tt> if no token is found.

432 433
      def authenticate(controller, &login_procedure)
        token, options = token_and_options(controller.request)
N
Neeraj Singh 已提交
434
        unless token.blank?
435 436 437 438
          login_procedure.call(token, options)
        end
      end

439
      # Parses the token and options out of the token authorization header. If
440 441
      # the header looks like this:
      #   Authorization: Token token="abc", nonce="def"
A
AvnerCohen 已提交
442
      # Then the returned token is "abc", and the options is {nonce: "def"}
443
      #
444
      # request - ActionDispatch::Request instance with the current headers.
445 446 447 448
      #
      # Returns an Array of [String, Hash] if a token is present.
      # Returns nil if no token is found.
      def token_and_options(request)
449 450 451
        authorization_request = request.authorization.to_s
        if authorization_request[TOKEN_REGEX]
          params = token_params_from authorization_request
452
          [params.shift[1], Hash[params].with_indifferent_access]
453 454 455
        end
      end

456 457 458 459 460 461 462 463 464
      def token_params_from(auth)
        rewrite_param_values params_array_from raw_params auth
      end

      # Takes raw_params and turns it into an array of parameters
      def params_array_from(raw_params)
        raw_params.map { |param| param.split %r/=(.+)?/ }
      end

465
      # This removes the <tt>"</tt> characters wrapping the value.
466
      def rewrite_param_values(array_params)
467
        array_params.each { |param| (param[1] || "").gsub! %r/^"|"$/, '' }
468 469 470
      end

      # This method takes an authorization body and splits up the key-value
471 472
      # pairs by the standardized <tt>:</tt>, <tt>;</tt>, or <tt>\t</tt>
      # delimiters defined in +AUTHN_PAIR_DELIMITERS+.
473
      def raw_params(auth)
474 475 476 477 478 479 480
        _raw_params = auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)

        if !(_raw_params.first =~ %r{\A#{TOKEN_KEY}})
          _raw_params[0] = "#{TOKEN_KEY}#{_raw_params.first}"
        end

        _raw_params
481 482
      end

483 484 485 486 487 488 489
      # Encodes the given token and options into an Authorization header value.
      #
      # token   - String token.
      # options - optional Hash of the options.
      #
      # Returns String.
      def encode_credentials(token, options = {})
490
        values = ["#{TOKEN_KEY}#{token.to_s.inspect}"] + options.map do |key, value|
A
Aaron Patterson 已提交
491
          "#{key}=#{value.to_s.inspect}"
492 493 494 495
        end
        "Token #{values * ", "}"
      end

496
      # Sets a WWW-Authenticate header to let the client know a token is desired.
497 498 499 500 501
      #
      # controller - ActionController::Base instance for the outgoing response.
      # realm      - String realm to use in the header.
      #
      # Returns nothing.
502 503
      def authentication_request(controller, realm, message = nil)
        message ||= "HTTP Token: Access denied.\n"
504
        controller.headers["WWW-Authenticate"] = %(Token realm="#{realm.tr('"'.freeze, "".freeze)}")
505
        controller.__send__ :render, plain: message, status: :unauthorized
506 507
      end
    end
508
  end
509
end