|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "digest" |
| 4 | +require "securerandom" |
| 5 | + |
| 6 | +module HTTP |
| 7 | + module Features |
| 8 | + # Implements HTTP Digest Authentication (RFC 2617 / RFC 7616) |
| 9 | + # |
| 10 | + # When a server responds with 401 and a Digest challenge, this feature |
| 11 | + # automatically computes the digest response and retries the request |
| 12 | + # with the correct Authorization header. |
| 13 | + class DigestAuth < Feature |
| 14 | + # Supported hash algorithms |
| 15 | + ALGORITHMS = { |
| 16 | + "MD5" => Digest::MD5, |
| 17 | + "SHA-256" => Digest::SHA256, |
| 18 | + "MD5-sess" => Digest::MD5, |
| 19 | + "SHA-256-sess" => Digest::SHA256 |
| 20 | + }.freeze |
| 21 | + |
| 22 | + # @api private |
| 23 | + WWW_AUTHENTICATE = "WWW-Authenticate" |
| 24 | + |
| 25 | + # Initialize the DigestAuth feature |
| 26 | + # |
| 27 | + # @example |
| 28 | + # DigestAuth.new(user: "admin", pass: "secret") |
| 29 | + # |
| 30 | + # @param user [String] username for authentication |
| 31 | + # @param pass [String] password for authentication |
| 32 | + # @return [DigestAuth] |
| 33 | + # @api public |
| 34 | + def initialize(user:, pass:) |
| 35 | + @user = user |
| 36 | + @pass = pass |
| 37 | + end |
| 38 | + |
| 39 | + # Wraps the HTTP exchange to handle digest authentication challenges |
| 40 | + # |
| 41 | + # On a 401 with a Digest WWW-Authenticate header, flushes the error |
| 42 | + # response, computes digest credentials, and retries the request. |
| 43 | + # |
| 44 | + # @example |
| 45 | + # feature.around_request(request) { |req| perform(req) } |
| 46 | + # |
| 47 | + # @param request [HTTP::Request] |
| 48 | + # @yield [HTTP::Request] the request to perform |
| 49 | + # @yieldreturn [HTTP::Response] |
| 50 | + # @return [HTTP::Response] |
| 51 | + # @api public |
| 52 | + def around_request(request) |
| 53 | + response = yield request |
| 54 | + return response unless digest_challenge?(response) |
| 55 | + |
| 56 | + response.flush |
| 57 | + yield authorize(request, response) |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + # Check if the response contains a digest authentication challenge |
| 63 | + # |
| 64 | + # @param response [HTTP::Response] |
| 65 | + # @return [Boolean] |
| 66 | + # @api private |
| 67 | + def digest_challenge?(response) |
| 68 | + www_auth = response.headers[WWW_AUTHENTICATE] #: String? |
| 69 | + response.status.code == 401 && www_auth&.start_with?("Digest ") == true |
| 70 | + end |
| 71 | + |
| 72 | + # Build an authorized copy of the request using the digest challenge |
| 73 | + # |
| 74 | + # @param request [HTTP::Request] the original request |
| 75 | + # @param response [HTTP::Response] the 401 response with challenge |
| 76 | + # @return [HTTP::Request] a new request with Authorization header |
| 77 | + # @api private |
| 78 | + def authorize(request, response) |
| 79 | + www_auth = response.headers[WWW_AUTHENTICATE] #: String |
| 80 | + challenge = parse_challenge(www_auth) |
| 81 | + headers = request.headers.dup |
| 82 | + headers.set Headers::AUTHORIZATION, build_auth(request, challenge) |
| 83 | + |
| 84 | + Request.new( |
| 85 | + verb: request.verb, |
| 86 | + uri: request.uri, |
| 87 | + headers: headers, |
| 88 | + proxy: request.proxy, |
| 89 | + body: request.body.source, |
| 90 | + version: request.version, |
| 91 | + uri_normalizer: request.uri_normalizer |
| 92 | + ) |
| 93 | + end |
| 94 | + |
| 95 | + # Parse the WWW-Authenticate header into a parameter hash |
| 96 | + # |
| 97 | + # @param header [String] the WWW-Authenticate header value |
| 98 | + # @return [Hash{String => String}] parsed challenge parameters |
| 99 | + # @api private |
| 100 | + def parse_challenge(header) |
| 101 | + params = {} #: Hash[String, String] |
| 102 | + header.sub(/^Digest\s+/i, "").scan(/(\w+)=(?:"([^"]*)"|([\w-]+))/) do |match| |
| 103 | + key = match[0] #: String |
| 104 | + params[key] = format("%s", match[1] || match[2]) |
| 105 | + end |
| 106 | + params |
| 107 | + end |
| 108 | + |
| 109 | + # Build the Authorization header value |
| 110 | + # |
| 111 | + # @param request [HTTP::Request] the request being authorized |
| 112 | + # @param challenge [Hash{String => String}] parsed challenge params |
| 113 | + # @return [String] the Digest authorization header value |
| 114 | + # @api private |
| 115 | + def build_auth(request, challenge) |
| 116 | + algorithm = challenge.fetch("algorithm", "MD5") |
| 117 | + qop = select_qop(challenge["qop"]) |
| 118 | + nonce = challenge.fetch("nonce") |
| 119 | + cnonce = SecureRandom.hex(16) |
| 120 | + nonce_count = "00000001" |
| 121 | + uri = request.uri.request_uri.to_s |
| 122 | + ha1 = compute_ha1(algorithm, challenge.fetch("realm"), nonce, cnonce) |
| 123 | + ha2 = compute_ha2(algorithm, request.verb.to_s.upcase, uri) |
| 124 | + |
| 125 | + compute_auth_header(algorithm, qop, nonce, cnonce, nonce_count, uri, ha1, ha2, challenge) |
| 126 | + end |
| 127 | + |
| 128 | + # Compute digest and build the Authorization header string |
| 129 | + # |
| 130 | + # @return [String] formatted authorization header |
| 131 | + # @api private |
| 132 | + def compute_auth_header(algorithm, qop, nonce, cnonce, nonce_count, uri, ha1, ha2, challenge) # rubocop:disable Metrics/ParameterLists |
| 133 | + response = compute_response(algorithm, ha1, ha2, nonce: nonce, |
| 134 | + nonce_count: nonce_count, cnonce: cnonce, qop: qop) |
| 135 | + |
| 136 | + build_header(username: @user, realm: challenge.fetch("realm"), nonce: nonce, uri: uri, |
| 137 | + qop: qop, nonce_count: nonce_count, cnonce: cnonce, response: response, |
| 138 | + opaque: challenge["opaque"], algorithm: algorithm) |
| 139 | + end |
| 140 | + |
| 141 | + # Select the best qop value from the challenge |
| 142 | + # |
| 143 | + # @param qop_str [String, nil] comma-separated qop options |
| 144 | + # @return [String, nil] selected qop value |
| 145 | + # @api private |
| 146 | + def select_qop(qop_str) |
| 147 | + return unless qop_str |
| 148 | + |
| 149 | + qops = qop_str.split(",").map(&:strip) |
| 150 | + return "auth" if qops.include?("auth") |
| 151 | + |
| 152 | + qops.first |
| 153 | + end |
| 154 | + |
| 155 | + # Compute HA1 per RFC 2617 |
| 156 | + # |
| 157 | + # @return [String] hex digest |
| 158 | + # @api private |
| 159 | + def compute_ha1(algorithm, realm, nonce, cnonce) |
| 160 | + base = hex_digest(algorithm, "#{@user}:#{realm}:#{@pass}") |
| 161 | + |
| 162 | + if algorithm.end_with?("-sess") |
| 163 | + hex_digest(algorithm, "#{base}:#{nonce}:#{cnonce}") |
| 164 | + else |
| 165 | + base |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + # Compute HA2 per RFC 2617 |
| 170 | + # |
| 171 | + # @return [String] hex digest |
| 172 | + # @api private |
| 173 | + def compute_ha2(algorithm, method, uri) |
| 174 | + hex_digest(algorithm, "#{method}:#{uri}") |
| 175 | + end |
| 176 | + |
| 177 | + # Compute the final digest response value |
| 178 | + # |
| 179 | + # @param algorithm [String] algorithm name |
| 180 | + # @param ha1 [String] HA1 hex digest |
| 181 | + # @param ha2 [String] HA2 hex digest |
| 182 | + # @param nonce [String] server nonce |
| 183 | + # @param nonce_count [String] request counter |
| 184 | + # @param cnonce [String] client nonce |
| 185 | + # @param qop [String, nil] quality of protection |
| 186 | + # @return [String] hex digest |
| 187 | + # @api private |
| 188 | + def compute_response(algorithm, ha1, ha2, nonce:, nonce_count:, cnonce:, qop:) |
| 189 | + if qop |
| 190 | + hex_digest(algorithm, "#{ha1}:#{nonce}:#{nonce_count}:#{cnonce}:#{qop}:#{ha2}") |
| 191 | + else |
| 192 | + hex_digest(algorithm, "#{ha1}:#{nonce}:#{ha2}") |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + # Compute a hex digest using the specified algorithm |
| 197 | + # |
| 198 | + # @param algorithm [String] algorithm name |
| 199 | + # @param data [String] data to digest |
| 200 | + # @return [String] hex digest |
| 201 | + # @api private |
| 202 | + def hex_digest(algorithm, data) |
| 203 | + ALGORITHMS.fetch(algorithm.sub(/-sess$/i, "")).hexdigest(data) |
| 204 | + end |
| 205 | + |
| 206 | + # Build the Digest Authorization header string |
| 207 | + # |
| 208 | + # @return [String] formatted header value |
| 209 | + # @api private |
| 210 | + def build_header(username:, realm:, nonce:, uri:, qop:, nonce_count:, cnonce:, |
| 211 | + response:, opaque:, algorithm:) |
| 212 | + parts = [ |
| 213 | + %(username="#{username}"), |
| 214 | + %(realm="#{realm}"), |
| 215 | + %(nonce="#{nonce}"), |
| 216 | + %(uri="#{uri}") |
| 217 | + ] |
| 218 | + |
| 219 | + parts.push(%(qop=#{qop}), %(nc=#{nonce_count}), %(cnonce="#{cnonce}")) if qop |
| 220 | + |
| 221 | + parts << %(response="#{response}") |
| 222 | + parts << %(opaque="#{opaque}") if opaque |
| 223 | + parts << %(algorithm=#{algorithm}) |
| 224 | + |
| 225 | + "Digest #{parts.join(', ')}" |
| 226 | + end |
| 227 | + |
| 228 | + HTTP::Options.register_feature(:digest_auth, self) |
| 229 | + end |
| 230 | + end |
| 231 | +end |
0 commit comments