def test_password_check(self): auth.add_user("test_user", "password") # check_credentials returns None if there is no error, # or returns error string self.assertIsNone(auth.check_credentials("test_user", "password")) self.assertIsNotNone(auth.check_credentials("test_user", "wrong"))
def test_add_user(): user = ('evert', 'de man', '6v"&?5UXx-', 10173) with open(pwdb_path, 'wb+') as right_file: auth.add_user(user[0], user[1], user[2], db_dict_right, right_file) assert db_dict_right[user[0]] == (user[3], user[2]), 'Dict is not right in memory' with open(pwdb_path, 'rb+') as right_file: assert auth.read_pwdb(right_file) == db_dict_right
def add_user(args): print("Set password for user {}".format(args.user_name)) password = getpass.getpass() confirm = getpass.getpass(prompt='Confirm password: '******'Error: passwords do not match') else: auth.init() auth.add_user(args.user_name, password) print('User database updated')
def test_add_existing_user(): # get an existing user from our database user = db_right[-1] flag = False with open(pwdb_path, 'wb+') as right_file: try: auth.add_user(user[0], user[1], user[2], db_dict_right, right_file) except: flag = True assert flag
def test_user_already_exists(): username = '******' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME pwdb_file = open(pwdb_path, 'wb') pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: auth.add_user(username, password, salt, pwdb, pwdb_file) assert False except Exception as _: assert True
def test_empty_username(): username = '' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME with open(pwdb_path, 'wb+') as pwdb_file: pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: with open(pwdb_path, 'wb+') as pwdb_file: auth.add_user(username, password, salt, pwdb, pwdb_file) assert False except auth.UsernameError as _: assert True
def add_user(): data = request.get_json() if auth.check_permission(data["token"], "manage_users"): return auth.add_user(data["user_to_add"], data["password_of_user"], data["permissions"]) else: return jsonify({"message": "No permission!"}), 401
def sign_up(): try: username = request.json['user'] password = request.json['pass'] email = request.json['email'] return auth.add_user(username, password, email) except: return jsonify(success=False, reason='Could not sign up due to a Server Error')
def create_user(*args, **kwargs): print("*** Creating a new user ***") usr = input("Enter username: "******"Enter password: "******"Re-enter password: "******"Passwords don't match!") return if usr in auth.users: if not (input("User already exists. Replace user/pwd? [y/n]: ").lower( ).startswith("y")): print("No changes have been made.") return auth.add_user(usr, pwd) print(f"User, {usr}, added.")
def test_user_not_exists(): username = '******' password = '******' salt = auth.get_salt() pwdb = {'old_name': (auth.pwhash('old_password', salt), salt)} pwdb_path = tempfile.gettempdir() / PWDB_FLNAME with open(pwdb_path, 'wb+') as pwdb_file: pickle.dump(pwdb, pwdb_file) salt = auth.get_salt() try: with open(pwdb_path, 'wb+') as pwdb_file: auth.add_user(username, password, salt, pwdb, pwdb_file) with open(pwdb_path, 'rb+') as pwdb_file: pwdb = pickle.load(pwdb_file) print(pwdb) assert pwdb[username] == (auth.pwhash(password, salt), salt) except: assert False
def test_add_user(pwdb_path): username = '******' password = '******' try: pwdb_file = open(pwdb_path, 'rb+') except FileNotFoundError: pwdb_file = open(pwdb_path, 'wb+') pwdb = read_pwdb(pwdb_file) TEST_PWDB_FLNAME_COPY = tempfile.gettempdir() / pathlib.Path('test_pwdb_copy.pkl') pwdb_file_copy = open(TEST_PWDB_FLNAME_COPY, 'wb+') # creating a copy of the original test database # so that changes don't affect the original test database add_user(username, password, get_salt(), pwdb, pwdb_file_copy) assert username in pwdb
def register(): if request.method == 'GET': return render_template('register.html') elif request.method == 'POST': user = request.form['username'] if not auth.add_user(user, request.form['password']): return "Username already in use. Please pick another." + render_template( 'register.html') else: session['username'] = user return redirect('/registered')
def process_form(self, username=None, name=None, email=None, password=None, **kwargs): msg = Message() error = add_user(username, password, name, email, False) if error: msg.text = error else: msg.add = _("%s saved." % username) cfg.log(msg.text) main = self.main(username, name, email, msg=msg.text) return self.fill_template(title="Manage Users and Groups", main=main, sidebar_left=self.sidebar_left, sidebar_right=self.sidebar_right)
def real(environ, start_response): message = check_request(environ,start_response) query = pq(environ[QUERY]) if message == "": body = environ[BODY].read().decode("utf-8") body = json.loads(body) if 'google_token' not in body: start_response('400 Bad Request', [('Content-Type', 'json')]) message = json.dumps({ STATUS: FAILED, MESSAGE: "Improper request no google_token in body" }) else: token = body['google_token'] idinfo = google_auth(token) if idinfo is None: start_response('400 Bad Request', [('Content-Type', 'json')]) message = json.dumps({ STATUS: FAILED, MESSAGE: "Not valid google token" }) else: try: netId = idinfo['email'].split("@")[0] add_user(netId) session = start_session(netId) start_response('200 OK', [('Content-Type', 'json')]) message = json.dumps( { STATUS: SUCCESS, MESSAGE: session }) except: print_exc() start_response('400 Bad Request', [('Content-Type', 'json')]) message = json.dumps({ STATUS: FAILED, MESSAGE: "Error in Database, check your code" }) print(message) return [message.encode()]
def post(self): logging.debug('%s: post=> %s' % (self.__class__.__name__, self.request.POST)) status = 0 if not users.is_current_user_admin(): status = 401 else: user = auth.add_user(username = self.request.get('username_'), is_superuser = self.request.get('is_superuser_') ) if (status == 0): status = 200 self.response.set_status(status) self.redirect('/admin/') return status
def post(self): logging.debug('%s: post=> %s' % (self.__class__.__name__, self.request.POST)) status = 0 if not users.is_current_user_admin(): status = 401 else: user = auth.add_user( username=self.request.get('username_'), is_superuser=self.request.get('is_superuser_')) if (status == 0): status = 200 self.response.set_status(status) self.redirect('/admin/') return status
if __name__ == "__main__": (conffile, argport) = parse_args(sys.argv[1:]) conf = config.TorrentDownloaderConfig(conffile) (port, save_path, users) = (conf.port, conf.save_path, conf.users) if argport: port = argport if not len(users): print "Error: no users in %s config file" % conffile sys.exit(1) for (user, passwd) in users: auth.add_user(user, passwd) add_handler = taddhandler.TAddHandler(page_template.success) if save_path: add_handler.set_save_path(save_path) add_handler.set_file_exists_template(page_template.file_exists) handlers = { "*": tanyhandler.TAnyHandler(page_template.index), "auth": tauthhandler.TAuthHandler(""), "torrentadd": add_handler, } (login, password) = get_rutracker_authdata() browser = rt_browser.RutrackerBrowser(login, password)
def test_password_too_long(self): password = "******" * 4097 self.assertIsNotNone(auth.add_user("test_user", password)) self.assertIsNotNone(auth.check_credentials("test_user", password))
def test_salt_is_random(self): auth.add_user("test_user1", "password") auth.add_user("test_user2", "password") self.assertNotEqual(cfg.users["test_user1"]["salt"], cfg.users["test_user2"]["salt"])
def add_user(): username = raw_input('User Name:') password = getpass.getpass() auth_key = raw_input('Auth Key:') auth.add_user(username, password, auth_key) print("A user is added: {0}".format(username))
def test_hash_is_random(self): auth.add_user("test_user1", "password") auth.add_user("test_user2", "password") self.assertNotEqual(cfg.users["test_user1"]["passphrase"], cfg.users["test_user2"]["passphrase"])
def sign_up(): try: return auth.add_user(**request.json) except: return jsonify(success=False, reason='Could not sign up due to a Server Error'), 500
if __name__ == '__main__': (conffile, argport) = parse_args(sys.argv[1:]) conf = config.TorrentDownloaderConfig(conffile) (port, save_path, users) = (conf.port, conf.save_path, conf.users) if argport: port = argport if not len(users): print 'Error: no users in %s config file' % conffile sys.exit(1) for (user, passwd) in users: auth.add_user(user, passwd) add_handler = taddhandler.TAddHandler(page_template.success) if save_path: add_handler.set_save_path(save_path) add_handler.set_file_exists_template(page_template.file_exists) handlers = { '*': tanyhandler.TAnyHandler(page_template.index), 'auth': tauthhandler.TAuthHandler(''), 'torrentadd': add_handler } (login, password) = get_rutracker_authdata() browser = rt_browser.RutrackerBrowser(login, password)
def test_add_user_user_exists(): with pytest.raises(Exception) as e_info: au.add_user("John Doe", None, None, {"John Doe": ["", ""]}, None) assert str(e_info.value) == 'Username already exists [John Doe]'
def test_add_user_new_user(): pwdb = {} with au.tempfile.TemporaryFile() as tmp_f: au.add_user("John Doe", "password", "SALT", pwdb, tmp_f) assert pwdb == {"John Doe": (au.pwhash("password", "SALT"), "SALT")}
def test_add_user(self): self.assertIsNone(auth.add_user("test_user", "password")) self.assertIsNotNone(auth.add_user(None, "password")) self.assertIsNotNone(auth.add_user("test_user", None)) self.assertIsNotNone(auth.add_user("test_user", "password"))