def get_tree_workspace(request): passphrase = request.headers['passphrase'] workspace_id = request.GET.get('workspace_id') if not workspace_id: return JsonResponse( { 'status': False, 'error': _("Please define a workspace ID") }, status=400) workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.get_tree(passphrase) if not status['status']: return JsonResponse({ 'status': False, 'error': status['error'] }, status=500) folders = json.loads(status['folders']) rights = 'Read & Write' if status['rights'] == 1: rights = 'Readonly' return JsonResponse({'status': True, 'tree': folders, 'rights': rights})
def add_key(request): passphrase = request.headers['passphrase'] session_key = request.session['key'] workspace_id = request.POST.get('workspace_id') if not workspace_id: return JsonResponse( { 'status': False, 'error': _("Please define a workspace ID") }, status=400) key = { "id": None, "name": request.POST['name'], "login": request.POST['login'], "password": request.POST['password'], "uri": request.POST.get('uri'), "ipv4": request.POST.get('ipv4'), "ipv6": request.POST.get('ipv6'), "informations": request.POST.get('informations', ""), "folder": request.POST['folder_id'], } workspace_utils = WorkspaceUtils(request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.save_key(key, passphrase) if not status: return JsonResponse({'status': False, 'error': error}, status=500) return JsonResponse({'status': True}, status=201)
def get_keys(request): passphrase = request.headers['passphrase'] workspace_id = request.GET.get('workspace_id') folder_id = request.GET.get('folder_id') if not workspace_id: return JsonResponse( { 'status': False, 'error': _("Please define a workspace ID") }, status=400) if not folder_id: return JsonResponse( { 'status': False, 'error': _("Please define a folder ID") }, status=400) workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.get_keys(passphrase, folder_id, api=True) if not status['status']: return JsonResponse({ 'status': False, 'error': status['error'] }, status=500) return JsonResponse(status)
def workspace_create(request): name = request.POST['name'] workspace_utils = WorkspaceUtils(request.user) status = workspace_utils.create_workspace(name) if not status['status']: return JsonResponse({'status': False, 'error': status['error']}) return JsonResponse({'status': True, 'workspace': status['workspace']})
def workspace_search(request): passphrase = request.POST['passphrase'] workspace_id = request.POST['workspace_id'] search = request.POST['search'] workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.search(passphrase, search) return JsonResponse(status)
def delFolder(request): session_key = request.session['key'] workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] folder_id = request.POST['folder_id'] workspace_utils = WorkspaceUtils( request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.del_folder(folder_id, passphrase) return result(status, error, folder_id)
def workspace_tree(request): passphrase = request.POST['passphrase'] workspace_id = request.POST['workspace_id'] workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.get_tree(passphrase) if not status['status']: return JsonResponse({'status': False, 'error': status['error']}) return JsonResponse(status)
def getPassword(request): session_key = request.session['key'] workspace_id = request.POST['workspace_id'] key_id = request.POST['key_id'] passphrase = request.POST['passphrase'] folder_id = request.POST['folder_id'] workspace_utils = WorkspaceUtils(request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.get_password(key_id, folder_id, passphrase) return result(status, error)
def workspace_backup(request): passphrase = request.POST.get('passphrase') workspace_id = request.POST['workspace_id'] workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status, file = workspace_utils.backup(passphrase, from_ui=True) if not status: return JsonResponse({'status': False, 'error': file}) return JsonResponse({'status': True, 'backup': json.dumps(file, indent=4)})
def moveKey(request): session_key = request.session['key'] workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] key_id = request.POST['key_id'] folder_from = request.POST['folder_from'] folder_to = request.POST['folder_to'] workspace_utils = WorkspaceUtils(request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.move_key(key_id, folder_from, folder_to, passphrase) return result(status, error)
def workspace_import_xml_keepass(request): passphrase = request.POST.get('passphrase') workspace_id = request.POST['workspace_id'] file = request.FILES['keepass'] workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.import_xml_keepass(passphrase, file) if not status['status']: return JsonResponse({'status': False, 'error': status['error']}) return JsonResponse({'status': True})
def saveFolder(request): session_key = request.session['key'] workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] folder = { "id": request.POST['id'], "text": request.POST['text'], "icon": request.POST['icon'], "parent": request.POST['parent'], } workspace_utils = WorkspaceUtils( request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.save_folder(folder, passphrase) return result(status, error, folder)
def backup_workspaces(password, path="/var/tmp/"): email = "*****@*****.**" user = authenticate(username=email, password=password) if not user: raise Exception("Invalid backup password") for shared in Shared.objects.filter(user=user): workspace_utils = WorkspaceUtils(user, workspace_id=shared.workspace.pk) status, file = workspace_utils.backup( hashlib.sha512(password.encode()).hexdigest()) if status: filename = f"backup_workspace_{shared}.json" with open(os.path.join(path, filename), 'w') as f: f.write(file)
def workspace_share(request): workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] right = request.POST['right'] users = json.loads(request.POST['users']) workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.share_workspace(passphrase, users, [], right) if not status['status']: return JsonResponse({'status': False, 'error': status['error']}) return JsonResponse(status)
def workspace_export(request): workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] workspace_utils = WorkspaceUtils(request.user, workspace_id, session_key=request.session.get('key')) status = workspace_utils.export_workspace(passphrase) if not status['status']: return JsonResponse({'status': False, 'error': status['error']}) response = StreamingHttpResponse(streaming_content=status['file']) response[ 'Content-Disposition'] = 'attachement; filename="{filename}.kbdx"'.format( filename=workspace_id) return response
def saveKey(request): session_key = request.session['key'] workspace_id = request.POST['workspace_id'] passphrase = request.POST['passphrase'] key = { "id": request.POST['id'], "name": request.POST['name'], "login": request.POST['login'], "password": request.POST['password'], "uri": request.POST['uri'], "ipv4": request.POST.get('ipv4'), "ipv6": request.POST.get('ipv6'), "informations": request.POST['informations'], "folder": request.POST['folder'], } workspace_utils = WorkspaceUtils(request.user, workspace_id=workspace_id, session_key=session_key) status, error = workspace_utils.save_key(key, passphrase) return result(status, error)
def configure_account(request, user_id): model_user = get_user_model() try: user = model_user.objects.get(pk=user_id) except model_user.DoesNotExist: return HttpResponseForbidden('Not allowed') if user.configure: return HttpResponseRedirect('/') try: settings = SecuritySettings.objects.get() except SecuritySettings.DoesNotExist: settings = SecuritySettings(length_password=8) if request.method == "POST": first_name = request.POST['first_name'] last_name = request.POST['last_name'] password = request.POST['password'] password = request.POST['password'] repassword = request.POST['repassword'] if password != repassword: error = _("Password mismatch") return render(request, 'configure.html', { 'user': user, 'error': error }) if len(password) < settings.length_password: error = "The password must be at least {} characters long.".format( settings.length_password) return render(request, 'configure.html', { 'user': user, 'error': error }) # At least one letter and one non-letter first_isalpha = password[0].isalpha() if all(c.isalpha() == first_isalpha for c in password): error = _("The password must contain at least one letter and at least \ one digit or punctuation character.") return render(request, 'configure.html', { 'user': user, 'error': error }) hashed_passwd = hashlib.sha512(password.encode('utf-8')).hexdigest() user.generate_keys(hashed_passwd) user.set_password(password) user.first_name = first_name user.last_name = last_name user.last_change_passwd = datetime.datetime.now() user.configure = True user.save() workspace_utils = WorkspaceUtils(user) workspace_utils.create_workspace("Personal") logger.info("User {} configured".format(user.email)) return HttpResponseRedirect('/login') password_indication = "At least {} characters, with one letter and \ one digit or punctuation".format(settings.length_password) return render(request, 'configure.html', { 'user': user, 'password_indication': password_indication, })
def install(request): # Check if installation have already be done if get_user_model().objects.count(): return HttpResponseRedirect('/') mail_form = MailSettingsForm(instance=MailSettings()) security_form = SecuritySettingsForm(instance=SecuritySettings()) general_form = GeneralSettingsForm(instance=GeneralSettings()) if request.method == "POST": User = get_user_model() secu_settings = SecuritySettings( password_change=int(request.POST['password_change']), length_password=int(request.POST['length_password']), key_size=int(request.POST['key_size']), ) mail_settings = MailSettings(host=request.POST['host']) gen_settings = GeneralSettings( company_name=request.POST['company_name']) first_name = request.POST['first_name'] last_name = request.POST['last_name'] email = request.POST['email'] password = request.POST['password'] password = request.POST['password'] repassword = request.POST['repassword'] backup_password = request.POST['backup_password'] if password != repassword: error = _("Password mismatch") return JsonResponse({'status': False, 'error': error}) if len(password) < secu_settings.length_password: error = _("The password must be at least \ {} characters long.".format(secu_settings.length_password)) return JsonResponse({'status': False, 'error': error}) # At least one letter and one non-letter first_isalpha = password[0].isalpha() if all(c.isalpha() == first_isalpha for c in password): error = _("The password must contain at least one letter and at \ least one digit or punctuation character.") return JsonResponse({'status': False, 'error': error}) superuser = User(first_name=first_name, last_name=last_name, email=email, last_change_passwd=datetime.datetime.now(), configure=True, is_superuser=True) superuser.set_password(password) superuser.generate_keys( hashlib.sha512(password.encode('utf-8')).hexdigest()) superuser.save() backup_user = User(first_name="Backup", last_name="Backup", email="*****@*****.**", last_change_passwd=datetime.datetime.now(), configure=True, is_superuser=False) backup_user.set_password(password) backup_user.generate_keys( hashlib.sha512(backup_password.encode('utf-8')).hexdigest()) backup_user.save() secu_settings.save() try: mail_settings.save() except Exception: pass gen_settings.save() # Creating workspace workspace_utils = WorkspaceUtils(superuser) status, error = workspace_utils.create_workspace(name="Personal") return JsonResponse({'status': True}) return render(request, 'install.html', { 'security': security_form, 'general': general_form, 'mail': mail_form, })