def generate_key(): ''' Generate a new key for user. Returns JSON-encoded username and API Key. ''' reqjson = request.get_json() username = reqjson.get('username', '') password = reqjson.get('password', '') try: user = User.get(User.username == username) except User.DoesNotExist: logging.debug('User does not exist with username of %s' % username) abort(403) if user.password == password: key = hashlib.sha1(username + password + str(datetime.now().microsecond) + SALT) apikey = ApiKey.create(owner=user.id, key=key.hexdigest()) return jsonify({'username': user.username, 'apikey': apikey.key}), 201 else: logging.debug( 'Unable to authenticate user to generate API Key for username %s' % username) abort(403)
def register(): ''' Register a new user and generate then an API Key. Returns JSON-encoded username and API Key. ''' reqjson = request.get_json() username = reqjson.get('username', '') password = reqjson.get('password', '') if not username or not password: logging.debug('Denying access for username of %s' % username) abort(400) try: user = User.create(username=username, password=password) except IntegrityError as e: logging.error('unable to register user with username %s' % username, e) abort(409) key = hashlib.sha1(username + password + str(datetime.now().microsecond) + SALT) apikey = ApiKey.create(owner=user.id, key=key.hexdigest()) logging.warn('registered user with username %s' % username) return jsonify({'username': user.username, 'apikey': apikey.key}), 201
def generate_key(): ''' Generate a new key for user. Returns JSON-encoded username and API Key. ''' reqjson = request.get_json() username = reqjson.get('username', '') password = reqjson.get('password', '') try: user = User.get(User.username == username) except User.DoesNotExist: logging.debug('User does not exist with username of %s' % username) abort(403) if user.password == password: key = hashlib.sha1(username + password + str(datetime.now().microsecond) + SALT) apikey = ApiKey.create(owner=user.id, key=key.hexdigest()) return jsonify({'username': user.username, 'apikey': apikey.key}), 201 else: logging.debug('Unable to authenticate user to generate API Key for username %s' % username) abort(403)