diff --git a/lib/net/http.rb b/lib/net/http.rb index 551ec529..a2e480b1 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -309,6 +309,21 @@ class HTTPHeaderSyntaxError < StandardError; end # } # puts res.body # + # == Bearer Authentication + # + # Bearer authentication is performed according to + # [RFC8898](https://www.ietf.org/rfc/rfc8898.txt). + # + # uri = URI('http://example.com/index.html?key=value') + # + # req = Net::HTTP::Get.new(uri) + # req.bearer_auth('access_token') + # + # res = Net::HTTP.start(uri.hostname, uri.port) {|http| + # http.request(req) + # } + # puts res.body + # # == Streaming Response Bodies # # By default Net::HTTP reads an entire response into memory. If you are diff --git a/lib/net/http/header.rb b/lib/net/http/header.rb index f7aa1033..cf5f9c6d 100644 --- a/lib/net/http/header.rb +++ b/lib/net/http/header.rb @@ -134,9 +134,11 @@ # - #[]=: Sets the string or array value for the given key. # - #add_field: Creates or adds to the array value for the given key. # - #basic_auth: Sets the string authorization header for 'Authorization'. +# - #bearer_auth: Sets the string authorization header for 'Authorization'. # - #content_length=: Sets the integer length for field 'Content-Length. # - #content_type=: Sets the string value for field 'Content-Type'. # - #proxy_basic_auth: Sets the string authorization header for 'Proxy-Authorization'. +# - #proxy_bearer_auth: Sets the string authorization header for 'Proxy-Authorization'. # - #set_range: Sets the value for field 'Range'. # # === Form Setters @@ -871,21 +873,36 @@ def set_form(params, enctype='application/x-www-form-urlencoded', formopt={}) end end - # Set the Authorization: header for "Basic" authorization. + # Sets the Authorization: header for "Basic" authorization. def basic_auth(account, password) @header['authorization'] = [basic_encode(account, password)] end - # Set Proxy-Authorization: header for "Basic" authorization. + # Sets the Authorization: header for "Bearer" authorization. + def bearer_auth(access_token) + @header['authorization'] = [bearer_encode(access_token)] + end + + # Sets the Proxy-Authorization: header for "Basic" authorization. def proxy_basic_auth(account, password) @header['proxy-authorization'] = [basic_encode(account, password)] end + # Sets the Proxy-Authorization: header for "Bearer" authorization. + def proxy_bearer_auth(access_token) + @header['proxy-authorization'] = [bearer_encode(access_token)] + end + def basic_encode(account, password) 'Basic ' + ["#{account}:#{password}"].pack('m0') end private :basic_encode + def bearer_encode(access_token) + "Bearer #{access_token}" + end + private :bearer_encode + def connection_close? token = /(?:\A|,)\s*close\s*(?:\z|,)/i @header['connection']&.grep(token) {return true} diff --git a/test/net/http/test_httpheader.rb b/test/net/http/test_httpheader.rb index 69563168..2ebace85 100644 --- a/test/net/http/test_httpheader.rb +++ b/test/net/http/test_httpheader.rb @@ -9,7 +9,7 @@ class C def initialize initialize_http_header({}) end - attr_accessor :body + attr_accessor :body, :header end def setup @@ -461,9 +461,23 @@ def test_set_form_data end def test_basic_auth + @c.basic_auth("test", "test") + assert_equal(@c.header["authorization"], ["Basic dGVzdDp0ZXN0"]) + end + + def test_bearer_auth + @c.bearer_auth("dGVzdA==") + assert_equal(@c.header["authorization"], ["Bearer dGVzdA=="]) end def test_proxy_basic_auth + @c.proxy_basic_auth("test", "test") + assert_equal(@c.header["proxy-authorization"], ["Basic dGVzdDp0ZXN0"]) + end + + def test_proxy_bearer_auth + @c.proxy_bearer_auth("dGVzdA==") + assert_equal(@c.header["proxy-authorization"], ["Bearer dGVzdA=="]) end end