Class: HTTPClient::OAuth

Inherits:
Object
  • Object
show all
Includes:
Util, Mutex_m
Defined in:
lib/httpclient/auth.rb

Overview

Authentication filter for handling OAuth negotiation. Used in WWWAuth.

CAUTION: This impl only support ‘#7 Accessing Protected Resources’ in OAuth Core 1.0 spec for now. You need to obtain Access token and Access secret by yourself.

CAUTION: This impl does NOT support OAuth Request Body Hash spec for now. oauth.googlecode.com/svn/spec/ext/body_hash/1.0/oauth-bodyhash.html

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#argument_to_hash, hash_find_value, #http?, #https?, #keyword_argument, try_require, uri_dirname, uri_part_of, urify

Constructor Details

#initializeOAuth

Creates new DigestAuth filter.



770
771
772
773
774
775
776
777
778
779
780
# File 'lib/httpclient/auth.rb', line 770

def initialize
  super
  @config = nil # common config
  @auth = {} # configs for each site
  @challenge = {}
  @nonce_count = 0
  @signature_handler = {
    'HMAC-SHA1' => method(:sign_hmac_sha1)
  }
  @scheme = "OAuth"
end

Instance Attribute Details

#schemeObject (readonly)

Authentication scheme.



700
701
702
# File 'lib/httpclient/auth.rb', line 700

def scheme
  @scheme
end

Class Method Details

.escape(str) ⇒ Object

:nodoc:



753
754
755
756
757
758
759
760
761
762
763
# File 'lib/httpclient/auth.rb', line 753

def self.escape(str) # :nodoc:
  if str.respond_to?(:force_encoding)
    str.dup.force_encoding('BINARY').gsub(/([^a-zA-Z0-9_.~-]+)/) {
      '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
    }
  else
    str.gsub(/([^a-zA-Z0-9_.~-]+)/n) {
      '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase
    }
  end
end

Instance Method Details

#challenge(uri, param_str = nil) ⇒ Object

Challenge handler: remember URL for response.

challenge() in OAuth handler always returns false to avoid connection retry which should not work in OAuth authentication context. This method just remember URL (nil means ‘any’) for the next connection. Normally OAuthClient handles this correctly but see how it uses when you need to use this class directly.



843
844
845
846
847
848
849
850
851
852
# File 'lib/httpclient/auth.rb', line 843

def challenge(uri, param_str = nil)
  synchronize {
    if uri.nil?
      @challenge[nil] = true
    else
      @challenge[urify(uri)] = true
    end
    false
  }
end

#escape(str) ⇒ Object



765
766
767
# File 'lib/httpclient/auth.rb', line 765

def escape(str)
  self.class.escape(str)
end

#get(req) ⇒ Object

Response handler: returns credential. It sends cred only when a given uri is;

  • child page of challengeable(got *Authenticate before) uri and,

  • child page of defined credential



824
825
826
827
828
829
830
831
832
833
834
# File 'lib/httpclient/auth.rb', line 824

def get(req)
  target_uri = req.header.request_uri
  synchronize {
    return nil unless @challenge[nil] or @challenge.find { |uri, ok|
      Util.uri_part_of(target_uri, uri) and ok
    }
    config = do_get_config(target_uri) || @config
    return nil unless config
    calc_cred(req, config)
  }
end

#get_config(uri = nil) ⇒ Object

Get authentication credential.



814
815
816
817
818
# File 'lib/httpclient/auth.rb', line 814

def get_config(uri = nil)
  synchronize {
    do_get_config(uri)
  }
end

#reset_challengeObject

Resets challenge state. Do not send ‘*Authorization’ header until the server sends ‘*Authentication’ again.



784
785
786
787
788
# File 'lib/httpclient/auth.rb', line 784

def reset_challenge
  synchronize do
    @challenge.clear
  end
end

#set(*args) ⇒ Object

Set authentication credential. You cannot set OAuth config via WWWAuth#set_auth. Use OAuth#config=



792
793
794
# File 'lib/httpclient/auth.rb', line 792

def set(*args)
  # not supported
end

#set?Boolean

Check always (not effective but it works)

Returns:

  • (Boolean)


797
798
799
# File 'lib/httpclient/auth.rb', line 797

def set?
  !@challenge.empty?
end

#set_config(uri, config) ⇒ Object

Set authentication credential.



802
803
804
805
806
807
808
809
810
811
# File 'lib/httpclient/auth.rb', line 802

def set_config(uri, config)
  synchronize do
    if uri.nil?
      @config = config
    else
      uri = Util.uri_dirname(urify(uri))
      @auth[uri] = config
    end
  end
end