~kline/torgateway

9e9275834728a965eae36cee4579dbdc108dc018 — Gareth Pulham 11 months ago 6b8a2ef master
Add registration via XML-RPC
2 files changed, 32 insertions(+), 1 deletions(-)

A freenode.py
M web.py
A freenode.py => freenode.py +29 -0
@@ 0,0 1,29 @@
import xmlrpc.client

class RegistrationError(RuntimeError):
    pass

# ValueError returns with a reason, and permits token/username reuse
# RegistrationError returns a reason but requires staff intervention (stuck account)
def register_user(username, password, certfp):
    services = xmlrpc.client.ServerProxy("http://localhost:8192/xmlrpc")

    fakemail = "{}@tor.fake.mail.freenode.net".format(username)
    feedback = services.atheme.command("*", "*", "::1337", "nickserv", "register", username, password, fakemail)
    if "is now registered" not in feedback:
        raise ValueError(feedback)

    try:
        cookie = services.atheme.login(username, password)
    except xmlrpc.client.Fault as e:
        raise RegistrationError(
            "Unable to update account {}, fault {}. Please report this error and username to staff".format(
                username, e.faultCode
            )
        )
    
    feedback = services.atheme.command(cookie, username, "::1337", "nickserv", "cert", "add", certfp)
    if "Added fingerprint" not in feedback:
        token = services.atheme.command(cookie, username, "::1337", "nickserv", "drop", username).split()[-1]
        services.atheme.command(cookie, username, "::1337", "nickserv", "drop", username, token)
        raise ValueError(feedback)

M web.py => web.py +3 -1
@@ 2,6 2,8 @@ from flask import Flask, render_template, request, g, jsonify
import sqlite3
import hashcash

import freenode

hash_difficulty = 4
hash_expiry = 7 # Days



@@ 65,7 67,7 @@ def register():
        return jsonify({"status": "failure", "reason": str(e)})

    try:
        # freenode.register_user(username, password, certfp=fingerprint, tor_only=True)
        freenode.register_user(username, password, certfp=fingerprint)
        database.commit()
    except NotImplementedError as e: # registration_error
        database.rollback()