<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,5 +1,6 @@
 helpers do
   # verify subscribers callback
+  # TODO: use it in /hub/subscribe
   def do_verify(url, data)
     return false unless url and data
     begin
@@ -40,7 +41,8 @@ post '/hub/publish' do
     if topic.first
       topic.update(:updated =&gt; Time.now)
     else  
-      DB[:channels] &lt;&lt; { :name =&gt; id, :created =&gt; Time.now, :type =&gt; 'pubsubhubbub' }
+      DB[:channels] &lt;&lt; { :name =&gt; id, :created =&gt; Time.now, 
+                         :type =&gt; 'pubsubhubbub', :secret =&gt; 'change_me' }
     end  
   rescue Exception =&gt; e
     puts e.to_s
@@ -55,46 +57,32 @@ end
 # return hub.challenge
 get '/hub/callback' do
   id = [params['hub.topic']].pack(&quot;m*&quot;).strip
-  topic = DB[:channels].filter(:name =&gt; id)
-  respond &quot;404 Not Found&quot;, 404  unless topic.first
+  topic = DB[:channels].filter(:name =&gt; id).first
+  unless topic
+    status 404
+    return &quot;404 Not Found&quot;
+  end  
   if request.post?
     postman(id, atom_parse(request.body.string)).to_json
     {:status =&gt; 'OK'}.to_json
   else
-    rec_all = DB[:users].filter(:name =&gt; 'all', :service =&gt; 'channels').first
-    respond &quot;404 Not Found&quot;, 404 unless rec_all
-    # secret per channel
-    rec = DB[:users].filter(:name =&gt; &quot;#{id}&quot;, :service =&gt; 'channels').first
-    rec = rec_all unless rec
-    respond &quot;404 Not Found&quot;, 404 unless request['hub.verify_token'] == rec[:password]
+    unless request['hub.verify_token'] == topic[:secret]
+      status 404
+      return &quot;404 Not Found&quot;
+    end
     request['hub.challenge']
   end  
 end 
 
-get '/hub/verify' do
-  topic, secret = params[:topic], params[:secret]
-  unless topic and secret
-    status 400
-    return &quot;400 Bad request: need url and secret parameters&quot;
-  end
-  data = { :mode =&gt; 'subscribe', :verify =&gt; 'sync', :vtoken =&gt; secret, :url =&gt; topic }
-  unless do_verify(&quot;http://localhost:4567/hub/callback&quot;, data)
-    status 409
-    return &quot;409 Subscription verification failed&quot;
-  end
-  status 204
-  &quot;204 No Content&quot;
-end  
 
 # Check ownership - response body is the superfeedr secret token
 get '/superfeedr' do
-  begin
-    rec = DB[:channels].filter(:type =&gt; 'superfeedr').order(:created).last
-    raise &quot;'superfeedr' type topic does not exists&quot; unless rec[:id]
-    rec[:name]
-  rescue Exception =&gt; e  
-    {:status =&gt; e.to_s}.to_json
-  end  
+  rec = DB[:channels].filter(:type =&gt; 'superfeedr').order(:created).last
+  unless rec
+    status 404
+    return &quot;404 Not Found&quot;
+  end   
+  rec[:name]
 end  
 
 post '/superfeedr' do</diff>
      <filename>hubbub.rb</filename>
    </modified>
    <modified>
      <diff>@@ -2,18 +2,32 @@ require 'rubygems'
 require 'rest_client'
 require 'json'
 
-# Need a test user created
-# DB[:users] &lt;&lt; { :name =&gt; 'test', :password =&gt; 'test', :service =&gt; 'self' }
 puts &quot;creating channel...&quot;
-resp = RestClient.post 'http://test:test@localhost:4567/channels', :data =&gt; ''
+resp = RestClient.post 'http://admin:change_me@localhost:4567/channels', :data =&gt; ''
 id = JSON.parse(resp)[&quot;id&quot;]
 
 puts &quot;adding subscribers to channel #{id}&quot;
 resp = RestClient.post 'http://localhost:4567/subscribe', :data =&gt; { :channel =&gt; id, :url =&gt; 'http://localhost:8080/test-handler' }.to_json
 puts resp
+
 resp = RestClient.post 'http://localhost:4567/subscribe', :data =&gt; { :channel =&gt; id, :url =&gt; 'http://localhost:8080/slow-handler' }.to_json
 puts resp
 
 puts &quot;posting message to #{id}&quot;
 resp = RestClient.post 'http://localhost:4567/publish', :payload =&gt; { :channel =&gt; id, :message =&gt; 'HAYYYY' }.to_json
 puts resp
+
+# protected channels
+puts &quot;creating protected channel...&quot;
+resp = RestClient.post 'http://admin:change_me@localhost:4567/channels', :data =&gt; {:secret =&gt; 'secret123'}.to_json
+id = JSON.parse(resp)[&quot;id&quot;]
+
+# will fail
+puts &quot;adding subscribers to channel #{id}&quot;
+resp = RestClient.post 'http://localhost:4567/subscribe', :data =&gt; { :channel =&gt; id, :url =&gt; 'http://localhost:8080/test-handler' }.to_json
+puts resp
+
+# will success
+puts &quot;adding subscribers to protected channel #{id}&quot;
+resp = RestClient.post 'http://localhost:4567/subscribe', :data =&gt; { :channel =&gt; id, :url =&gt; 'http://localhost:8080/test-handler', :secret =&gt; 'secret123' }.to_json
+puts resp</diff>
      <filename>tools/example.rb</filename>
    </modified>
    <modified>
      <diff>@@ -22,14 +22,18 @@ configure do
       varchar :name, :size =&gt; 128
       # channel types: 'github', 'pingfm', 'superfeedr' etc.
       varchar :type, :size =&gt; 32, :default =&gt; 'seq'
+      # protected channels
+      varchar :secret, :size =&gt; 32
       time    :created
       time    :updated
       index   [:updated]
       index   [:name], :unique =&gt; true
     end
     # system channels
-    DB[:channels] &lt;&lt; { :name =&gt; '__pingfm__', :created =&gt; Time.now, :type =&gt; 'pingfm' }
-    DB[:channels] &lt;&lt; { :name =&gt; '__github__', :created =&gt; Time.now, :type =&gt; 'github' }
+    DB[:channels] &lt;&lt; { :name =&gt; '__pingfm__', :created =&gt; Time.now, 
+                       :type =&gt; 'pingfm', :secret =&gt; 'change_me' }
+    DB[:channels] &lt;&lt; { :name =&gt; '__github__', :created =&gt; Time.now, 
+                       :type =&gt; 'github', :secret =&gt; 'change_me' }
   end
 
   unless DB.table_exists? &quot;subscribers&quot;
@@ -60,8 +64,6 @@ configure do
     DB[:users] &lt;&lt; { :name =&gt; 'all', :password =&gt; 'change_me', :service =&gt; 'hooks' }
     # secret per hook
     # DB[:users] &lt;&lt; { :name =&gt; 'ff', :password =&gt; 'change_me_too', :service =&gt; 'hooks' }
-    # channel protection - for PubSubHubbub subscribers
-    DB[:users] &lt;&lt; { :name =&gt; 'all', :password =&gt; 'change_me', :service =&gt; 'channels' }
   end 
 end
 
@@ -163,11 +165,14 @@ post '/channels' do
     # superfeedr api key
     id = data['id'] if data['id'] &amp;&amp; (data['type'] == 'superfeedr')
     type = data['type'] || 'seq'
-  rescue
+    secret = data['secret']
+  rescue Exception =&gt; e
+    puts e.to_s
     type = 'seq'
-  end 
+  end
   unless DB[:channels].filter(:name =&gt; id).first
-    DB[:channels] &lt;&lt; { :name =&gt; id, :created =&gt; Time.now, :type =&gt; type }
+    DB[:channels] &lt;&lt; { :name =&gt; id, :created =&gt; Time.now, 
+                       :type =&gt; type, :secret =&gt; secret }
   end  
   { :id =&gt; id.to_s }.to_json
 end
@@ -180,7 +185,11 @@ post '/subscribe' do
     type = data['type'] || 'debug'
     ['url', 'channel', 'type'].each { |d| data.delete(d) }
     rec = DB[:channels].filter(:name =&gt; channel_name).first
-    raise &quot;channel #{channel_name} does not exists&quot; unless rec[:id]  
+    raise &quot;channel #{channel_name} does not exists&quot; unless rec[:id]
+    if rec[:secret]
+      raise &quot;not authorized&quot; unless rec[:secret] == data['secret']
+      data.delete('secret')
+    end  
     unless DB[:subscribers].filter(:channel_id =&gt; rec[:id], :url =&gt; url).first
       raise &quot;DB insert failed&quot; unless DB[:subscribers] &lt;&lt; { 
                                             :channel_id =&gt; rec[:id], </diff>
      <filename>watercoolr.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>c8cebb048238c39f3e8c43d174a4966c625f521e</id>
    </parent>
  </parents>
  <author>
    <name>Stoyan Zhekov</name>
    <email>zh@zhware.net</email>
  </author>
  <url>http://github.com/zh/watercoolr/commit/d041e64c29bb31375d58fb309eff85a7f8afa26c</url>
  <id>d041e64c29bb31375d58fb309eff85a7f8afa26c</id>
  <committed-date>2009-07-10T02:37:33-07:00</committed-date>
  <authored-date>2009-07-10T02:37:33-07:00</authored-date>
  <message>support for protected channels</message>
  <tree>47515a64c12b4c7117e6d9e019468e3aec9cfbbf</tree>
  <committer>
    <name>Stoyan Zhekov</name>
    <email>zh@zhware.net</email>
  </committer>
</commit>
