Class: Legion::Extensions::Identity::Entra::Helpers::CallbackServer

Inherits:
Object
  • Object
show all
Includes:
Helpers::Lex
Defined in:
lib/legion/extensions/identity/entra/helpers/callback_server.rb

Constant Summary collapse

RESPONSE_HTML =
<<~HTML
  <html><body style="font-family:sans-serif;text-align:center;padding:40px;">
  <h2>Authentication complete</h2><p>You can close this window.</p></body></html>
HTML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCallbackServer

Returns a new instance of CallbackServer.



22
23
24
25
26
27
28
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 22

def initialize
  @server = nil
  @port = nil
  @result = nil
  @mutex = Mutex.new
  @cv = ConditionVariable.new
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



20
21
22
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 20

def port
  @port
end

Instance Method Details

#redirect_uriObject



52
53
54
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 52

def redirect_uri
  "http://127.0.0.1:#{@port}/callback"
end

#shutdownObject



43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 43

def shutdown
  @server&.close
  @thread&.join(2)
  @thread&.kill
rescue IOError => e
  log.debug("shutdown ignored closed server: #{e.message}")
  nil
end

#startObject



30
31
32
33
34
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 30

def start
  @server = TCPServer.new('127.0.0.1', 0)
  @port = @server.addr[1]
  @thread = Thread.new { listen } # rubocop:disable ThreadSafety/NewThread
end

#wait_for_callback(timeout: 120) ⇒ Object



36
37
38
39
40
41
# File 'lib/legion/extensions/identity/entra/helpers/callback_server.rb', line 36

def wait_for_callback(timeout: 120)
  @mutex.synchronize do
    @cv.wait(@mutex, timeout) unless @result
    @result
  end
end