Class: HTTPClient::OAuth
- Inherits:
-
Object
- Object
- HTTPClient::OAuth
- Includes:
- Util
- 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
-
#scheme ⇒ Object
readonly
Authentication scheme.
Class Method Summary collapse
-
.escape(str) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#challenge(uri, param_str = nil) ⇒ Object
Challenge handler: remember URL for response.
- #escape(str) ⇒ Object
-
#get(req) ⇒ Object
Response handler: returns credential.
-
#get_config(uri = nil) ⇒ Object
Get authentication credential.
-
#initialize ⇒ OAuth
constructor
Creates new DigestAuth filter.
-
#reset_challenge ⇒ Object
Resets challenge state.
-
#set(uri, user, passwd) ⇒ Object
Set authentication credential.
-
#set_config(uri, config) ⇒ Object
Set authentication credential.
Methods included from Util
hash_find_value, #keyword_argument, uri_dirname, uri_part_of, #urify
Constructor Details
#initialize ⇒ OAuth
Creates new DigestAuth filter.
606 607 608 609 610 611 612 613 614 615 |
# File 'lib/httpclient/auth.rb', line 606 def initialize @config = nil # common config @auth = {} # configs for each site @challengeable = {} @nonce_count = 0 @signature_handler = { 'HMAC-SHA1' => method(:sign_hmac_sha1) } @scheme = "OAuth" end |
Instance Attribute Details
#scheme ⇒ Object (readonly)
Authentication scheme.
541 542 543 |
# File 'lib/httpclient/auth.rb', line 541 def scheme @scheme end |
Class Method Details
.escape(str) ⇒ Object
:nodoc:
589 590 591 592 593 594 595 596 597 598 599 |
# File 'lib/httpclient/auth.rb', line 589 def self.escape(str) # :nodoc: if str.respond_to?(:force_encoding) s = 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.
666 667 668 669 670 671 672 673 |
# File 'lib/httpclient/auth.rb', line 666 def challenge(uri, param_str = nil) if uri.nil? @challengeable[nil] = true else @challengeable[urify(uri)] = true end true end |
#escape(str) ⇒ Object
601 602 603 |
# File 'lib/httpclient/auth.rb', line 601 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
655 656 657 658 659 660 661 662 663 |
# File 'lib/httpclient/auth.rb', line 655 def get(req) target_uri = req.header.request_uri return nil unless @challengeable[nil] or @challengeable.find { |uri, ok| Util.uri_part_of(target_uri, uri) and ok } config = get_config(target_uri) || @config return nil unless config calc_cred(req, config) end |
#get_config(uri = nil) ⇒ Object
Get authentication credential.
640 641 642 643 644 645 646 647 648 649 |
# File 'lib/httpclient/auth.rb', line 640 def get_config(uri = nil) if uri.nil? @config else uri = urify(uri) Util.hash_find_value(@auth) { |cand_uri, cred| Util.uri_part_of(uri, cand_uri) } end end |
#reset_challenge ⇒ Object
Resets challenge state. Do not send ‘*Authorization’ header until the server sends ‘*Authentication’ again.
619 620 621 |
# File 'lib/httpclient/auth.rb', line 619 def reset_challenge @challengeable.clear end |
#set(uri, user, passwd) ⇒ Object
Set authentication credential. You cannot set OAuth config via WWWAuth#set_auth. Use OAuth#config=
625 626 627 |
# File 'lib/httpclient/auth.rb', line 625 def set(uri, user, passwd) # not supported end |
#set_config(uri, config) ⇒ Object
Set authentication credential.
630 631 632 633 634 635 636 637 |
# File 'lib/httpclient/auth.rb', line 630 def set_config(uri, config) if uri.nil? @config = config else uri = Util.uri_dirname(urify(uri)) @auth[uri] = config end end |