Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server now generates addresses #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions register.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# -*- coding: utf-8 -*-
import json

import random
import mongo
import command
from hashlib import sha1


class Register(command.Command):
""" Allows clients to register their address with the
server's central database. If a given address
already exists, they should regenerate and try
again with a new one, if the user requested a
registration.
"""Allows clients to register an account. The
server generates a random address and password,
registers it, and sends it back to the client.

fingerprint: {"cmd": "register", "addr": _, "pwd": _}
"""
required = ['addr', 'pwd']
fingerprint: {"cmd": "register"}
"""
required = []

def handle(self, *args, **kwargs):
addr = self.data['addr']
pwd = self.data['pwd']
if len(addr) != 40:
self.error("Registration unsuccessful")
return
if mongo.db.addresses.find_one({"addr": addr}):
self.error("That address already exists")
return

# Send error message to old clients, that used to chose the address themselves.
try:
self.data['addr'], self.data['pwd']
self.error("Registration failed. Update your client.")
except KeyError:
pass

addr = sha1(str(random.randint(0, 100000000000000))).hexdigest()
pwd = sha1(str(random.randint(0, 100000000000000))).hexdigest()

# Making sure the address doesn't exist already.
while mongo.db.addresses.find_one({"addr": addr}):
addr = sha1(str(random.randint(0, 100000000000000))).hexdigest()

mongo.db.addresses.insert({"addr": addr, "pwd": pwd})
self.success({"addr": addr})
self.success({"addr": addr, "pwd": pwd})