Class: Legion::Extensions::Identity::Entra::Helpers::BrowserAuth
- Inherits:
-
Object
- Object
- Legion::Extensions::Identity::Entra::Helpers::BrowserAuth
- Includes:
- Logging::Helper, Settings::Helper
- Defined in:
- lib/legion/extensions/identity/entra/helpers/browser_auth.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#scopes ⇒ Object
readonly
Returns the value of attribute scopes.
-
#tenant_id ⇒ Object
readonly
Returns the value of attribute tenant_id.
Class Method Summary collapse
Instance Method Summary collapse
- #api_hook_available? ⇒ Boolean
- #authenticate ⇒ Object
- #generate_pkce ⇒ Object
- #gui_available? ⇒ Boolean
- #hook_redirect_uri ⇒ Object
-
#initialize(tenant_id:, client_id:, scopes: self.class.default_scopes, auth: nil, force_local_server: false) ⇒ BrowserAuth
constructor
A new instance of BrowserAuth.
- #open_browser(url) ⇒ Object
Constructor Details
#initialize(tenant_id:, client_id:, scopes: self.class.default_scopes, auth: nil, force_local_server: false) ⇒ BrowserAuth
Returns a new instance of BrowserAuth.
26 27 28 29 30 31 32 33 34 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 26 def initialize(tenant_id:, client_id:, scopes: self.class.default_scopes, auth: nil, force_local_server: false, **) @tenant_id = tenant_id @client_id = client_id @scopes = scopes @auth = auth || Object.new.extend(Legion::Extensions::Identity::Entra::Delegated::Runners::Login) @force_local_server = force_local_server log.debug("BrowserAuth initialized: tenant=#{tenant_id} client=#{client_id} force_local=#{force_local_server}") log.info("BrowserAuth scopes: #{@scopes}") end |
Instance Attribute Details
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
24 25 26 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 24 def client_id @client_id end |
#scopes ⇒ Object (readonly)
Returns the value of attribute scopes.
24 25 26 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 24 def scopes @scopes end |
#tenant_id ⇒ Object (readonly)
Returns the value of attribute tenant_id.
24 25 26 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 24 def tenant_id @tenant_id end |
Class Method Details
Instance Method Details
#api_hook_available? ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 46 def api_hook_available? return false if @force_local_server return false unless defined?(Legion::API) && defined?(Legion::Events) return false unless defined?(Legion::Extensions::Hooks::Base) hook_route_registered? end |
#authenticate ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 36 def authenticate if gui_available? log.info('BrowserAuth: GUI available, using browser auth') authenticate_browser else log.info('BrowserAuth: no GUI detected, using device code flow') authenticate_device_code end end |
#generate_pkce ⇒ Object
59 60 61 62 63 64 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 59 def generate_pkce verifier = SecureRandom.hex(32) challenge = [Digest::SHA256.digest(verifier)].pack('m0').tr('+/', '-_').delete('=') log.debug('BrowserAuth: PKCE challenge generated') [verifier, challenge] end |
#gui_available? ⇒ Boolean
66 67 68 69 70 71 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 66 def gui_available? os = host_os return true if /darwin|mswin|mingw/.match?(os) !ENV['DISPLAY'].nil? || !ENV['WAYLAND_DISPLAY'].nil? end |
#hook_redirect_uri ⇒ Object
54 55 56 57 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 54 def hook_redirect_uri port = Legion::Settings.dig(:api, :port) || 4567 "http://127.0.0.1:#{port}/api/extensions/identity/entra/hooks/auth/handle" end |
#open_browser(url) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/legion/extensions/identity/entra/helpers/browser_auth.rb', line 73 def open_browser(url) cmd = case host_os when /darwin/ then 'open' when /linux/ then 'xdg-open' when /mswin|mingw/ then 'start' end unless cmd log.warn('BrowserAuth: no browser command found for this OS') return false end log.debug("BrowserAuth: opening browser with #{cmd}") system(cmd, url) end |