mikemarsian-ruby_bosh 0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e9733286d7e012ea738fe3a704149ae3570b6569
4
+ data.tar.gz: b7176f5e8a292ae00a8e27114691076946018dd7
5
+ SHA512:
6
+ metadata.gz: 8c6a252002fe19fc8293b18cbfc83607955f9dd2049239357a2c2ce6ff97349ea3ab8289e4f4decfefd74bb75d06ac37c69ea719f3108a1fa095ba8bf41722e3
7
+ data.tar.gz: dfb7c28b100e3a3fbf6cb1823d669715a052ceee6ca7018db2c776cf982877488541e593c0f9146052e0f758e40e00d57b9fddaf5cb2e3786307847475720379
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Pradeep Elankumaran
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,43 @@
1
+ ruby_bosh
2
+ =========
3
+
4
+ The RubyBOSH library handles creating and pre-authenticating BOSH streams inside your Ruby application before passing them off to your template engine.
5
+
6
+ This method allows you to hide authentication details for your users' XMPP accounts.
7
+
8
+ Tested on Rails 2.x with eJabberd 1.2+
9
+
10
+ References
11
+ ==========
12
+ BOSH: http://xmpp.org/extensions/xep-0124.html
13
+ XMPP via BOSH: http://xmpp.org/extensions/xep-0206.html
14
+
15
+ Example
16
+ =======
17
+ In your Ruby app controller (or equivalent):
18
+
19
+ @session_jid, @session_id, @session_random_id =
20
+ RubyBOSH.initialize_session("me@jabber.org", "my_password", "http://localhost:5280/http-bind")
21
+
22
+ If you want to define your own resource name, include it within the jid. RubyBOSH will create a random one if none is supplied.
23
+ To define a resource name of 'home', do the following:
24
+
25
+ @session_jid, @session_id, @session_random_id =
26
+ RubyBOSH.initialize_session("me@jabber.org/home", "my_password", "http://localhost:5280/http-bind")
27
+
28
+ In your template, you would then pass these directly to your javascript BOSH connector:
29
+
30
+ var bosh_jid = '<%= @session_jid %>';
31
+ var bosh_sid = '<%= @session_id %>';
32
+ var bosh_rid = '<%= @session_random_id %>';
33
+
34
+ // using Strophe:
35
+ connect.attach(bosh_jid, bosh_sid, bosh_rid, onConnectHandlerFunction);
36
+
37
+ Acknowledgements
38
+ ================
39
+ Jack Moffit
40
+ - thanks for the nice Django example :)
41
+ #=> http://metajack.im/2008/10/03/getting-attached-to-strophe/
42
+
43
+ Copyright (c) 2008 Pradeep Elankumaran. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
8
+ task :test => :spec
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ write basic tests
2
+
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 11
4
+ :patch: 0
5
+ :build:
@@ -0,0 +1,5 @@
1
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
2
+
3
+ Autotest.add_discovery do
4
+ "rspec"
5
+ end
data/lib/ruby_bosh.rb ADDED
@@ -0,0 +1,180 @@
1
+ require 'rest_client'
2
+ require 'builder'
3
+ require 'rexml/document'
4
+ require 'base64'
5
+ require 'hpricot'
6
+
7
+ class RubyBOSH
8
+ BOSH_XMLNS = 'http://jabber.org/protocol/httpbind'
9
+ TLS_XMLNS = 'urn:ietf:params:xml:ns:xmpp-tls'
10
+ SASL_XMLNS = 'urn:ietf:params:xml:ns:xmpp-sasl'
11
+ BIND_XMLNS = 'urn:ietf:params:xml:ns:xmpp-bind'
12
+ SESSION_XMLNS = 'urn:ietf:params:xml:ns:xmpp-session'
13
+ CLIENT_XMLNS = 'jabber:client'
14
+
15
+ class Error < StandardError; end
16
+ class TimeoutError < RubyBOSH::Error; end
17
+ class AuthFailed < RubyBOSH::Error; end
18
+ class ConnFailed < RubyBOSH::Error; end
19
+
20
+ @@logging = true
21
+ def self.logging=(value)
22
+ @@logging = value
23
+ end
24
+
25
+ attr_accessor :jid, :rid, :sid, :success , :custom_resource
26
+ def initialize(jid, pw, service_url, opts={})
27
+ @service_url = service_url
28
+ # Extract the resource if present
29
+ split_jid = jid.split("/")
30
+ @jid = split_jid.first
31
+ @custom_resource = split_jid.last if split_jid.length > 1
32
+ @pw = pw
33
+ @host = @jid.split("@").last
34
+ @success = false
35
+ @timeout = opts[:timeout] || 3 #seconds
36
+ @headers = {"Content-Type" => "text/xml; charset=utf-8",
37
+ "Accept" => "text/xml"}
38
+ @wait = opts[:wait] || 5
39
+ @hold = opts[:hold] || 3
40
+ @window = opts[:window] || 5
41
+ end
42
+
43
+ def success?
44
+ @success == true
45
+ end
46
+
47
+ def self.initialize_session(*args)
48
+ new(*args).connect
49
+ end
50
+
51
+ def connect
52
+ initialize_bosh_session
53
+ if send_auth_request
54
+ send_restart_request
55
+ request_resource_binding
56
+ @success = send_session_request
57
+ end
58
+
59
+ raise RubyBOSH::AuthFailed, "could not authenticate #{@jid}" unless success?
60
+ @rid += 1 #updates the rid for the next call from the browser
61
+
62
+ [@jid, @sid, @rid]
63
+ end
64
+
65
+ private
66
+ def initialize_bosh_session
67
+ response = deliver(construct_body(:wait => @wait, :to => @host,
68
+ :hold => @hold, :window => @window,
69
+ "xmpp:version" => '1.0'))
70
+ parse(response)
71
+ end
72
+
73
+ def construct_body(params={}, &block)
74
+ @rid ? @rid+=1 : @rid=rand(100000)
75
+
76
+ builder = Builder::XmlMarkup.new
77
+ parameters = {:rid => @rid, :xmlns => BOSH_XMLNS,
78
+ "xmpp:version" => "1.0",
79
+ "xmlns:xmpp" => "urn:xmpp:xbosh"}.merge(params)
80
+
81
+ if block_given?
82
+ builder.body(parameters) {|body| yield(body)}
83
+ else
84
+ builder.body(parameters)
85
+ end
86
+ end
87
+
88
+ def send_auth_request
89
+ request = construct_body(:sid => @sid) do |body|
90
+ auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}"
91
+ body.auth(Base64.encode64(auth_string).gsub(/\s/,''),
92
+ :xmlns => SASL_XMLNS, :mechanism => 'PLAIN')
93
+ end
94
+
95
+ response = deliver(request)
96
+ response.include?("success")
97
+ end
98
+
99
+ def send_restart_request
100
+ request = construct_body(:sid => @sid, "xmpp:restart" => true, "xmlns:xmpp" => 'urn:xmpp:xbosh')
101
+ deliver(request).include?("stream:features")
102
+ end
103
+
104
+ def request_resource_binding
105
+ request = construct_body(:sid => @sid) do |body|
106
+ body.iq(:id => "bind_#{rand(100000)}", :type => "set",
107
+ :xmlns => "jabber:client") do |iq|
108
+ iq.bind(:xmlns => BIND_XMLNS) do |bind|
109
+ bind.resource(resource_name)
110
+ end
111
+ end
112
+ end
113
+
114
+ response = deliver(request)
115
+ response.include?("<jid>")
116
+ end
117
+
118
+ def send_session_request
119
+ request = construct_body(:sid => @sid) do |body|
120
+ body.iq(:xmlns => CLIENT_XMLNS, :type => "set",
121
+ :id => "sess_#{rand(100000)}") do |iq|
122
+ iq.session(:xmlns => SESSION_XMLNS)
123
+ end
124
+ end
125
+
126
+ response = deliver(request)
127
+ response.include?("body")
128
+ end
129
+
130
+ def parse(_response)
131
+ doc = Hpricot(_response.to_s)
132
+ doc.search("//body").each do |body|
133
+ @sid = body.attributes["sid"].to_s
134
+ end
135
+ _response
136
+ end
137
+
138
+ begin
139
+ require "timeout"
140
+ def deliver(xml)
141
+ ::Timeout.timeout(@timeout) do
142
+ send(xml)
143
+ recv(RestClient.post(@service_url, xml, @headers))
144
+ end
145
+ rescue Timeout::Error => e
146
+ raise RubyBOSH::TimeoutError, e.message
147
+ rescue Errno::ECONNREFUSED => e
148
+ raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}"
149
+ rescue Exception => e
150
+ raise RubyBOSH::Error, e.message
151
+ end
152
+ end
153
+
154
+ def send(msg)
155
+ puts("Ruby-BOSH - SEND\n[#{now}]: #{msg}") if @@logging; msg
156
+ end
157
+
158
+ def recv(msg)
159
+ puts("Ruby-BOSH - RECV\n[#{now}]: #{msg}") if @logging; msg
160
+ end
161
+
162
+ private
163
+ def now
164
+ Time.now.strftime("%a %b %d %H:%M:%S %Y")
165
+ end
166
+
167
+ def resource_name
168
+ if @custom_resource.nil?
169
+ "bosh_#{rand(10000)}"
170
+ else
171
+ @custom_resource
172
+ end
173
+ end
174
+ end
175
+
176
+
177
+ if __FILE__ == $0
178
+ p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
179
+ "http://localhost:5280/http-bind")
180
+ end
data/ruby_bosh.gemspec ADDED
@@ -0,0 +1,43 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mikemarsian-ruby_bosh"
3
+ s.version = "0.13"
4
+
5
+ s.require_paths = ["lib"]
6
+ s.authors = ["Original author: Pradeep Elankumaran. Update for ruby 2: Mike Polischuk"]
7
+ s.date = "2016-07-27"
8
+ s.description = "An XMPP BOSH session pre-initializer for Ruby web applications"
9
+ s.email = "mike@polischuk.com"
10
+ s.extra_rdoc_files = [
11
+ "LICENSE",
12
+ "README",
13
+ "TODO"
14
+ ]
15
+ s.licenses = ['MIT']
16
+ s.files = [
17
+ "LICENSE",
18
+ "README",
19
+ "Rakefile",
20
+ "TODO",
21
+ "VERSION.yml",
22
+ "autotest/discover.rb",
23
+ "lib/ruby_bosh.rb",
24
+ "ruby_bosh.gemspec",
25
+ "spec/ruby_bosh_spec.rb",
26
+ "spec/spec_helper.rb"
27
+ ]
28
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
29
+ s.homepage = "http://github.com/mikemarsian/ruby_bosh"
30
+ s.summary = "A BOSH session pre-initializer for Ruby web applications (for Ruby 2)"
31
+ s.required_ruby_version = '>= 1.9.3'
32
+
33
+ s.add_development_dependency "bundler", "~> 1.10"
34
+ s.add_development_dependency "rake", "~> 10.0"
35
+ s.add_development_dependency "rspec", '~> 3.0'
36
+ s.add_development_dependency "rspec_junit_formatter", '0.2.2'
37
+
38
+ s.add_dependency "builder", "~> 3.0"
39
+ s.add_dependency "rest-client" , "~> 1.8"
40
+ s.add_dependency "hpricot", "~> 0.8"
41
+
42
+ end
43
+
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe RubyBOSH do
4
+ before(:each) do
5
+ RubyBOSH.logging = false
6
+ @rbosh = RubyBOSH.new("skyfallsin@localhost", "skyfallsin",
7
+ "http://localhost:5280/http-bind")
8
+
9
+ @rbosh.stub(:send_auth_request).and_return(true)
10
+ @rbosh.stub(:send_restart_request).and_return(true)
11
+ @rbosh.stub(:request_resource_binding).and_return(true)
12
+ @rbosh.stub(:send_session_request).and_return(true)
13
+ RestClient.stub(:post).and_return("<body sid='123456'></body>")
14
+ end
15
+
16
+ it "should set the sid attribute after the session creation request" do
17
+ @rbosh.connect
18
+ @rbosh.sid.should == '123456'
19
+ end
20
+
21
+ it "should update the rid on every call to the BOSH server" do
22
+ @rbosh.rid = 100
23
+ @rbosh.connect
24
+ @rbosh.rid.should > 100
25
+ end
26
+
27
+ it "should return an array with [jid, sid, rid] on success" do
28
+ s = @rbosh.connect
29
+ s.should be_kind_of(Array)
30
+ s.size.should == 3
31
+ s.first.should == 'skyfallsin@localhost'
32
+ s.last.should be_kind_of(Fixnum)
33
+ s[1].should == '123456'
34
+ end
35
+
36
+ describe "Errors" do
37
+ it "should crash with AuthFailed when its not a success?" do
38
+ @rbosh.stub(:send_session_request).and_return(false)
39
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::AuthFailed)
40
+ end
41
+
42
+ it "should raise a ConnFailed if a connection could not be made to the XMPP server" do
43
+ RestClient.stub(:post).and_raise(Errno::ECONNREFUSED)
44
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::ConnFailed)
45
+ end
46
+
47
+ it "should crash with a generic error on any other problem" do
48
+ [RestClient::ServerBrokeConnection, RestClient::RequestTimeout].each{|err|
49
+ RestClient.stub(:post).and_raise(err)
50
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::Error)
51
+ }
52
+ end
53
+
54
+ after(:each) do
55
+ lambda { @rbosh.connect }.should raise_error(RubyBOSH::Error)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require File.join(File.dirname(__FILE__), '..', "lib", "ruby_bosh")
4
+
5
+ RSpec.configure do |config|
6
+
7
+ config.expect_with :rspec do |c|
8
+ c.syntax = :should
9
+ end
10
+ config.mock_with :rspec do |c|
11
+ c.syntax = :should
12
+ end
13
+
14
+ end
15
+
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mikemarsian-ruby_bosh
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.13'
5
+ platform: ruby
6
+ authors:
7
+ - 'Original author: Pradeep Elankumaran. Update for ruby 2: Mike Polischuk'
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_junit_formatter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.2.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.2.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: builder
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rest-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: hpricot
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description: An XMPP BOSH session pre-initializer for Ruby web applications
112
+ email: mike@polischuk.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files:
116
+ - LICENSE
117
+ - README
118
+ - TODO
119
+ files:
120
+ - LICENSE
121
+ - README
122
+ - Rakefile
123
+ - TODO
124
+ - VERSION.yml
125
+ - autotest/discover.rb
126
+ - lib/ruby_bosh.rb
127
+ - ruby_bosh.gemspec
128
+ - spec/ruby_bosh_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://github.com/mikemarsian/ruby_bosh
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 1.9.3
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.2.2
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A BOSH session pre-initializer for Ruby web applications (for Ruby 2)
154
+ test_files:
155
+ - spec/ruby_bosh_spec.rb
156
+ - spec/spec_helper.rb
157
+ has_rdoc: