Skip to content

Commit c127726

Browse files
committed
Add "Bearer" authentication scheme support
1 parent 4e02f93 commit c127726

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

lib/net/http.rb

+15
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,21 @@ class HTTPHeaderSyntaxError < StandardError; end
309309
# }
310310
# puts res.body
311311
#
312+
# == Bearer Authentication
313+
#
314+
# Bearer authentication is performed according to
315+
# [RFC8898](https://www.ietf.org/rfc/rfc8898.txt).
316+
#
317+
# uri = URI('http://example.com/index.html?key=value')
318+
#
319+
# req = Net::HTTP::Get.new(uri)
320+
# req.bearer_auth('access_token')
321+
#
322+
# res = Net::HTTP.start(uri.hostname, uri.port) {|http|
323+
# http.request(req)
324+
# }
325+
# puts res.body
326+
#
312327
# == Streaming Response Bodies
313328
#
314329
# By default Net::HTTP reads an entire response into memory. If you are

lib/net/http/header.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@
134134
# - #[]=: Sets the string or array value for the given key.
135135
# - #add_field: Creates or adds to the array value for the given key.
136136
# - #basic_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
137+
# - #bearer_auth: Sets the string authorization header for <tt>'Authorization'</tt>.
137138
# - #content_length=: Sets the integer length for field <tt>'Content-Length</tt>.
138139
# - #content_type=: Sets the string value for field <tt>'Content-Type'</tt>.
139140
# - #proxy_basic_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
141+
# - #proxy_bearer_auth: Sets the string authorization header for <tt>'Proxy-Authorization'</tt>.
140142
# - #set_range: Sets the value for field <tt>'Range'</tt>.
141143
#
142144
# === Form Setters
@@ -871,21 +873,36 @@ def set_form(params, enctype='application/x-www-form-urlencoded', formopt={})
871873
end
872874
end
873875

874-
# Set the Authorization: header for "Basic" authorization.
876+
# Sets the Authorization: header for "Basic" authorization.
875877
def basic_auth(account, password)
876878
@header['authorization'] = [basic_encode(account, password)]
877879
end
878880

879-
# Set Proxy-Authorization: header for "Basic" authorization.
881+
# Sets the Authorization: header for "Bearer" authorization.
882+
def bearer_auth(access_token)
883+
@header['authorization'] = [bearer_encode(access_token)]
884+
end
885+
886+
# Sets the Proxy-Authorization: header for "Basic" authorization.
880887
def proxy_basic_auth(account, password)
881888
@header['proxy-authorization'] = [basic_encode(account, password)]
882889
end
883890

891+
# Sets the Proxy-Authorization: header for "Bearer" authorization.
892+
def proxy_bearer_auth(access_token)
893+
@header['proxy-authorization'] = [bearer_encode(access_token)]
894+
end
895+
884896
def basic_encode(account, password)
885897
'Basic ' + ["#{account}:#{password}"].pack('m0')
886898
end
887899
private :basic_encode
888900

901+
def bearer_encode(access_token)
902+
"Bearer #{access_token}"
903+
end
904+
private :bearer_encode
905+
889906
def connection_close?
890907
token = /(?:\A|,)\s*close\s*(?:\z|,)/i
891908
@header['connection']&.grep(token) {return true}

test/net/http/test_httpheader.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class C
99
def initialize
1010
initialize_http_header({})
1111
end
12-
attr_accessor :body
12+
attr_accessor :body, :header
1313
end
1414

1515
def setup
@@ -461,9 +461,23 @@ def test_set_form_data
461461
end
462462

463463
def test_basic_auth
464+
@c.basic_auth("test", "test")
465+
assert_equal(@c.header["authorization"], ["Basic dGVzdDp0ZXN0"])
466+
end
467+
468+
def test_bearer_auth
469+
@c.bearer_auth("dGVzdA==")
470+
assert_equal(@c.header["authorization"], ["Bearer dGVzdA=="])
464471
end
465472

466473
def test_proxy_basic_auth
474+
@c.proxy_basic_auth("test", "test")
475+
assert_equal(@c.header["proxy-authorization"], ["Basic dGVzdDp0ZXN0"])
476+
end
477+
478+
def test_proxy_bearer_auth
479+
@c.proxy_bearer_auth("dGVzdA==")
480+
assert_equal(@c.header["proxy-authorization"], ["Bearer dGVzdA=="])
467481
end
468482

469483
end

0 commit comments

Comments
 (0)