Class: OAuth::Signature::Base — Documentation by YARD 0.9.44

# Class: OAuth::Signature::Base

Inherits:
  Object
  - Object
  - OAuth::Signature::Base
  show all

Includes:
  Helper

Defined in:
  lib/oauth/signature/base.rb

## Overview

Base class for OAuth signature implementations.

Includes {OAuth::AUTH_SANITIZER::FilteredAttributes} so inspect output redacts
secret-bearing fields captured during signature construction.

## Direct Known Subclasses

HMAC::SHA1, HMAC::SHA256, PLAINTEXT, RSA::SHA1

## Instance Attribute Summary collapse

- #consumer_secret ⇒ Object readonly

  Returns the value of attribute consumer_secret.

- #options ⇒ Hash

  Signature construction options.

- #request ⇒ Object readonly

  Returns the value of attribute request.

- #token_secret ⇒ Object readonly

  Returns the value of attribute token_secret.

## Class Method Summary collapse

- .implements(signature_method = nil) ⇒ Object

## Instance Method Summary collapse

- #==(other) ⇒ Object
- #body_hash ⇒ Object
- #initialize(request, options = {}, &block) ⇒ Base constructor

  A new instance of Base.

- #signature ⇒ Object
- #signature_base_string ⇒ Object
- #verify ⇒ Object

### Methods included from Helper

_escape, escape, generate_key, generate_timestamp, normalize, normalize_nested_query, parse_header, stringify_keys, unescape

## Constructor Details

### #initialize(request, options = {}, &block) ⇒ `Base`

Returns a new instance of Base.

Raises:

- (`TypeError`)

    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
    58
    59
    60
    61
    62
    # File 'lib/oauth/signature/base.rb', line 33

    def initialize(request, options = {}, &block)
      raise TypeError unless request.is_a?(OAuth::RequestProxy::Base)

      @request = request
      @options = options

      ## consumer secret was determined beforehand

      @consumer_secret = options[:consumer].secret if options[:consumer]

      # presence of :consumer_secret option will override any Consumer that's provided
      @consumer_secret = options[:consumer_secret] if options[:consumer_secret]

      ## token secret was determined beforehand

      @token_secret = options[:token].secret if options[:token]

      # presence of :token_secret option will override any Token that's provided
      @token_secret = options[:token_secret] if options[:token_secret]

      # override secrets based on the values returned from the block (if any)
      if block
        # consumer secret and token secret need to be looked up based on pieces of the request
        secrets = yield (block.arity == 1) ? request : [token, consumer_key, nonce, request.timestamp]
        if secrets.is_a?(Array) && secrets.size == 2
          @token_secret = secrets[0]
          @consumer_secret = secrets[1]
        end
      end
    end

## Instance Attribute Details

### #consumer_secret ⇒ `Object` (readonly)

Returns the value of attribute consumer_secret.

    22
    23
    24
    # File 'lib/oauth/signature/base.rb', line 22

    def consumer_secret
      @consumer_secret
    end

### #options ⇒ `Hash`

Signature construction options.

Returns:

- (`Hash`)

    21
    22
    23
    # File 'lib/oauth/signature/base.rb', line 21

    def options
      @options
    end

### #request ⇒ `Object` (readonly)

Returns the value of attribute request.

    22
    23
    24
    # File 'lib/oauth/signature/base.rb', line 22

    def request
      @request
    end

### #token_secret ⇒ `Object` (readonly)

Returns the value of attribute token_secret.

    22
    23
    24
    # File 'lib/oauth/signature/base.rb', line 22

    def token_secret
      @token_secret
    end

## Class Method Details

### .implements(signature_method = nil) ⇒ `Object`

    26
    27
    28
    29
    30
    # File 'lib/oauth/signature/base.rb', line 26

    def implements(signature_method = nil)
      return OAuth::Signature.available_methods.key(self) if signature_method.nil?

      OAuth::Signature.available_methods[signature_method] = self
    end

## Instance Method Details

### #==(other) ⇒ `Object`

    68
    69
    70
    71
    72
    # File 'lib/oauth/signature/base.rb', line 68

    def ==(other)
      check = signature.bytesize ^ other.bytesize
      signature.bytes.zip(other.bytes) { |x, y| check |= x ^ y.to_i }
      check.zero?
    end

### #body_hash ⇒ `Object`

    82
    83
    84
    # File 'lib/oauth/signature/base.rb', line 82

    def body_hash
      raise_instantiation_error
    end

### #signature ⇒ `Object`

    64
    65
    66
    # File 'lib/oauth/signature/base.rb', line 64

    def signature
      Base64.encode64(digest).chomp.delete("\n")
    end

### #signature_base_string ⇒ `Object`

    78
    79
    80
    # File 'lib/oauth/signature/base.rb', line 78

    def signature_base_string
      request.signature_base_string
    end

### #verify ⇒ `Object`

    74
    75
    76
    # File 'lib/oauth/signature/base.rb', line 74

    def verify
      self == request.signature
    end