Class: Jabber::PubSub::OAuthServiceHelper

Inherits:
ServiceHelper show all
Defined in:
lib/xmpp4r/pubsub/helper/oauth_service_helper.rb

Overview

PubSub service helper for use with OAuth-authenticated nodes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ServiceHelper

#add_event_callback, #create_collection_node, #create_node, #delete_item_from, #delete_node, #get_affiliations, #get_config_from, #get_items_from, #get_options_from, #get_subids_for, #get_subscribers_from, #get_subscriptions_from, #get_subscriptions_from_all_nodes, #publish_item_to, #publish_item_with_id_to, #purge_items_from, #set_affiliations, #set_config_for, #set_options_for, #subscribe_to, #to_s, #unsubscribe_from

Constructor Details

#initialize(stream, pubsubjid, oauth_consumer, oauth_token, options = {}) ⇒ OAuthServiceHelper

Returns a new instance of OAuthServiceHelper



20
21
22
23
24
25
26
27
28
29
# File 'lib/xmpp4r/pubsub/helper/oauth_service_helper.rb', line 20

def initialize(stream, pubsubjid, oauth_consumer, oauth_token, options = {})
  # imbue the stream with magical OAuth signing powers
  stream.extend(OAuthPubSubStreamHelper)
  stream.oauth_consumer = oauth_consumer
  stream.oauth_token = oauth_token
  stream.oauth_options = options
  stream.pubsubjid = pubsubjid

  super(stream, pubsubjid)
end

Class Method Details

.create_oauth_node(jid, pubsubjid, oauth_consumer, oauth_token, options = {}) ⇒ Object

add the OAuth sauce (XEP-0235) The `options` hash may contain the following parameters:

:oauth_nonce            => nonce (one will be generated otherwise)
:oauth_timestamp        => timestamp (one will be generated otherwise)
:oauth_signature_method => signature method (defaults to HMAC-SHA1)
:oauth_version          => OAuth version (defaults to "1.0")


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xmpp4r/pubsub/helper/oauth_service_helper.rb', line 37

def self.create_oauth_node(jid, pubsubjid, oauth_consumer, oauth_token, options = {})
  require 'oauth'

  request = OAuth::RequestProxy.proxy \
    "method" => "iq",
    "uri"    => [jid.strip.to_s, pubsubjid.strip.to_s] * "&",
    "parameters" => {
      "oauth_consumer_key"     => oauth_consumer.key,
      "oauth_nonce"            => options[:oauth_nonce] || OAuth::Helper.generate_nonce,
      "oauth_timestamp"        => options[:oauth_timestamp] || OAuth::Helper.generate_timestamp,
      "oauth_token"            => oauth_token.token,
      "oauth_signature_method" => options[:oauth_signature_method] || "HMAC-SHA1",
      "oauth_version"          => options[:oauth_version] || "1.0"
    }

  request.sign!(:consumer => oauth_consumer, :token => oauth_token)

  # TODO create XMPPElements for OAuth elements
  oauth = REXML::Element.new("oauth")
  oauth.attributes['xmlns'] = 'urn:xmpp:oauth:0'

  oauth_consumer_key = REXML::Element.new("oauth_consumer_key")
  oauth_consumer_key.text = request.oauth_consumer_key
  oauth.add(oauth_consumer_key)

  oauth_token_node = REXML::Element.new("oauth_token")
  oauth_token_node.text = request.oauth_token
  oauth.add(oauth_token_node)

  oauth_signature_method = REXML::Element.new("oauth_signature_method")
  oauth_signature_method.text = request.oauth_signature_method
  oauth.add(oauth_signature_method)

  oauth_signature = REXML::Element.new("oauth_signature")
  oauth_signature.text = request.oauth_signature
  oauth.add(oauth_signature)

  oauth_timestamp = REXML::Element.new("oauth_timestamp")
  oauth_timestamp.text = request.oauth_timestamp
  oauth.add(oauth_timestamp)

  oauth_nonce = REXML::Element.new("oauth_nonce")
  oauth_nonce.text = request.oauth_nonce
  oauth.add(oauth_nonce)

  oauth_version = REXML::Element.new("oauth_version")
  oauth_version.text = request.oauth_version
  oauth.add(oauth_version)

  oauth
end