Class: Aspera::WebAuth::Server

Inherits:
Aspera::WebServerSimple show all
Defined in:
lib/aspera/web_auth/server.rb

Overview

start a local web server then start a browser that will callback the local server upon authentication store the final query

Instance Attribute Summary collapse

Attributes inherited from Aspera::WebServerSimple

#uri

Instance Method Summary collapse

Methods inherited from Aspera::WebServerSimple

#<<, assert_protected, loopback?, read_chain_file, self_signed_cert, serve, servlet_parameters, #start, url

Constructor Details

#initialize(endpoint_url, additional_info = nil, reply_keys: nil) ⇒ Server

Returns a new instance of Server.

Parameters:

  • endpoint_url (String) —

    e.g. 'https://127.0.0.1:12345'

  • additional_info (String) (defaults to: nil) —

    Information in web page

  • reply_keys (Array<String>, nil) (defaults to: nil) —

    If set, the page shows to the user a code with these keys of the query, to paste in the terminal of ascli that waits for it



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/aspera/web_auth/server.rb', line 79

def initialize(endpoint_url, additional_info = nil, reply_keys: nil)
  uri = URI.parse(endpoint_url)
  super(uri)
  @reply_keys = reply_keys
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @expected_path = uri.path.empty? ? '/' : uri.path
  @query = nil
  @additional_info = additional_info
  # last argument (self) is provided to constructor of servlet
  mount(@expected_path, Servlet, self)
  # server runs in thread
  @thread = Thread.new { start }
end

Instance Attribute Details

#additional_info ⇒ Array<String>? (readonly)

Returns Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page.

Returns:

  • (Array<String>, nil) —

    Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page



74
75
76
# File 'lib/aspera/web_auth/server.rb', line 74

def additional_info
  @additional_info
end

#expected_path ⇒ Array<String>? (readonly)

Returns Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page.

Returns:

  • (Array<String>, nil) —

    Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page



74
75
76
# File 'lib/aspera/web_auth/server.rb', line 74

def expected_path
  @expected_path
end

#reply_keys ⇒ Array<String>? (readonly)

Returns Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page.

Returns:

  • (Array<String>, nil) —

    Keys of the query that the page gives to the user as a code to paste in a terminal, nil for the standard page



74
75
76
# File 'lib/aspera/web_auth/server.rb', line 74

def reply_keys
  @reply_keys
end

Instance Method Details

#received_request ⇒ Hash

wait for request on web server (main thread)

Returns:

  • (Hash) —

    the query



117
118
119
120
121
122
123
# File 'lib/aspera/web_auth/server.rb', line 117

def received_request
  # wait for signal from thread, unless the request was already received (signal is not memorized)
  @mutex.synchronize { @cond.wait(@mutex) while @query.nil? }
  # tell server thread to stop
  shutdown
  return @query
end

#signal_request(request) ⇒ nil

Called by web server thread on received request

Returns:

  • (nil)

Raises:

  • (WEBrick::HTTPStatus::NotFound)


105
106
107
108
109
110
111
112
113
# File 'lib/aspera/web_auth/server.rb', line 105

def signal_request(request)
  raise WEBrick::HTTPStatus::NotFound, "unexpected path: #{request.path}" unless request.path.eql?(@expected_path)
  # acquire lock and signal change
  @mutex.synchronize do
    @query = request.query
    @cond.signal
  end
  nil
end

#wait_stopped(timeout = 10) ⇒ nil

Wait until the server thread has ended: the response of the last request (page shown to the user) is sent. To call after received_request, when the process does not continue with a long operation.

Parameters:

  • timeout (Integer) (defaults to: 10) —

    Maximum time to wait, in seconds

Returns:

  • (nil)


98
99
100
101
# File 'lib/aspera/web_auth/server.rb', line 98

def wait_stopped(timeout = 10)
  @thread.join(timeout)
  nil
end