Class: OAuth::TTY::Commands::AuthorizeCommand — Documentation by YARD 0.9.45

# Class: OAuth::TTY::Commands::AuthorizeCommand

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

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

## Instance Method Summary collapse

- #_run ⇒ Object
- #ask_user_for_verifier ⇒ Object
- #get_consumer ⇒ Object
- #get_request_token ⇒ Object
- #required_options ⇒ Object
- #verbosely_get_access_token(request_token, oauth_verifier) ⇒ Object

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

#initialize, #inspect, #run

## Constructor Details

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

## Instance Method Details

### #_run ⇒ `Object`

    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 15

    def _run
      request_token = get_request_token

      if request_token.callback_confirmed?
        puts "Server appears to support OAuth 1.0a; enabling support."
        options[:version] = "1.0a"
      end

      puts "Please visit this url to authorize:"
      puts request_token.authorize_url

      # parameters for OAuth 1.0a
      oauth_verifier = ask_user_for_verifier

      verbosely_get_access_token(request_token, oauth_verifier)
    end

### #ask_user_for_verifier ⇒ `Object`

    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 54

    def ask_user_for_verifier
      if options[:version] == "1.0a"
        puts "Please enter the verification code provided by the SP (oauth_verifier):"
        @stdin.gets.chomp
      else
        puts "Press return to continue..."
        @stdin.gets
        nil
      end
    end

### #get_consumer ⇒ `Object`

    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 42

    def get_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
      )
    end

### #get_request_token ⇒ `Object`

    32
    33
    34
    35
    36
    37
    38
    39
    40
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 32

    def get_request_token
      consumer = get_consumer
      scope_options = options[:scope] ? {"scope" => options[:scope]} : {}
      consumer.get_request_token({oauth_callback: options[:oauth_callback]}, scope_options)
    rescue OAuth::Unauthorized => e
      alert("A problem occurred while attempting to authorize:")
      alert(e)
      alert(e.request.body)
    end

### #required_options ⇒ `Object`

    11
    12
    13
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 11

    def required_options
      [:uri]
    end

### #verbosely_get_access_token(request_token, oauth_verifier) ⇒ `Object`

    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    # File 'lib/oauth/tty/commands/authorize_command.rb', line 65

    def verbosely_get_access_token(request_token, oauth_verifier)
      access_token = request_token.get_access_token(oauth_verifier: oauth_verifier)

      puts "Response:"
      access_token.params.each do |k, v|
        puts "  #{k}: #{v}" unless k.is_a?(Symbol)
      end
    rescue OAuth::Unauthorized => e
      alert("A problem occurred while attempting to obtain an access token:")
      alert(e)
      alert(e.request.body)
    end