Class: Sendmux::Core::Auth
- Inherits:
-
Object
- Object
- Sendmux::Core::Auth
- Defined in:
- lib/sendmux/core/auth.rb
Constant Summary collapse
- ROOT_PREFIX =
'smx_root_'- MAILBOX_PREFIX =
'smx_mbx_'
Class Method Summary collapse
- .assert_api_key_surface(api_key, expected_surface) ⇒ Object
- .configure_base_url(configuration, base_url) ⇒ Object
- .configure_bearer(configuration, api_key, expected_surface, base_url: nil) ⇒ Object
- .surface_for(api_key) ⇒ Object
Class Method Details
.assert_api_key_surface(api_key, expected_surface) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/sendmux/core/auth.rb', line 22 def self.assert_api_key_surface(api_key, expected_surface) actual = surface_for(api_key) raise ArgumentError, "Sendmux API keys must start with #{ROOT_PREFIX} or #{MAILBOX_PREFIX}" unless actual return actual if actual == expected_surface raise ArgumentError, "Expected a #{expected_surface} API key, received a #{actual} API key" end |
.configure_base_url(configuration, base_url) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/sendmux/core/auth.rb', line 37 def self.configure_base_url(configuration, base_url) uri = URI(base_url) raise ArgumentError, 'base_url must include a scheme and host' unless uri.scheme && uri.host configuration.scheme = uri.scheme configuration.host = uri.host configuration.base_path = uri.path configuration.ignore_operation_servers = true if configuration.respond_to?(:ignore_operation_servers=) end |
.configure_bearer(configuration, api_key, expected_surface, base_url: nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/sendmux/core/auth.rb', line 11 def self.configure_bearer(configuration, api_key, expected_surface, base_url: nil) assert_api_key_surface(api_key, expected_surface) unless configuration.respond_to?(:access_token=) raise ArgumentError, 'Generated configuration does not support bearer access tokens' end configuration.access_token = api_key configure_base_url(configuration, base_url) if base_url && !base_url.empty? configuration end |
.surface_for(api_key) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/sendmux/core/auth.rb', line 30 def self.surface_for(api_key) return ApiKeySurface::ROOT if api_key.start_with?(ROOT_PREFIX) return ApiKeySurface::MAILBOX if api_key.start_with?(MAILBOX_PREFIX) nil end |