Пример #1
0
    def post(self, request, *args, **kwargs):

        body_unicode = request.body.decode("utf-8")
        body = json.loads(body_unicode)

        try:
            # Get service from database
            embed = Thirdparty.objects.get(name=body["embedID"])
        except KeyError:
            # No embed kwarg is set, warn
            logger.warn(
                "EmbedMetaView.post: Malformed POST request received",
                extra={"request": request},
            )
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))
        except ObjectDoesNotExist:
            logger.warn(
                "EmbedMetaView.post: POST request for non-existing Thirdparty",
                extra={"request": request},
            )
            return JsonResponse(convertForJsonResponse({"success": False}))

        # Form is valid
        sector = body["sector"]
        country = body["country_of_origin"]

        if sector != "" and country != "":
            embed.country_of_origin = country
            embed.sector = sector
            embed.save()
            return JsonResponse(convertForJsonResponse({"success": True}))

        else:
            return JsonResponse(convertForJsonResponse({"success": False}))
Пример #2
0
 def toJSON(self):
     return {
         "url": convertForJsonResponse(self.url),
         "name": convertForJsonResponse(self.name),
         "permitted_senders": convertForJsonResponse(self.permitted_senders),
         # "thirdparties" : convertForJsonResponse(list(self.thirdparties.all())),
         "resultsdirty": convertForJsonResponse(self.resultsdirty),
         "hasApprovedIdentity": convertForJsonResponse(self.hasApprovedIdentity),
         "country_of_origin": convertForJsonResponse(self.country_of_origin.code),
         "sector": convertForJsonResponse(self.sector),
     }
Пример #3
0
 def toJSON(self):
     return {
         "first_name": convertForJsonResponse(self.first_name),
         "surname": convertForJsonResponse(self.surname),
         "mail": convertForJsonResponse(self.mail),
         "gender": convertForJsonResponse(self.gender),
         "service": convertForJsonResponse(self.service),
         "approved": convertForJsonResponse(self.approved),
     }
Пример #4
0
 def toJSON(self):
     return {
         "service":
         convertForJsonResponse(self.service),
         "thirdparty":
         convertForJsonResponse(self.thirdparty),
         "leaks_address":
         convertForJsonResponse(self.leaks_address),
         "embed_type":
         convertForJsonResponse(self.embed_type),
         "mail":
         convertForJsonResponse(self.mail),
         "sets_cookie":
         convertForJsonResponse(self.sets_cookie),
         "receives_identifier":
         convertForJsonResponse(self.receives_identifier),
     }
Пример #5
0
    def get(self, request, *args, **kwargs):
        # Check if the kwarg is even set
        try:
            sid = kwargs["embed"]
        except KeyError:
            try:
                sid = self.parseUrlToId(request.GET.get("url"))
            except:
                return HttpResponseNotFound("embed not found")
        try:
            embed = Thirdparty.objects.get(id=sid)
        except ObjectDoesNotExist:
            logger.info(
                "EmbedView.get: Invalid service requested",
                extra={"request": request, "service_id": sid},
            )
            # TODO: Add code to display a warning on homepage
            return redirect("Home")

        return JsonResponse(convertForJsonResponse(self.render_embed(request, embed)))
Пример #6
0
 def toJSON(self):
     return {
         "name":
         convertForJsonResponse(self.name),
         "host":
         convertForJsonResponse(self.host),
         "resultsdirty":
         convertForJsonResponse(self.resultsdirty),
         "country_of_origin":
         convertForJsonResponse(self.country_of_origin.code),
         "sector":
         convertForJsonResponse(self.sector),
         "service":
         convertForJsonResponse(self.service),
     }
Пример #7
0
    def post(self, request, *args, **kwargs):
        try:
            # Get service from database
            body_unicode = request.body.decode("utf-8")
            body = json.loads(body_unicode)
            domain = body["domain"]
        except KeyError:
            # No service kwarg is set, warn
            logger.warn(
                "ServiceMetaView.post: Malformed POST request received",
                extra={"request": request},
            )
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))
        try:
            # Format domain. Will also ensure that the domain is valid, and return None on invalid domains
            domain = validate_domain(domain)
        except KeyError:
            # Someone is messing with us. Log this.
            logger.warning(
                "IdentityView.post: Malformed POST request received",
                extra={"request": request},
            )
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))
        # Check if people are messing with us
        except AssertionError:
            # Someone may be messing with us. Save it, just in case.
            logger.info(
                "IdentityView.post: Invalid URL passed",
                extra={"request": request, "domain": domain},
            )
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))

        # Get or create service
        service, created = Service.get_or_create(url=domain, name=domain)
        service.save()

        # Select a domain to use for the identity
        # Create a list of possible domains
        domains = [cred["DOMAIN"] for cred in settings.MAILCREDENTIALS]
        # Shuffle it
        shuffle(domains)
        # Iterate through it
        for identityDomain in domains:
            # If the domain has not yet been used, stop the loop, otherwise try the next
            if (
                Identity.objects.filter(service_id=service.pk)
                .filter(mail__contains=identityDomain)
                .count()
                == 0
            ):
                break
        # At this point, we have either selected a domain that has not yet been used for the
        # provided service, or the service already has at least one identity for each domain,
        # in which case we have picked one domain at random (by shuffling the list first).

        # Create an identity and save it
        ident = Identity.create(service, identityDomain)
        ident.save()

        if created:
            create_service_cache(service, force=True)

        # Display the result to the user
        return JsonResponse(convertForJsonResponse(ident))