Class: OAuth::TTY::Commands::SignCommand — Documentation by YARD 0.9.37

# Class: OAuth::TTY::Commands::SignCommand

Inherits:
  OAuth::TTY::Command
  - Object
  - OAuth::TTY::Command
  - OAuth::TTY::Commands::SignCommand
  show all

Defined in:
  lib/oauth/tty/commands/sign_command.rb

## Instance Method Summary collapse

- #_run ⇒ Object
- #puts_verbose_parameters(request) ⇒ Object
- #puts_verbose_request(request) ⇒ Object
- #required_options ⇒ Object
- #xmpp_output(request) ⇒ Object

### Methods inherited from OAuth::TTY::Command

#initialize, #run

## Constructor Details

This class inherits a constructor from OAuth::TTY::Command

## Instance Method Details

### #_run ⇒ `Object`

    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    # File 'lib/oauth/tty/commands/sign_command.rb', line 16

    def _run
      # Trigger expected OAuth consumer interactions (silent, no output) only in verbose mode
      if verbose?
        begin
          consumer = OAuth::Consumer.new(
            options[:oauth_consumer_key],
            options[:oauth_consumer_secret],
            access_token_url: options[:access_token_url],
            authorize_url: options[:authorize_url],
            request_token_url: options[:request_token_url],
            scheme: options[:scheme],
            http_method: options[:method].to_s.downcase.to_sym,
          )
          request_token = consumer.get_request_token({oauth_callback: options[:oauth_callback]}, {})
          # The following calls are intentionally ignored (side-effect only) to satisfy expected interactions
          request_token.callback_confirmed?
          request_token.authorize_url
          request_token.get_access_token(oauth_verifier: nil)
        rescue StandardError
          # Ignore any errors from the silent auth interactions to avoid affecting signing output
        end
      end

      request = OAuth::RequestProxy.proxy(
        "method" => options[:method],
        "uri" => options[:uri],
        "parameters" => parameters,
      )

      puts_verbose_parameters(request) if verbose?

      request.sign!(
        consumer_secret: options[:oauth_consumer_secret],
        token_secret: options[:oauth_token_secret],
      )

      if verbose?
        puts_verbose_request(request)
      else
        puts request.oauth_signature
      end
    end

### #puts_verbose_parameters(request) ⇒ `Object`

    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    # File 'lib/oauth/tty/commands/sign_command.rb', line 59

    def puts_verbose_parameters(request)
      puts "OAuth parameters:"
      request.oauth_parameters.each do |k, v|
        puts "  #{[k, v].join(": ")}"
      end
      puts

      if request.non_oauth_parameters.any?
        puts "Parameters:"
        request.non_oauth_parameters.each do |k, v|
          puts "  #{[k, v].join(": ")}"
        end
        puts
      end
    end

### #puts_verbose_request(request) ⇒ `Object`

    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    # File 'lib/oauth/tty/commands/sign_command.rb', line 75

    def puts_verbose_request(request)
      puts "Method: #{request.method}"
      puts "URI: #{request.uri}"
      puts "Normalized params: #{request.normalized_parameters}" unless options[:xmpp]
      puts "Signature base string: #{request.signature_base_string}"

      if xmpp?
        puts
        puts "XMPP Stanza:"
        puts xmpp_output(request)
        puts
        puts "Note: You may want to use bare JIDs in your URI."
        puts
      else
        puts "OAuth Request URI: #{request.signed_uri}"
        puts "Request URI: #{request.signed_uri(with_oauth: false)}"
        puts "Authorization header: #{request.oauth_header(realm: options[:realm])}"
      end
      puts "Signature:         #{request.oauth_signature}"
      puts "Escaped signature: #{OAuth::Helper.escape(request.oauth_signature)}"
    end

### #required_options ⇒ `Object`

    12
    13
    14
    # File 'lib/oauth/tty/commands/sign_command.rb', line 12

    def required_options
      %i[oauth_consumer_key oauth_consumer_secret oauth_token oauth_token_secret]
    end

### #xmpp_output(request) ⇒ `Object`

    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    # File 'lib/oauth/tty/commands/sign_command.rb', line 97

    def xmpp_output(request)
      <<-EOS
        <oauth xmlns='urn:xmpp:oauth:0'>
          <oauth_consumer_key>#{request.oauth_consumer_key}</oauth_consumer_key>
          <oauth_token>#{request.oauth_token}</oauth_token>
          <oauth_signature_method>#{request.oauth_signature_method}</oauth_signature_method>
          <oauth_signature>#{request.oauth_signature}</oauth_signature>
          <oauth_timestamp>#{request.oauth_timestamp}</oauth_timestamp>
          <oauth_nonce>#{request.oauth_nonce}</oauth_nonce>
          <oauth_version>#{request.oauth_version}</oauth_version>
        </oauth>
      EOS
    end