Class: Aspera::WebAuth::Paste

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/web_auth/paste.rb

Overview

Receives the result of a web login from the user, who gives it in the terminal. No server is started: used when the browser cannot reach the loopback address of this host (e.g. SSH session, without tunnel). The user gives either:

  • a code (SealedHash) shown by the browser of the workstation, after ascli config web_login <code> (ascli is installed there)
  • the URL the browser was redirected to (it shows a connection error), or its query Same interface as Server for the part used by OAuth clients: received_request.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redirect_uri, login_url: nil, reply_keys: nil) ⇒ Paste

Returns a new instance of Paste.

Parameters:

  • redirect_uri (String) —

    Loopback URL registered for the login (the browser fails to connect to it)

  • login_url (String, nil) (defaults to: nil) —

    URL of the login page: if set, a code is given to the user for ascli on the workstation

  • reply_keys (Array, nil) (defaults to: nil) —

    Keys of the query needed to continue (with login_url)



67
68
69
70
71
# File 'lib/aspera/web_auth/paste.rb', line 67

def initialize(redirect_uri, login_url: nil, reply_keys: nil)
  @redirect_uri = redirect_uri
  @login_url = 
  @reply_keys = reply_keys
end

Class Method Details

.parse(input) ⇒ Hash{String=>String}

Extract the query parameters from what the user pasted

Parameters:

  • input (String) —

    Code, or full redirect URL, or the query only (with or without leading ?)

Returns:

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aspera/web_auth/paste.rb', line 29

def parse(input)
  text = input.to_s.strip
  raise ParameterError, 'Nothing was pasted' if text.empty?
  params =
    if SealedHash.sealed?(text)
      data = SealedHash.unseal(text, base_key: Cli::Info::CMD_NAME)
      raise ParameterError, 'This code is for ascli on the workstation: paste the code shown by the browser after login' if data.key?('login_url')
      data
    else
      parse_query(text)
    end
  raise ParameterError, 'No "state" found in what was pasted' if params['state'].to_s.empty?
  params
end

Instance Method Details

#received_request ⇒ Hash{String=>String}

Ask the user for the code, or the URL the browser was redirected to Input is not echoed on a terminal: it contains an authorization code or tokens.

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aspera/web_auth/paste.rb', line 81

def received_request
  Log.log.warn do
    browser_steps = [
      "The browser shows a connection error on #{@redirect_uri}: this is expected.",
      'Copy the full URL from the address bar of the browser, and paste it in the prompt below.'
    ]
    if @login_url.nil?
      (['USER ACTION: Log in with the web browser, then:'] + browser_steps.map { |s| "  - #{s}" }).join("\n")
    else
      code = SealedHash.seal({'login_url' => @login_url.to_s, 'redirect_uri' => @redirect_uri, 'reply_keys' => @reply_keys}, base_key: Cli::Info::CMD_NAME)
      [
        'USER ACTION: To continue, choose one of these two ways:',
        "  1. With any browser (#{Cli::Info::CMD_NAME} is not needed on the workstation):",
        '     - Open this URL in a browser and log in:',
        "         #{@login_url.to_s.red}",
        *browser_steps.map { |s| "     - #{s}" },
        "  2. If #{Cli::Info::CMD_NAME} is installed on the workstation that has the browser:",
        '     - Run there:',
        "         #{Cli::Info::CMD_NAME} config web_login #{code}",
        '     - Log in in the page that opens.',
        '     - Copy the code shown by the browser, and paste it in the prompt below.'
      ].join("\n")
    end
  end
  self.class.parse(Prompt.prompt_user_input('Code or URL', sensitive: true))
end

#shows_login_url? ⇒ Boolean

Returns true if the message given to the user contains the login URL (so it shall not be displayed separately).

Returns:

  • (Boolean) —

    true if the message given to the user contains the login URL (so it shall not be displayed separately)



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

def 
  !@login_url.nil?
end