renaming authenticationn modules to just auth

* `:authentication` plugin becomes `:auth`
  * `authentication` helper becomes `authorization`
* `:basic_authentication` plugin becomes `:basic_auth`
  * `:basic_authentication` helper is removed
* `:digest_authentication` plugin becomes `:digest_auth`
  * `:digest_authentication` helper is removed
* `:ntlm_authentication` plugin becomes `:ntlm_auth`
  * `:ntlm_authentication` helper is removed
This commit is contained in:
HoneyryderChuck 2023-07-02 22:49:01 +01:00
parent a9cb0a69a2
commit 4f587c5508
29 changed files with 104 additions and 92 deletions

View File

@ -56,13 +56,13 @@ HTTPX.delete("https://myapi.com/users/1")
require "httpx"
# Basic Auth
response = HTTPX.plugin(:basic_authentication).basic_authentication("username", "password").get("https://google.com")
response = HTTPX.plugin(:basic_auth).basic_auth("username", "password").get("https://google.com")
# Digest Auth
response = HTTPX.plugin(:digest_authentication).digest_authentication("username", "password").get("https://google.com")
response = HTTPX.plugin(:digest_auth).digest_auth("username", "password").get("https://google.com")
# Bearer Token Auth
response = HTTPX.plugin(:authentication).authentication("eyrandomtoken").get("https://google.com")
response = HTTPX.plugin(:auth).authorization("eyrandomtoken").get("https://google.com")
```

View File

@ -19,7 +19,7 @@ And also:
* Compression (gzip, deflate, brotli)
* Streaming Requests
* Authentication (Basic Auth, Digest Auth, NTLM)
* Auth (Basic Auth, Digest Auth, NTLM)
* Expect 100-continue
* Multipart Requests
* Advanced Cookie handling

View File

@ -8,6 +8,18 @@
* `:read_timeout` and `:write_timeout` are now set to 60 seconds by default, and preferred over `:operation_timeout`;
* the exception being in the `:stream` plugin, as the response is theoretically endless (so `:read_timeout` is unset).
### plugins
* `:authentication` plugin becomes `:auth`
* `.authentication` helper becomes `.authorization`
* `:basic_authentication` plugin becomes `:basic_auth`
* `:basic_authentication` helper is removed
* `:digest_authentication` plugin becomes `:digest_auth`
* `:digest_authentication` helper is removed
* `:ntlm_authentication` plugin becomes `:ntlm_auth`
* `:ntlm_authentication` helper is removed
* OAuth plugin: `:oauth_authentication` helper is rename to `:oauth_auth`
### Support removed for deprecated APIs
* The deprecated `HTTPX::Client` constant lookup has been removed (use `HTTPX::Session` instead).

25
lib/httpx/plugins/auth.rb Normal file
View File

@ -0,0 +1,25 @@
# frozen_string_literal: true
module HTTPX
module Plugins
#
# This plugin adds a shim +authorization+ method to the session, which will fill
# the HTTP Authorization header, and another, +bearer_auth+, which fill the "Bearer " prefix
# in its value.
#
# https://gitlab.com/os85/httpx/wikis/Auth#authorization
#
module Auth
module InstanceMethods
def authorization(token)
with(headers: { "authorization" => token })
end
def bearer_auth(token)
authorization("Bearer #{token}")
end
end
end
register_plugin :auth, Auth
end
end

View File

@ -1,24 +0,0 @@
# frozen_string_literal: true
module HTTPX
module Plugins
#
# This plugin adds a shim +authentication+ method to the session, which will fill
# the HTTP Authorization header.
#
# https://gitlab.com/os85/httpx/wikis/Authentication#authentication
#
module Authentication
module InstanceMethods
def authentication(token)
with(headers: { "authorization" => token })
end
def bearer_auth(token)
authentication("Bearer #{token}")
end
end
end
register_plugin :authentication, Authentication
end
end

View File

@ -5,26 +5,25 @@ module HTTPX
#
# This plugin adds helper methods to implement HTTP Basic Auth (https://tools.ietf.org/html/rfc7617)
#
# https://gitlab.com/os85/httpx/wikis/Authentication#basic-authentication
# https://gitlab.com/os85/httpx/wikis/Authorization#basic-auth
#
module BasicAuth
class << self
def load_dependencies(_klass)
require_relative "authentication/basic"
require_relative "auth/basic"
end
def configure(klass)
klass.plugin(:authentication)
klass.plugin(:auth)
end
end
module InstanceMethods
def basic_auth(user, password)
authentication(Authentication::Basic.new(user, password).authenticate)
authorization(Authentication::Basic.new(user, password).authenticate)
end
alias_method :basic_authentication, :basic_auth
end
end
register_plugin :basic_authentication, BasicAuth
register_plugin :basic_auth, BasicAuth
end
end

View File

@ -5,7 +5,7 @@ module HTTPX
#
# This plugin adds helper methods to implement HTTP Digest Auth (https://tools.ietf.org/html/rfc7616)
#
# https://gitlab.com/os85/httpx/wikis/Authentication#authentication
# https://gitlab.com/os85/httpx/wikis/Authorization#digest-auth
#
module DigestAuth
DigestError = Class.new(Error)
@ -16,7 +16,7 @@ module HTTPX
end
def load_dependencies(*)
require_relative "authentication/digest"
require_relative "auth/digest"
end
end
@ -29,11 +29,11 @@ module HTTPX
end
module InstanceMethods
def digest_authentication(user, password, hashed: false)
def digest_auth(user, password, hashed: false)
with(digest: Authentication::Digest.new(user, password, hashed: hashed))
end
alias_method :digest_auth, :digest_authentication
private
def send_requests(*requests)
requests.flat_map do |request|
@ -57,6 +57,6 @@ module HTTPX
end
end
register_plugin :digest_authentication, DigestAuth
register_plugin :digest_auth, DigestAuth
end
end

View File

@ -3,12 +3,12 @@
module HTTPX
module Plugins
#
# https://gitlab.com/os85/httpx/wikis/Authentication#ntlm-authentication
# https://gitlab.com/os85/httpx/wikis/Authorization#ntlm-auth
#
module NTLMAuth
class << self
def load_dependencies(_klass)
require_relative "authentication/ntlm"
require_relative "auth/ntlm"
end
def extra_options(options)
@ -25,11 +25,11 @@ module HTTPX
end
module InstanceMethods
def ntlm_authentication(user, password, domain = nil)
def ntlm_auth(user, password, domain = nil)
with(ntlm: Authentication::Ntlm.new(user, password, domain: domain))
end
alias_method :ntlm_auth, :ntlm_authentication
private
def send_requests(*requests)
requests.flat_map do |request|
@ -55,6 +55,6 @@ module HTTPX
end
end
end
register_plugin :ntlm_authentication, NTLMAuth
register_plugin :ntlm_auth, NTLMAuth
end
end

View File

@ -8,7 +8,7 @@ module HTTPX
module OAuth
class << self
def load_dependencies(_klass)
require_relative "authentication/basic"
require_relative "auth/basic"
end
end
@ -106,7 +106,7 @@ module HTTPX
end
module InstanceMethods
def oauth_authentication(**args)
def oauth_auth(**args)
with(oauth_session: OAuthSession.new(**args))
end

View File

@ -53,7 +53,7 @@ module HTTPX
auth_scheme = scheme.to_s.capitalize
require_relative "authentication/#{scheme}" unless defined?(Authentication) && Authentication.const_defined?(auth_scheme, false)
require_relative "auth/#{scheme}" unless defined?(Authentication) && Authentication.const_defined?(auth_scheme, false)
@authenticator = Authentication.const_get(auth_scheme).new(@username, @password, **extra)
end

View File

@ -20,7 +20,7 @@ module HTTPX
class << self
def load_dependencies(*)
require_relative "../authentication/socks5"
require_relative "../auth/socks5"
end
def extra_options(options)

View File

@ -12,10 +12,10 @@ module HTTPX
def with: (options) -> Session
| (options) { (Session) -> void } -> void
def plugin: (:authentication, ?options) -> Plugins::sessionAuthentication
| (:basic_authentication, ?options) -> Plugins::sessionBasicAuth
| (:digest_authentication, ?options) -> Plugins::sessionDigestAuth
| (:ntlm_authentication, ?options) -> Plugins::sessionNTLMAuth
def plugin: (:auth, ?options) -> Plugins::sessionAuthorization
| (:basic_auth, ?options) -> Plugins::sessionBasicAuth
| (:digest_auth, ?options) -> Plugins::sessionDigestAuth
| (:ntlm_auth, ?options) -> Plugins::sessionNTLMAuth
| (:aws_sdk_authentication, ?options) -> Plugins::sessionAwsSdkAuthentication
| (:compression, ?options) -> Session
| (:cookies, ?options) -> Plugins::sessionCookies

13
sig/plugins/auth.rbs Normal file
View File

@ -0,0 +1,13 @@
module HTTPX
module Plugins
module Authorization
module InstanceMethods
def authorization: (string token) -> instance
def bearer_auth: (string token) -> instance
end
end
type sessionAuthorization = Session & Authorization::InstanceMethods
end
end

View File

@ -1,13 +0,0 @@
module HTTPX
module Plugins
module Authentication
module InstanceMethods
def authentication: (string token) -> instance
def bearer_auth: (string token) -> instance
end
end
type sessionAuthentication = Session & Authentication::InstanceMethods
end
end

View File

@ -6,10 +6,10 @@ module HTTPX
def self.configure: (singleton(Session)) -> void
module InstanceMethods
def basic_authentication: (string user, string password) -> instance
def basic_auth: (string user, string password) -> instance
end
end
type sessionBasicAuth = sessionAuthentication & Authentication::InstanceMethods & BasicAuth::InstanceMethods
type sessionBasicAuth = sessionAuthorization & BasicAuth::InstanceMethods
end
end

View File

@ -12,10 +12,10 @@ module HTTPX
def self.load_dependencies: (*untyped) -> void
module InstanceMethods
def digest_authentication: (string user, string password, ?hashed: bool) -> instance
def digest_auth: (string user, string password, ?hashed: bool) -> instance
end
end
type sessionDigestAuth = sessionAuthentication & DigestAuth::InstanceMethods
type sessionDigestAuth = sessionAuthorization & DigestAuth::InstanceMethods
end
end

View File

@ -11,11 +11,11 @@ module HTTPX
def self.load_dependencies: (*untyped) -> void
module InstanceMethods
def ntlm_authentication: (string user, string password, ?string? domain) -> instance
def ntlm_auth: (string user, string password, ?string? domain) -> instance
end
end
type sessionNTLMAuth = sessionAuthentication & NTLMAuth::InstanceMethods
type sessionNTLMAuth = sessionAuthorization & NTLMAuth::InstanceMethods
end
end

View File

@ -43,7 +43,7 @@ module HTTPX
end
module InstanceMethods
def oauth_authentication: (**untyped args) -> instance
def oauth_auth: (**untyped args) -> instance
def with_access_token: () -> instance
end

View File

@ -7,7 +7,7 @@ module Requests
def test_plugin_bearer_auth
get_uri = build_uri("/get")
session = HTTPX.plugin(:authentication)
session = HTTPX.plugin(:auth)
response = session.bearer_auth("TOKEN").get(get_uri)
verify_status(response, 200)
body = json_body(response)
@ -16,13 +16,13 @@ module Requests
# Basic Auth
def test_plugin_basic_authentication
def test_plugin_basic_auth
no_auth_response = HTTPX.get(basic_auth_uri)
verify_status(no_auth_response, 401)
verify_header(no_auth_response.headers, "www-authenticate", "Basic realm=\"Fake Realm\"")
no_auth_response.close
session = HTTPX.plugin(:basic_authentication)
session = HTTPX.plugin(:basic_auth)
response = session.basic_auth(user, pass).get(basic_auth_uri)
verify_status(response, 200)
body = json_body(response)
@ -35,8 +35,8 @@ module Requests
# Digest
def test_plugin_digest_authentication
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
def test_plugin_digest_auth
session = HTTPX.plugin(:digest_auth).with_headers("cookie" => "fake=fake_value")
response = session.digest_auth(user, pass).get(digest_auth_uri)
verify_status(response, 200)
body = json_body(response)
@ -45,8 +45,8 @@ module Requests
end
%w[SHA1 SHA2 SHA256 SHA384 SHA512 RMD160].each do |alg|
define_method "test_plugin_digest_authentication_#{alg}" do
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
define_method "test_plugin_digest_auth_#{alg}" do
session = HTTPX.plugin(:digest_auth).with_headers("cookie" => "fake=fake_value")
response = session.digest_auth(user, pass).get("#{digest_auth_uri}/#{alg}")
verify_status(response, 200)
body = json_body(response)
@ -56,10 +56,10 @@ module Requests
end
%w[MD5 SHA1].each do |alg|
define_method "test_plugin_digest_authentication_#{alg}_sess" do
define_method "test_plugin_digest_auth_#{alg}_sess" do
start_test_servlet(DigestServer, algorithm: "#{alg}-sess") do |server|
uri = "#{server.origin}/"
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
session = HTTPX.plugin(:digest_auth).with_headers("cookie" => "fake=fake_value")
response = session.digest_auth(user, server.get_passwd(user), hashed: true).get(uri)
verify_status(response, 200)
assert response.read == "yay"
@ -67,8 +67,8 @@ module Requests
end
end
def test_plugin_digest_authentication_bypass
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
def test_plugin_digest_auth_bypass
session = HTTPX.plugin(:digest_auth).with_headers("cookie" => "fake=fake_value")
response = session.get(digest_auth_uri)
verify_status(response, 401)
response = session.get(build_uri("/get"))
@ -81,12 +81,12 @@ module Requests
if RUBY_VERSION < "3.1.0"
# TODO: enable again once ruby-openssl 3 supports legacy ciphers
def test_plugin_ntlm_authentication
def test_plugin_ntlm_auth
return if origin.start_with?("https")
start_test_servlet(NTLMServer) do |server|
uri = "#{server.origin}/"
HTTPX.plugin(SessionWithPool).plugin(:ntlm_authentication).wrap do |http|
HTTPX.plugin(SessionWithPool).plugin(:ntlm_auth).wrap do |http|
# skip unless NTLM
no_auth_response = http.get(uri)
verify_status(no_auth_response, 401)
@ -100,7 +100,7 @@ module Requests
verify_status(response, 200)
response = http.ntlm_auth("user", "password").get(build_uri("/get"))
verify_status(response, 200)
# invalid_response = http.ntlm_authentication("user", "fake").get(uri)
# invalid_response = http.ntlm_auth("user", "fake").get(uri)
# verify_status(invalid_response, 401)
end
end

View File

@ -5,7 +5,7 @@ module Requests
module OAuth
def test_plugin_oauth_options
with_oauth_metadata do |server|
opts = HTTPX.plugin(:oauth).oauth_authentication(
opts = HTTPX.plugin(:oauth).oauth_auth(
issuer: server.origin,
client_id: "CLIENT_ID", client_secret: "SECRET",
scope: "all"
@ -16,7 +16,7 @@ module Requests
assert opts.oauth_session.token_endpoint_auth_method == "client_secret_basic"
assert opts.oauth_session.scope == %w[all]
opts = HTTPX.plugin(:oauth).oauth_authentication(
opts = HTTPX.plugin(:oauth).oauth_auth(
issuer: "https://smthelse",
token_endpoint_auth_method: "client_secret_post",
client_id: "CLIENT_ID", client_secret: "SECRET",
@ -29,7 +29,7 @@ module Requests
assert opts.oauth_session.scope == %w[foo bar]
assert_raises(HTTPX::Error) do
HTTPX.plugin(:oauth).oauth_authentication(
HTTPX.plugin(:oauth).oauth_auth(
issuer: server.origin,
client_id: "CLIENT_ID", client_secret: "SECRET",
token_endpoint_auth_method: "unsupported"
@ -40,7 +40,7 @@ module Requests
def test_plugin_oauth_client_credentials
with_oauth_metadata do |server|
session = HTTPX.plugin(:oauth).oauth_authentication(
session = HTTPX.plugin(:oauth).oauth_auth(
issuer: server.origin,
client_id: "CLIENT_ID", client_secret: "SECRET", scope: "all"
)
@ -57,7 +57,7 @@ module Requests
def test_plugin_oauth_refresh_token
with_oauth_metadata do |server|
session = HTTPX.plugin(:oauth).oauth_authentication(
session = HTTPX.plugin(:oauth).oauth_auth(
issuer: server.origin,
token_endpoint_auth_method: "client_secret_post",
client_id: "CLIENT_ID", client_secret: "SECRET",

View File

@ -94,7 +94,7 @@ module Requests
private
def webdav_client
@webdav_client ||= HTTPX.plugin(:basic_authentication).plugin(:webdav, origin: start_webdav_server).basic_auth("user", "pass")
@webdav_client ||= HTTPX.plugin(:basic_auth).plugin(:webdav, origin: start_webdav_server).basic_auth("user", "pass")
end
def start_webdav_server