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_'- AGENT_PREFIX =
'smx_agent_'
Class Method Summary collapse
- .assert_api_key_surface(api_key, expected_surface) ⇒ Object
- .compatible_surface?(api_key, actual, expected_surface) ⇒ Boolean
- .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
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sendmux/core/auth.rb', line 23 def self.assert_api_key_surface(api_key, expected_surface) actual = surface_for(api_key) unless actual raise ArgumentError, "Sendmux API keys must start with #{ROOT_PREFIX}, #{MAILBOX_PREFIX}, or #{AGENT_PREFIX}" end return actual if compatible_surface?(api_key, actual, expected_surface) raise ArgumentError, "Expected a #{expected_surface} API key, received a #{actual} API key" end |
.compatible_surface?(api_key, actual, expected_surface) ⇒ Boolean
34 35 36 37 38 39 40 41 |
# File 'lib/sendmux/core/auth.rb', line 34 def self.compatible_surface?(api_key, actual, expected_surface) return true if actual == expected_surface return true if expected_surface == ApiKeySurface::SENDING && (api_key.start_with?(MAILBOX_PREFIX) || api_key.start_with?(AGENT_PREFIX)) return true if expected_surface == ApiKeySurface::MAILBOX && actual == ApiKeySurface::MAILBOX false end |
.configure_base_url(configuration, base_url) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/sendmux/core/auth.rb', line 51 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
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sendmux/core/auth.rb', line 12 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
43 44 45 46 47 48 49 |
# File 'lib/sendmux/core/auth.rb', line 43 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) return ApiKeySurface::MAILBOX if api_key.start_with?(AGENT_PREFIX) nil end |