Esempio n. 1
0
 def test_invalid_valid_services(self):
     """
     When invalid services are configured, ``is_valid_service``
     should raise ``ImproperlyConfigured``.
     """
     with self.assertRaises(ImproperlyConfigured):
         is_valid_service('http://www.example.com')
 def test_invalid_valid_services(self):
     """
     When invalid services are configured, ``is_valid_service``
     should raise ``ImproperlyConfigured``.
     """
     with self.assertRaises(ImproperlyConfigured):
         is_valid_service('http://www.example.com')
Esempio n. 3
0
 def test_is_valid_service(self):
     """
     When valid services are configured, ``is_valid_service()``
     should return ``True`` if the provided URL matches, and
     ``False`` otherwise.
     """
     self.assertTrue(is_valid_service('http://www.example.com'))
     self.assertFalse(is_valid_service('http://www.example.org'))
 def test_is_valid_service(self):
     """
     When valid services are configured, ``is_valid_service()``
     should return ``True`` if the provided URL matches, and
     ``False`` otherwise.
     """
     self.assertTrue(is_valid_service('http://www.example.com'))
     self.assertFalse(is_valid_service('http://www.example.org'))
Esempio n. 5
0
    def validate_ticket(self, ticket, service, renew=False, require_https=False):
        """
        Given a ticket string and service identifier, validate the
        corresponding ``Ticket``. If validation succeeds, return the
        ``Ticket``. If validation fails, raise an appropriate error.

        If ``renew`` is ``True``, ``ServiceTicket`` validation will
        only succeed if the ticket was issued from the presentation
        of the user's primary credentials.

        If ``require_https`` is ``True``, ``ServiceTicket`` validation
        will only succeed if the service URL scheme is HTTPS.
        """
        if not ticket:
            raise InvalidRequest("No ticket string provided")

        if not self.model.TICKET_RE.match(ticket):
            raise InvalidTicket("Ticket string %s is invalid" % ticket)

        try:
            t = self.get(ticket=ticket)
        except self.model.DoesNotExist:
            raise InvalidTicket("Ticket %s does not exist" % ticket)

        if t.is_consumed():
            raise InvalidTicket("%s %s has already been used" %
                                (t.name, ticket))
        if t.is_expired():
            raise InvalidTicket("%s %s has expired" % (t.name, ticket))

        if not service:
            raise InvalidRequest("No service identifier provided")

        if require_https and not is_scheme_https(service):
            raise InvalidService("Service %s is not HTTPS" % service)

        if not is_valid_service(service):
            raise InvalidService("Service %s is not a valid %s URL" %
                                 (service, t.name))

        try:
            if not match_service(t.service, service):
                raise InvalidService("%s %s for service %s is invalid for "
                        "service %s" % (t.name, ticket, t.service, service))
        except AttributeError:
            pass

        try:
            if renew and not t.is_primary():
                raise InvalidTicket("%s %s was not issued via primary "
                                    "credentials" % (t.name, ticket))
        except AttributeError:
            pass

        logger.debug("Validated %s %s" % (t.name, ticket))
        return t
Esempio n. 6
0
    def get(self, request, *args, **kwargs):
        service = request.GET.get('service')
        ticket = request.GET.get('ticket')

        if not is_valid_service(service):
            return redirect('cas_login')

        msg = _("Do you want to access %(service)s as %(user)s?") % {
                'service': clean_service_url(service),
                'user': request.user}
        messages.info(request, msg)
        kwargs['service'] = add_query_params(service, {'ticket': ticket})
        return super(WarnView, self).get(request, *args, **kwargs)
Esempio n. 7
0
    def get(self, request, *args, **kwargs):
        service = request.GET.get('service')
        ticket = request.GET.get('ticket')

        if not is_valid_service(service):
            return redirect('cas_login')

        msg = _("Do you want to access %(service)s as %(user)s?") % {
            'service': clean_service_url(service),
            'user': request.user
        }
        messages.info(request, msg)
        kwargs['service'] = add_query_params(service, {'ticket': ticket})
        return super(WarnView, self).get(request, *args, **kwargs)
Esempio n. 8
0
    def validate_ticket(self,
                        ticket,
                        service,
                        renew=False,
                        require_https=False):
        """
        Given a ticket string and service identifier, validate the
        corresponding ``Ticket``. If validation succeeds, return the
        ``Ticket``. If validation fails, raise an appropriate error.

        If ``renew`` is ``True``, ``ServiceTicket`` validation will
        only succeed if the ticket was issued from the presentation
        of the user's primary credentials.

        If ``require_https`` is ``True``, ``ServiceTicket`` validation
        will only succeed if the service URL scheme is HTTPS.
        """
        if not ticket:
            raise InvalidRequest("No ticket string provided")

        if not self.model.TICKET_RE.match(ticket):
            raise InvalidTicket("Ticket string %s is invalid" % ticket)

        try:
            t = self.get(ticket=ticket)
        except self.model.DoesNotExist:
            raise InvalidTicket("Ticket %s does not exist" % ticket)

        if t.is_consumed():
            raise InvalidTicket("%s %s has already been used" %
                                (t.name, ticket))
        if t.is_expired():
            raise InvalidTicket("%s %s has expired" % (t.name, ticket))

        if not service:
            raise InvalidRequest("No service identifier provided")

        if require_https and not is_scheme_https(service):
            raise InvalidService("Service %s is not HTTPS" % service)

        if not is_valid_service(service):
            raise InvalidService("Service %s is not a valid %s URL" %
                                 (service, t.name))

        try:
            if not match_service(t.service, service):
                raise InvalidService("%s %s for service %s is invalid for "
                                     "service %s" %
                                     (t.name, ticket, t.service, service))
        except AttributeError:
            pass

        try:
            if renew and not t.is_primary():
                raise InvalidTicket("%s %s was not issued via primary "
                                    "credentials" % (t.name, ticket))
        except AttributeError:
            pass

        logger.debug("Validated %s %s" % (t.name, ticket))
        return t
Esempio n. 9
0
 def test_empty_valid_services(self):
     """
     When no valid services are configured,
     ``is_valid_service()`` should return ``True``.
     """
     self.assertTrue(is_valid_service('http://www.example.com'))
Esempio n. 10
0
 def test_empty_valid_services(self):
     """
     When no valid services are configured,
     ``is_valid_service()`` should return ``True``.
     """
     self.assertTrue(is_valid_service('http://www.example.com'))