import tornado.web
import os, os.path
import tornado.httpserver
+import tornado.httpclient as httpclient
import sqlite3
import arrow
import datetime
from rd import RD, Link
import hashlib
+import hmac
db = None
#insert into user (name,email) values('mikael','mikael@frykholm.com');
#insert into entry (userid,text) values (1,'My thoughts on ostatus');
hub_topic = self.get_argument('hub.topic')
hub_verify = self.get_argument('hub.verify')
hub_lease_seconds = self.get_argument('hub.lease_seconds','')
- hub_secret = self.get_argument('hub.sercret','')
+ hub_secret = self.get_argument('hub.secret','')
hub_verify_token = self.get_argument('hub.verify_token','')
print(self.request.body)
if hub_mode == 'unsubscribe':
path = hub_topic.split(self.settings['domain'])[1]
user = path.split('user/')[1]
row = db.execute("select id from user where name=?",(user,)).fetchone()
- if row: #FIXME calculate expire timestamp
- db.execute("INSERT into subscriptions (userid, expires, callback, secret, verified) values (?,?,?,?)",(row['id'],datetime.datetime.now(),hub_secret,hub_callback,False))
+ expire = datetime.datetime.utcnow() + datetime.timedelta(seconds=int(hub_lease_seconds))
+ if row:
+ db.execute("INSERT into subscriptions (userid, expires, callback, secret, verified) "
+ "values (?,?,?,?,?)",(row['id'],expire,hub_callback,hub_secret,False))
db.commit()
self.set_status(202)
- #TODO add GET callback with the same data we got
+ http_client = httpclient.HTTPClient()
+ try:
+ response = http_client.fetch(hub_callback+"?hub.mode={}&hub.topic={}&hub.secret".format(hub_mode,hub_topic,hub_secret))
+ print(response.body)
+ except httpclient.HTTPError as e:
+ # HTTPError is raised for non-200 responses; the response
+ # can be found in e.response.
+ print("Error: " + str(e))
+ except Exception as e:
+ # Other errors are possible, such as IOError.
+ print("Error: " + str(e))
+ http_client.close()
#TODO add secret to outgoing feeds with hmac
class XrdHandler(tornado.web.RequestHandler):
entries = db.execute("select entry.id,text,ts from user,entry where user.id=entry.userid and user.name=?",(user,))
#import pdb;pdb.set_trace()
self.set_header("Content-Type", 'application/atom+xml')
- self.render("templates/feed.xml",
+ out = self.render("templates/feed.xml",
user=user,
feed_url="{}/user/{}".format(self.settings['domain'], user),
hub_url="{}/hub".format(self.settings['domain']),
entries=entries,
arrow=arrow )
+ #digest = hmac.new()
application = tornado.web.Application([
(r"/.well-known/host-meta", XrdHandler),
(r"/user/(.+)", UserHandler),
(r"/hub", PushHandler),
],debug=True,**settings)
-srv = tornado.httpserver.HTTPServer(application, ssl_options={
- "certfile": "ronin.frykholm.com.pem",
- "keyfile": "ronin.frykholm.com.key",
- })
+srv = tornado.httpserver.HTTPServer(application, )
def setup_db(path):
print("No db found, creating in {}".format(path))
con = sqlite3.connect(path)
setup_db(dbPath)
db = sqlite3.connect(dbPath, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
db.row_factory = sqlite3.Row
- srv.listen(443)
+ srv.listen(8080)
tornado.ioloop.IOLoop.instance().start()
+ #TODO hmac.new(b'5cc324285ece71e21e9554f4056563806f6ce0b7e4ab18d0133b602f8ba7e87a',open("apa.xml","rb").read(),digestmod='sha1').hexdigest()
+'b75d4733e0802629a0c15d0faa8de8fc9778cd05' queue runner