Class: Aspera::Cli::WebSecret
- Inherits:
-
Object
- Object
- Aspera::Cli::WebSecret
- Defined in:
- lib/aspera/cli/web_secret.rb
Overview
Secrets entered by the user on a web page, instead of the terminal.
Used by extended value @secret: when option ui is graphical, and in MCP server mode (see Mcp::Secrets).
Requests are kept in memory, identified by a random value in the URL of the page, and expire after TTL seconds. Pages are served by a local web server, started on first request, or by the web server of the caller (see #handle).
Constant Summary collapse
- TTL =
Lifetime of a request in seconds: the user shall enter the secret within this time
600- PATH_PREFIX =
Path of the page, followed by the request identifier
'/secret/'
Class Method Summary collapse
-
.ask(prompt) ⇒ String
Ask for a secret on a web page, opened in the browser.
Instance Method Summary collapse
-
#create(prompt) ⇒ String
Identifier of the new request.
-
#delete(ids) ⇒ Object
Forget requests, and their secrets.
-
#handle(req, res) ⇒ Object
Serve the page: GET shows the form, POST receives the secret or the cancellation.
-
#initialize(base_url: nil, access_code: nil, name: Info::CMD_NAME, requester: nil) ⇒ WebSecret
constructor
A new instance of WebSecret.
-
#prompt(id) ⇒ String?
Prompt of the request,
nilif unknown or expired. -
#shutdown ⇒ Object
Stop the local web server, if started, after it sends the last page to the browser.
-
#url(id) ⇒ String
URL of the page where the user enters the secret.
-
#wait_value(id) {|elapsed| ... } ⇒ String
Wait until the user enters the secret, or cancels, on the page.
Constructor Details
#initialize(base_url: nil, access_code: nil, name: Info::CMD_NAME, requester: nil) ⇒ WebSecret
Returns a new instance of WebSecret.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/aspera/cli/web_secret.rb', line 59 def initialize(base_url: nil, access_code: nil, name: Info::CMD_NAME, requester: nil) Aspera.assert(access_code.nil? || !access_code.to_s.empty?) { 'access_code must not be empty' } @base_url = base_url @access_code = access_code&.to_s @name = name @requester = requester @mutex = Mutex.new @cond = ConditionVariable.new # id => Request @requests = {} @local_server = nil @local_thread = nil end |
Class Method Details
.ask(prompt) ⇒ String
Ask for a secret on a web page, opened in the browser
45 46 47 48 49 50 51 52 |
# File 'lib/aspera/cli/web_secret.rb', line 45 def ask(prompt) web_secret = new id = web_secret.create(prompt) Environment.instance.open_uri(web_secret.url(id)) web_secret.wait_value(id) ensure web_secret&.shutdown end |
Instance Method Details
#create(prompt) ⇒ String
Returns Identifier of the new request.
75 76 77 78 79 80 81 82 |
# File 'lib/aspera/cli/web_secret.rb', line 75 def create(prompt) id = SecureRandom.urlsafe_base64(24) @mutex.synchronize do purge_expired @requests[id] = Request.new(prompt: prompt, status: :pending, expires_at: Time.now + TTL) end id end |
#delete(ids) ⇒ Object
Forget requests, and their secrets
117 118 119 |
# File 'lib/aspera/cli/web_secret.rb', line 117 def delete(ids) @mutex.synchronize { ids.each { |id| @requests.delete(id) } } end |
#handle(req, res) ⇒ Object
Serve the page: GET shows the form, POST receives the secret or the cancellation
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/aspera/cli/web_secret.rb', line 131 def handle(req, res) require 'webrick' id = req.path.delete_prefix(PATH_PREFIX) status, page = @mutex.synchronize do request = live_request(id) if request.nil? || !request.status.eql?(:pending) [404, {state: :unknown}] elsif req.request_method.eql?('GET') [200, {state: :form, prompt: request.prompt}] elsif req.request_method.eql?('POST') answer(request, WEBrick::HTTPUtils.parse_query(req.body.to_s)) else [405, {state: :unknown}] end end res.status = status WebPage.secure_headers(res) # Variables of the template, state is one of: :form, :received, :cancelled, :unknown res.body = WebPage.render( HTML_TEMPLATE_PATH, title: 'Secret request', name: @name, requester: @requester, action_url: "#{PATH_PREFIX}#{id}", access_code_required: !@access_code.nil?, prompt: nil, error: nil, **page ) end |
#prompt(id) ⇒ String?
Returns Prompt of the request, nil if unknown or expired.
92 93 94 |
# File 'lib/aspera/cli/web_secret.rb', line 92 def prompt(id) @mutex.synchronize { live_request(id)&.prompt } end |
#shutdown ⇒ Object
Stop the local web server, if started, after it sends the last page to the browser
122 123 124 125 126 |
# File 'lib/aspera/cli/web_secret.rb', line 122 def shutdown return if @local_server.nil? @local_server.shutdown @local_thread.join(SHUTDOWN_TIMEOUT) end |
#url(id) ⇒ String
Returns URL of the page where the user enters the secret.
86 87 88 |
# File 'lib/aspera/cli/web_secret.rb', line 86 def url(id) "#{base_url}#{PATH_PREFIX}#{id}" end |
#wait_value(id) {|elapsed| ... } ⇒ String
Wait until the user enters the secret, or cancels, on the page
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/aspera/cli/web_secret.rb', line 101 def wait_value(id) start = Time.now loop do @mutex.synchronize do request = live_request(id) raise Error, 'Secret request expired: execute the command again' if request.nil? raise Error, 'Secret entry cancelled by user' if request.status.eql?(:cancelled) return request.value if request.status.eql?(:received) @cond.wait(@mutex, (request.expires_at - Time.now).clamp(0, WAIT_SLICE)) end yield(Time.now - start) if block_given? end end |