initial, untested commit

master
Tomek Dubrownik 2012-09-18 02:50:13 +02:00
commit cbc5a41893
3 changed files with 125 additions and 0 deletions

15
socialist.cfg.dist Normal file
View File

@ -0,0 +1,15 @@
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
CAP_URL = 'https://capacifier.hackerspace.pl'
AUTH_URL = 'https://auth.hackerspace.pl'
API_AUTHORIZED = [
"/C=PL/ST=Mazowieckie/L=Warsaw/O=Hackerspace Warsaw/OU=API/CN=ood-web/name=ood-web/emailAddress=bofh@hackerspace.pl",
"/C=PL/ST=Mazowieckie/L=Warsaw/O=Hackerspace Warsaw/OU=API/CN=patologia-new/name=itanic/emailAddress=bofh@hackerspace.pl",
]
TWITTER_ROLE = 'twitter'

104
socialist.py Normal file
View File

@ -0,0 +1,104 @@
import json
import urllib
import requests
import functools
import oauth2 as oauth
from flask import Flask, request, abort, jsonify, render_template
app = Flask('socialist')
app.config.from_pyfile('socialist.cfg')
consumer_secret = app.config['CONSUMER_SECRET']
consumer_key = app.config['CONSUMER_KEY']
oauth_token = app.config['OAUTH_TOKEN']
oauth_token_secret = app.config['OAUTH_TOKEN_SECRET']
auth_url = app.config['AUTH_URL']
cap_url = app.config['CAP_URL']
api_allowed = app.config['API_AUTHORIZED']
twitter_role = app.config['TWITTER_ROLE']
def api_access(f):
@functools.wraps(f)
def func(*a, **kw):
dn = request.environ.get('PEER_DN')
if dn not in api_allowed:
return abort(403)
return f(*a, **kw)
return func
def get_login():
return request.form['login']
def plain_error(text='Unauthorized', code=401):
return make_response(text, error, { 'Content-Type': 'text/plain' })
def require_role(role, login_source = get_login, error_func = plain_error):
def decorator(f):
@functools.wraps(f)
def func(*a, **kw):
res = requests.get('/'.join((cap_url, role, get_login())))
if res.code == 200:
return f(*a, **kw)
else:
return error_func()
return func
return decorator
def irc_dereference(from_parameter = 'nick', to_parameter = 'login', error_func = plain_error):
def decorator(f):
@functools.wraps(f)
def func(*a, **kw):
res = requests.post(auth_url + '/irc',
data = { 'nick': requests.form[from_parameter] })
if res.code == 200:
requests.form[to_parameter] = res.text
return f(*a, **kw)
else:
return error_func()
return func
return decorator
def oauth_req(url, http_method = 'GET', data = {}, headers = None):
# za https://dev.twitter.com/docs/auth/oauth/single-user-with-examples#python
from urllib import urlencode
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret)
token = oauth.Token(key=oauth_token, secret=oauth_token_secret)
client = oauth.Client(consumer, token)
return client.request(
url,
method = http_method,
body = urlencode(data),
headers = headers)
def post_update(entry, format_str, title_shortener, max_title_length):
title = title_shortener(get_text(entry, 'title'), max_title_length)
try:
author = get_text(get_first_child(entry, 'author'), 'name')
except IndexError:
author = 'Somebody'
url = get_first_child(entry, 'link').attributes['href'].value
text = format_str % dict(title=title, author=author, url=url)
logger.info('posting update (text: "%s")' % text)
oauth_req('https://api.twitter.com/1/statuses/update.json', 'POST',
{ 'status': text })
@app.route('/')
def motd():
return render_template('motd.html')
@app.route('/socialist/tweet', methods=['GET'])
def test_form():
return render_template('tweet_form.html')
@app.route('/socialist/tweet', methods=['POST'])
@api_access
@irc_dereference(error_func = lambda: plain_error('Unknown nick'))
@require_role(twitter_role, error_func = lambda: plain_error('Unauthorized to tweet'))
def irc_tweet():
pass
if __name__ == '__main__':
app.run('0.0.0.0', port=8083, debug=True)

6
templates/motd.html Normal file
View File

@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
<h1>Socialist (.hackerspace.pl)</h1>
</body>
</html>