Skip to content

Commit

Permalink
Ensure user is authenticated correctly when deploying asset
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisRayM committed Nov 24, 2022
1 parent 2049257 commit 05dc735
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,32 @@
from typing import Generator, Optional, Union
from urllib.parse import urlparse
from xml.etree import ElementTree as ET

try:
from zoneinfo import ZoneInfo
except ImportError:
from backports.zoneinfo import ZoneInfo

import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured
from lxml import etree
from django.core.files import File
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as t
from lxml import etree
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.reverse import reverse

from kpi.constants import (
SUBMISSION_FORMAT_TYPE_JSON,
SUBMISSION_FORMAT_TYPE_XML,
PERM_FROM_KC_ONLY,
PERM_CHANGE_SUBMISSIONS,
PERM_DELETE_SUBMISSIONS,
PERM_VALIDATE_SUBMISSIONS,
)
from kpi.exceptions import (
AttachmentNotFoundException,
InvalidXPathException,
SubmissionIntegrityError,
SubmissionNotFoundException,
XPathNotFoundException,
)
from kpi.constants import (PERM_CHANGE_SUBMISSIONS, PERM_DELETE_SUBMISSIONS,
PERM_FROM_KC_ONLY, PERM_VALIDATE_SUBMISSIONS,
SUBMISSION_FORMAT_TYPE_JSON,
SUBMISSION_FORMAT_TYPE_XML)
from kpi.exceptions import (AttachmentNotFoundException, InvalidXPathException,
SubmissionIntegrityError,
SubmissionNotFoundException,
XPathNotFoundException)
from kpi.interfaces.sync_backend_media import SyncBackendMediaInterface
from kpi.models.asset_file import AssetFile
from kpi.models.object_permission import ObjectPermission
Expand All @@ -49,24 +44,17 @@
from kpi.utils.mongo_helper import MongoHelper
from kpi.utils.permissions import is_user_anonymous
from kpi.utils.xml import edit_submission_xml

from ..exceptions import (BadFormatException,
KobocatBulkUpdateSubmissionsClientException,
KobocatDeploymentException,
KobocatDuplicateSubmissionException)
from .base_backend import BaseDeploymentBackend
from .kc_access.shadow_models import (
KobocatOneTimeAuthToken,
KobocatXForm,
ReadOnlyKobocatAttachment,
ReadOnlyKobocatInstance,
)
from .kc_access.utils import (
assign_applicable_kc_permissions,
instance_count,
last_submission_time
)
from ..exceptions import (
BadFormatException,
KobocatBulkUpdateSubmissionsClientException,
KobocatDeploymentException,
KobocatDuplicateSubmissionException,
)
from .kc_access.shadow_models import (KobocatOneTimeAuthToken, KobocatXForm,
ReadOnlyKobocatAttachment,
ReadOnlyKobocatInstance)
from .kc_access.utils import (assign_applicable_kc_permissions, instance_count,
last_submission_time)


class KobocatDeploymentBackend(BaseDeploymentBackend):
Expand Down Expand Up @@ -1372,7 +1360,8 @@ def __kobocat_proxy_request(kc_request, user=None):
:return: requests.models.Response
"""
if not is_user_anonymous(user):
token, created = Token.objects.get_or_create(user=user)
token = User.objects.using("kobocat").select_related(
"auth_token").get(username=user.username).auth_token
kc_request.headers['Authorization'] = 'Token %s' % token.key
session = requests.Session()
return session.send(kc_request.prepare())
Expand Down

0 comments on commit 05dc735

Please sign in to comment.