|
| 1 | +module ApiAuth |
| 2 | + module RequestDrivers # :nodoc: |
| 3 | + class TyphoeusRequest # :nodoc: |
| 4 | + include ApiAuth::Helpers |
| 5 | + |
| 6 | + def initialize(request) |
| 7 | + @request = request |
| 8 | + @headers = fetch_headers |
| 9 | + end |
| 10 | + |
| 11 | + def set_auth_header(header) |
| 12 | + headers_hash['Authorization'] = header |
| 13 | + @headers = fetch_headers |
| 14 | + @request |
| 15 | + end |
| 16 | + |
| 17 | + def calculated_hash |
| 18 | + sha256_base64digest(body) |
| 19 | + end |
| 20 | + |
| 21 | + def populate_content_hash |
| 22 | + return unless %w[POST PUT].include?(http_method) |
| 23 | + |
| 24 | + headers_hash['X-Authorization-Content-SHA256'] = calculated_hash |
| 25 | + @headers = fetch_headers |
| 26 | + end |
| 27 | + |
| 28 | + def content_hash_mismatch? |
| 29 | + if %w[POST PUT].include?(http_method) |
| 30 | + calculated_hash != content_hash |
| 31 | + else |
| 32 | + false |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + def fetch_headers |
| 37 | + @headers = capitalize_keys(headers_hash) |
| 38 | + end |
| 39 | + |
| 40 | + def http_method |
| 41 | + method = @request.options[:method] |
| 42 | + method&.to_s&.upcase |
| 43 | + end |
| 44 | + |
| 45 | + def content_type |
| 46 | + find_header(%w[CONTENT-TYPE CONTENT_TYPE HTTP_CONTENT_TYPE]) |
| 47 | + end |
| 48 | + |
| 49 | + def content_hash |
| 50 | + find_header(%w[X-AUTHORIZATION-CONTENT-SHA256]) |
| 51 | + end |
| 52 | + |
| 53 | + def original_uri |
| 54 | + find_header(%w[X-ORIGINAL-URI X_ORIGINAL_URI HTTP_X_ORIGINAL_URI]) |
| 55 | + end |
| 56 | + |
| 57 | + def request_uri |
| 58 | + url = (@request.base_url || '').to_s |
| 59 | + return '/' if url.empty? |
| 60 | + |
| 61 | + uri = URI.parse(url) |
| 62 | + merged_query = merge_query(uri.query, params_query) |
| 63 | + uri.query = merged_query unless merged_query.nil? |
| 64 | + |
| 65 | + path = uri.request_uri |
| 66 | + path.nil? || path.empty? ? '/' : path |
| 67 | + rescue URI::InvalidURIError |
| 68 | + '/' |
| 69 | + end |
| 70 | + |
| 71 | + def set_date |
| 72 | + headers_hash['DATE'] = Time.now.utc.httpdate |
| 73 | + @headers = fetch_headers |
| 74 | + end |
| 75 | + |
| 76 | + def timestamp |
| 77 | + find_header(%w[DATE HTTP_DATE]) |
| 78 | + end |
| 79 | + |
| 80 | + def authorization_header |
| 81 | + find_header %w[Authorization AUTHORIZATION HTTP_AUTHORIZATION] |
| 82 | + end |
| 83 | + |
| 84 | + private |
| 85 | + |
| 86 | + def body |
| 87 | + encoded = @request.respond_to?(:encoded_body) ? @request.encoded_body : nil |
| 88 | + return '' if encoded.nil? |
| 89 | + return encoded unless encoded.empty? |
| 90 | + |
| 91 | + source = @request.options[:body] |
| 92 | + return '' if source.nil? |
| 93 | + |
| 94 | + if source.respond_to?(:read) |
| 95 | + contents = source.read |
| 96 | + source.rewind if source.respond_to?(:rewind) |
| 97 | + contents |
| 98 | + else |
| 99 | + source.to_s |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + def params_query |
| 104 | + params = @request.options[:params] |
| 105 | + return nil if params.nil? |
| 106 | + return params if params.is_a?(String) |
| 107 | + return nil if params.respond_to?(:empty?) && params.empty? |
| 108 | + |
| 109 | + Typhoeus::Pool.with_easy do |easy| |
| 110 | + query = Ethon::Easy::Params.new(easy, params) |
| 111 | + if @request.options.key?(:params_encoding) && query.respond_to?(:params_encoding=) |
| 112 | + query.params_encoding = @request.options[:params_encoding] |
| 113 | + end |
| 114 | + query.escape = true |
| 115 | + query.to_s |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + def merge_query(existing, additional) |
| 120 | + segments = [existing, additional].compact.reject(&:empty?) |
| 121 | + segments.empty? ? nil : segments.join('&') |
| 122 | + end |
| 123 | + |
| 124 | + def headers_hash |
| 125 | + @request.options[:headers] ||= {} |
| 126 | + end |
| 127 | + |
| 128 | + def find_header(keys) |
| 129 | + keys.map { |key| @headers[key] }.compact.first |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | +end |
0 commit comments