コード例 #1
0
    def test_init(self):
        # Should reject unknown algorithms
        with self.assertRaises(TokenBackendError):
            TokenBackend('oienarst oieanrsto i', 'not_secret')

        TokenBackend('HS256', 'not_secret')
コード例 #2
0
class TestTokenBackend(TestCase):
    def setUp(self):
        self.hmac_token_backend = TokenBackend('HS256', SECRET)
        self.rsa_token_backend = TokenBackend('RS256', PRIVATE_KEY, PUBLIC_KEY)
        self.aud_iss_token_backend = TokenBackend('RS256', PRIVATE_KEY, PUBLIC_KEY, AUDIENCE, ISSUER)
        self.payload = {'foo': 'bar'}

    def test_init(self):
        # Should reject unknown algorithms
        with self.assertRaises(TokenBackendError):
            TokenBackend('oienarst oieanrsto i', 'not_secret')

        TokenBackend('HS256', 'not_secret')

    def test_encode_hmac(self):
        # Should return a JSON web token for the given payload
        payload = {'exp': make_utc(datetime(year=2000, month=1, day=1))}

        hmac_token = self.hmac_token_backend.encode(payload)

        # Token could be one of two depending on header dict ordering
        self.assertIn(
            hmac_token,
            (
                'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjk0NjY4NDgwMH0.NHpdD2X8ub4SE_MZLBedWa57FCpntGaN_r6f8kNKdUs',
                'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk0NjY4NDgwMH0.jvxQgXCSDToR8uKoRJcMT-LmMJJn2-NM76nfSR2FOgs',
            ),
        )

    def test_encode_rsa(self):
        # Should return a JSON web token for the given payload
        payload = {'exp': make_utc(datetime(year=2000, month=1, day=1))}

        rsa_token = self.rsa_token_backend.encode(payload)

        # Token could be one of two depending on header dict ordering
        self.assertIn(
            rsa_token,
            (
                'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4NDgwMH0.cuE6ocmdxVHXVrGufzn-_ZbXDV475TPPb5jtSacvJsnR3s3tLIm9yR7MF4vGcCAUGJn2hU_JgSOhs9sxntPPVjdwvC3-sxAwfUQ5AgUCEAF5XC7wTvGhmvufzhAgEG_DNsactCh79P8xRnc0iugtlGNyFp_YK582-ZrlfQEp-7C0L9BNG_JCS2J9DsGR7ojO2xGFkFfzezKRkrVTJMPLwpl0JAiZ0iqbQE-Tex7redCrGI388_mgh52GLsNlSIvW2gQMqCVMYndMuYx32Pd5tuToZmLUQ2PJ9RyAZ4fOMApTzoshItg4lGqtnt9CDYzypHULJZohJIPcxFVZZfHxhw',
                'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk0NjY4NDgwMH0.pzHTOaVvKJMMkSqksGh-NdeEvQy8Thre3hBM3smUW5Sohtg77KnHpaUYjq30DyRmYQRmPSjEVprh1Yvic_-OeAXPW8WVsF-r4YdJuxWUpuZbIPwJ9E-cMfTZkDkOl18z1zOdlsLtsP2kXyAlptyy9QQsM7AxoqM6cyXoQ5TI0geWccgoahTy3cBtA6pmjm7H0nfeDGqpqYQBhtaFmRuIWn-_XtdN9C6NVmRCcZwyjH-rP3oEm6wtuKJEN25sVWlZm8YRQ-rj7A7SNqBB5tFK2anM_iv4rmBlIEkmr_f2s_WqMxn2EWUSNeqbqiwabR6CZUyJtKx1cPG0B2PqOTcZsg',
            ),
        )

    def test_encode_aud_iss(self):
        # Should return a JSON web token for the given payload
        original_payload = {'exp': make_utc(datetime(year=2000, month=1, day=1))}
        payload = original_payload.copy()

        rsa_token = self.aud_iss_token_backend.encode(payload)

        # Assert that payload has not been mutated by the encode() function
        self.assertEqual(payload, original_payload)

        # Token could be one of 12 depending on header dict ordering
        self.assertIn(
            rsa_token,
            (
                'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4NDgwMCwiYXVkIjoib3BlbmlkLWNsaWVudC1pZCIsImlzcyI6Imh0dHBzOi8vd3d3Lm15b2lkY3Byb3ZpZGVyLmNvbSJ9.kSz7KyUZgpKaeQHYSQlhsE-UFLG2zhBiJ2MFCIvhstA4lSIKj3U1fdP1OhEDg7X66EquRRIZrby6M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',
                'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk0NjY4NDgwMCwiaXNzIjoiaHR0cHM6Ly93d3cubXlvaWRjcHJvdmlkZXIuY29tIiwiYXVkIjoib3BlbmlkLWNsaWVudC1pZCJ9.3NjgS14SGFyJ6cix2XJZFPlcyeSu4LSduEMUIH0grJuCljbhalyoib5s4JnBaK4slKrQv1WHlhnKB737hX1FF7_EwQM3toCf--DBjrIuq5cYK3rzcn71JDe_op3CvClwpVyxd2vQZtQfP_tWqN6cNTuWF8ZQ0rJGug6Zb-NeE5h68YK_9tXLZC_i5anyjjAVONOc3Nd-TeIUBaLQKQXOddw0gcTcA7vg3uS0gXTEDq-_ZkF-v9bn1ua4_lgRPbuaYvrBFbXSCEdvNORPfPz4zfL3XU09q0gmnmXC9nxjUFVX4BjkP_YiCCO42sqUKY4y7STTB_IkK_04e2wntonVZA',
                'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3d3dy5teW9pZGNwcm92aWRlci5jb20iLCJleHAiOjk0NjY4NDgwMCwiYXVkIjoib3BlbmlkLWNsaWVudC1pZCJ9.b4pdohov81oqzLyCIp4y7e4VYz7LSez7bH0t1o0Zwzau1uXPYXcasT9lxxNMEEiZwHIovPLyWQ6XvF0bMWTk9vc4PyIpkLqsLBJPsuQ-wYUOD04fECmqUX_JaINqm2pPbohTcOQwl0xsE1UMIKTFBZDL1hEXGEMdW9lrPcXilhbC1ikyMpzsmVh55Q_wL2GqydssnOOcDQTqEkWoKvELJJhBcE-YuQkUp8jEVhF3VZ4jEZkzCErTlyXcfe1qXZHkWtw2QNo9s_SfLuRy_fACOo8XE9pHBoE7rqiSm-FmISgiLO1Jj3Pqq-abjN4SnAbU7PZWcV3fUoO1eYLGORmAcw',
                'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL3d3dy5teW9pZGNwcm92aWRlci5jb20iLCJhd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iy_ip4O082V8ElZqwugvDpS-7yQ3FTvQ3WXqtVAJc-fBZe4ZsBnrXUWwZV0Nhoe6iWKjEjTPjonQWbWL_WUJmIC2HVz18AOISnqReV2rcuLSHQ2ckhsyktlE9K1Rfj-Hi6f3HzzzePEgTsL2ZdBH6GrcmJVDFKqLLrkvOHShoPp7rcwuFBr0J_S1oqYac5O0B-u0OVnxBXTwij0ThrTGMgVCp2rn6Hk0NvtF6CE49Eu4XP8Ue-APT8l5_SjqX9GcrjkJp8Gif_oyBheL-zRg_v-cU60X6qY9wVolO8WodVPSnlE02XyYhLVxvfK-w5129A',
            ),
        )

    def test_decode_hmac_with_no_expiry(self):
        no_exp_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        self.hmac_token_backend.decode(no_exp_token)

    def test_decode_hmac_with_no_expiry_no_verify(self):
        no_exp_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        self.assertEqual(
            self.hmac_token_backend.decode(no_exp_token, verify=False),
            self.payload,
        )

    def test_decode_hmac_with_expiry(self):
        self.payload['exp'] = aware_utcnow() - timedelta(seconds=1)

        expired_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        with self.assertRaises(TokenBackendError):
            self.hmac_token_backend.decode(expired_token)

    def test_decode_hmac_with_invalid_sig(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, SECRET, algorithm='HS256')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, SECRET, algorithm='HS256')

        if DECODE_STRINGS:
            token_1 = token_1.decode('utf-8')
            token_2 = token_2.decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        with self.assertRaises(TokenBackendError):
            self.hmac_token_backend.decode(invalid_token)

    def test_decode_hmac_with_invalid_sig_no_verify(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, SECRET, algorithm='HS256')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, SECRET, algorithm='HS256')
        if DECODE_STRINGS:
            token_1 = token_1.decode('utf-8')
            token_2 = token_2.decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        self.assertEqual(
            self.hmac_token_backend.decode(invalid_token, verify=False),
            self.payload,
        )

    def test_decode_hmac_success(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        self.payload['foo'] = 'baz'

        token = jwt.encode(self.payload, SECRET, algorithm='HS256')
        if DECODE_STRINGS:
            token = token.decode('utf-8')
        self.assertEqual(self.hmac_token_backend.decode(token), self.payload)

    def test_decode_rsa_with_no_expiry(self):
        no_exp_token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        self.rsa_token_backend.decode(no_exp_token)

    def test_decode_rsa_with_no_expiry_no_verify(self):
        no_exp_token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        self.assertEqual(
            self.hmac_token_backend.decode(no_exp_token, verify=False),
            self.payload,
        )

    def test_decode_rsa_with_expiry(self):
        self.payload['exp'] = aware_utcnow() - timedelta(seconds=1)

        expired_token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        with self.assertRaises(TokenBackendError):
            self.rsa_token_backend.decode(expired_token)

    def test_decode_rsa_with_invalid_sig(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        if DECODE_STRINGS:
            token_1 = token_1.decode('utf-8')
            token_2 = token_2.decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        with self.assertRaises(TokenBackendError):
            self.rsa_token_backend.decode(invalid_token)

    def test_decode_rsa_with_invalid_sig_no_verify(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        if DECODE_STRINGS:
            token_1 = token_1.decode('utf-8')
            token_2 = token_2.decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        self.assertEqual(
            self.hmac_token_backend.decode(invalid_token, verify=False),
            self.payload,
        )

    def test_decode_rsa_success(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        self.payload['foo'] = 'baz'

        token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')
        if DECODE_STRINGS:
            token = token.decode('utf-8')

        self.assertEqual(self.rsa_token_backend.decode(token), self.payload)

    def test_decode_aud_iss_success(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        self.payload['foo'] = 'baz'
        self.payload['aud'] = AUDIENCE
        self.payload['iss'] = ISSUER

        token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')
        if DECODE_STRINGS:
            token = token.decode('utf-8')

        self.assertEqual(self.aud_iss_token_backend.decode(token), self.payload)
コード例 #3
0
ファイル: contextualizacion.py プロジェクト: OpenOPx/opx-web
def dia(request):

    try:

        barrioUbicacion = request.GET.get('barrioUbicacion')
        barrioSeleccion = request.GET.get('barrioSeleccion')
        year = request.GET.get('year')

        if request.META['HTTP_AUTHORIZATION'] != 'null':
            # Decodificando el access token
            tokenBackend = TokenBackend(settings.SIMPLE_JWT['ALGORITHM'],
                                        settings.SIMPLE_JWT['SIGNING_KEY'],
                                        settings.SIMPLE_JWT['VERIFYING_KEY'])
            tokenDecoded = tokenBackend.decode(
                request.META['HTTP_AUTHORIZATION'].split()[1], verify=True)

            # consultando el usuario
            user = User.objects.get(pk=tokenDecoded['user_id'])
            edadUsuario = calculoEdad(user.fecha_nacimiento)

        if barrioUbicacion is not None and barrioSeleccion is not None and year is not None:

            conflictividadesCiudad = []
            conflictividadesUbicacion = []
            conflictividadesSeleccion = []
            conflictividadesPerfil = []

            diasSemana = {
                'Sunday': 1,
                'Monday': 2,
                'Tuesday': 3,
                'Wednesday': 4,
                'Thursday': 5,
                'Friday': 6,
                'Saturday': 7
            }
            diaSemana = diasSemana.get(date.today().strftime("%A"))
            hora = 0
            horas = []

            while hora <= 23:

                horas.append(str(hora) + 'h')

                if hora < 10:
                    horaInicio = "0{}:00:00".format(str(hora))
                    horaFin = "0{}:59:59".format(str(hora))

                else:
                    horaInicio = "{}:00:00".format(str(hora))
                    horaFin = "{}:59:59".format(str(hora))

                queryIndicadorCiudad = "SELECT SUM(t.cantidad) as count FROM (" \
                                       "SELECT * FROM opx.contextualizaciones " \
                                       "WHERE dia = {1} and " \
                                       "(fecha_hecho between '{0}-01-01' and '{0}-12-31') and" \
                                       "(hora_hecho between '{2}' and '{3}')) t" \
                                       .format(year, diaSemana, horaInicio, horaFin)


                queryIndicadorUbicacion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE dia = {1} and " \
                                          "barrioid = {4} and " \
                                          "(fecha_hecho between '{0}-01-01' and '{0}-12-31') and" \
                                          "(hora_hecho between '{2}' and '{3}')) t" \
                                          .format(year, diaSemana, horaInicio, horaFin, barrioUbicacion)

                queryIndicadorSeleccion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE dia = {1} and " \
                                          "barrioid = {4} and " \
                                          "(fecha_hecho between '{0}-01-01' and '{0}-12-31') and" \
                                          "(hora_hecho between '{2}' and '{3}')) t" \
                                          .format(year, diaSemana, horaInicio, horaFin, barrioSeleccion)

                if 'user' in locals():
                    queryIndicadorPerfil = "SELECT SUM(t.cantidad) as count FROM (" \
                                           "SELECT * FROM opx.contextualizaciones " \
                                           "WHERE dia = {1} " \
                                           "and barrioid = {4}" \
                                           "and generoid = '{5}' " \
                                           "and nivelid = '{6}' " \
                                           "and edad = {7} " \
                                           "and (fecha_hecho between '{0}-01-01' and '{0}-12-31') " \
                                           "and (hora_hecho between '{2}' and '{3}')) t" \
                                           .format(year, diaSemana, horaInicio, horaFin, user.barrioid, user.generoid, user.nivel_educativo_id, edadUsuario)

                with connection.cursor() as cursor:

                    # Indicador Ciudad
                    cursor.execute(queryIndicadorCiudad)
                    indicadorCiudad = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesCiudad.append(indicadorCiudad)

                    # Indicador Ubicación
                    cursor.execute(queryIndicadorUbicacion)
                    indicadorUbicacion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesUbicacion.append(indicadorUbicacion)

                    # Indicador Selección
                    cursor.execute(queryIndicadorSeleccion)
                    indicadorSeleccion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesSeleccion.append(indicadorSeleccion)

                    if 'user' in locals():
                        # Indicador Perfil
                        cursor.execute(queryIndicadorPerfil)
                        indicadorPerfil = verificacionExistenciaConflictividades(
                            dictfetchall(cursor)[0]['count'])

                        conflictividadesPerfil.append(indicadorPerfil)

                hora += 1

            data = {
                'labels':
                horas,
                'datasets': [{
                    'label': 'ciudad',
                    'fill': False,
                    'borderColor': 'orange',
                    'data': conflictividadesCiudad
                }, {
                    'label': 'Ubicación',
                    'fill': False,
                    'borderColor': 'blue',
                    'data': conflictividadesUbicacion
                }, {
                    'label': 'Selección',
                    'fill': False,
                    'borderColor': 'green',
                    'data': conflictividadesSeleccion
                }]
            }

            if 'user' in locals():
                data['datasets'].append({
                    'label': 'Perfil',
                    'fill': False,
                    'borderColor': 'red',
                    'data': conflictividadesPerfil
                })

            response = {'code': 200, 'data': data, 'status': 'success'}

        else:
            response = {'code': 400, 'status': 'error'}

    except (IndexError):

        response = {'code': 400, 'status': 'error'}

    except TokenBackendError as e:

        response = {'code': 400, 'message': str(e), 'status': 'error'}

    return JsonResponse(response, safe=False, status=response['code'])
コード例 #4
0
 def setUp(self):
     self.hmac_token_backend = TokenBackend('HS256', SECRET)
     self.rsa_token_backend = TokenBackend('RS256', PRIVATE_KEY, PUBLIC_KEY)
     self.aud_iss_token_backend = TokenBackend('RS256', PRIVATE_KEY, PUBLIC_KEY, AUDIENCE, ISSUER)
     self.payload = {'foo': 'bar'}
コード例 #5
0
ファイル: contextualizacion.py プロジェクト: OpenOPx/opx-web
def mensual(request):

    try:

        barrioUbicacion = request.GET.get('barrioUbicacion')
        barrioSeleccion = request.GET.get('barrioSeleccion')
        year = request.GET.get('year')

        if request.META['HTTP_AUTHORIZATION'] != 'null':

            # Decodificando el access token
            tokenBackend = TokenBackend(settings.SIMPLE_JWT['ALGORITHM'],
                                        settings.SIMPLE_JWT['SIGNING_KEY'],
                                        settings.SIMPLE_JWT['VERIFYING_KEY'])
            tokenDecoded = tokenBackend.decode(
                request.META['HTTP_AUTHORIZATION'].split()[1], verify=True)

            # consultando el usuario
            user = User.objects.get(pk=tokenDecoded['user_id'])
            edadUsuario = calculoEdad(user.fecha_nacimiento)

        if barrioUbicacion is not None and barrioSeleccion is not None and year is not None:

            meses = [{
                'label': 'Enero',
                'value': '01',
                'lastDay': '31'
            }, {
                'label': 'Febrero',
                'value': '02',
                'lastDay': bisiesto(int(year), anio=False, mes=True)
            }, {
                'label': 'Marzo',
                'value': '03',
                'lastDay': '31'
            }, {
                'label': 'Abril',
                'value': '04',
                'lastDay': '30'
            }, {
                'label': 'Mayo',
                'value': '05',
                'lastDay': '31'
            }, {
                'label': 'Junio',
                'value': '06',
                'lastDay': '30'
            }, {
                'label': 'Julio',
                'value': '07',
                'lastDay': '31'
            }, {
                'label': 'Agosto',
                'value': '08',
                'lastDay': '31'
            }, {
                'label': 'Septiembre',
                'value': '09',
                'lastDay': '30'
            }, {
                'label': 'Octubre',
                'value': '10',
                'lastDay': '31'
            }, {
                'label': 'Noviembre',
                'value': '11',
                'lastDay': '30'
            }, {
                'label': 'Diciembre',
                'value': '12',
                'lastDay': '31'
            }]

            conflictividadesCiudad = []
            conflictividadesUbicacion = []
            conflictividadesSeleccion = []
            conflictividadesPerfil = []

            for mes in meses:

                queryIndicadorCiudad = "SELECT SUM(t.cantidad) as count FROM (" \
                                       "SELECT * FROM opx.contextualizaciones " \
                                       "WHERE fecha_hecho between '{0}-{1}-01' and '{0}-{1}-{2}') t" \
                                        .format(year, mes['value'], mes['lastDay'])

                queryIndicadorUbicacion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE barrioid = {3} and " \
                                          "(fecha_hecho between '{0}-{1}-01' and '{0}-{1}-{2}')) t" \
                                          .format(year, mes['value'], mes['lastDay'], barrioUbicacion)

                queryIndicadorSeleccion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE barrioid = {3} and " \
                                          "(fecha_hecho between '{0}-{1}-01' and '{0}-{1}-{2}')) t" \
                                          .format(year, mes['value'], mes['lastDay'], barrioSeleccion)

                if 'user' in locals():

                    queryIndicadorPerfil = "SELECT SUM(t.cantidad) as count FROM (" \
                                           "SELECT * FROM opx.contextualizaciones " \
                                           "WHERE barrioid = {3} " \
                                           "and generoid = '{4}' " \
                                           "and nivelid = '{5}' " \
                                           "and edad = {6} " \
                                           "and (fecha_hecho between '{0}-{1}-01' and '{0}-{1}-{2}')) t" \
                                           .format(year, mes['value'], mes['lastDay'], user.barrioid, user.generoid, user.nivel_educativo_id, edadUsuario)

                with connection.cursor() as cursor:

                    #Indicador Ciudad
                    cursor.execute(queryIndicadorCiudad)
                    indicadorCiudad = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesCiudad.append(indicadorCiudad)

                    #Indicador Ubicación
                    cursor.execute(queryIndicadorUbicacion)
                    indicadorUbicacion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesUbicacion.append(indicadorUbicacion)

                    #Indicador Selección
                    cursor.execute(queryIndicadorSeleccion)
                    indicadorSeleccion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesSeleccion.append(indicadorSeleccion)

                    if 'user' in locals():
                        #Indicador Perfil
                        cursor.execute(queryIndicadorPerfil)
                        indicadorPerfil = verificacionExistenciaConflictividades(
                            dictfetchall(cursor)[0]['count'])

                        conflictividadesPerfil.append(indicadorPerfil)

            data = {
                'labels': [m['label'] for m in meses],
                'datasets': [{
                    'label': 'ciudad',
                    'fill': False,
                    'borderColor': 'orange',
                    'data': conflictividadesCiudad
                }, {
                    'label': 'Ubicación',
                    'fill': False,
                    'borderColor': 'blue',
                    'data': conflictividadesUbicacion
                }, {
                    'label': 'Selección',
                    'fill': False,
                    'borderColor': 'green',
                    'data': conflictividadesSeleccion
                }]
            }

            if 'user' in locals():

                data['datasets'].append({
                    'label': 'Perfil',
                    'fill': False,
                    'borderColor': 'red',
                    'data': conflictividadesPerfil
                })

            response = {'code': 200, 'data': data, 'status': 'success'}

        else:
            response = {'code': 400, 'status': 'error'}

    except (IndexError):

        response = {'code': 400, 'status': 'error'}

    except TokenBackendError as e:

        response = {'code': 400, 'message': str(e), 'status': 'error'}

    return JsonResponse(response, safe=False, status=response['code'])
コード例 #6
0
ファイル: contextualizacion.py プロジェクト: OpenOPx/opx-web
def semanal(request):

    try:

        barrioUbicacion = request.GET.get('barrioUbicacion')
        barrioSeleccion = request.GET.get('barrioSeleccion')
        year = request.GET.get('year')

        if request.META['HTTP_AUTHORIZATION'] != 'null':
            # Decodificando el access token
            tokenBackend = TokenBackend(settings.SIMPLE_JWT['ALGORITHM'],
                                        settings.SIMPLE_JWT['SIGNING_KEY'],
                                        settings.SIMPLE_JWT['VERIFYING_KEY'])
            tokenDecoded = tokenBackend.decode(
                request.META['HTTP_AUTHORIZATION'].split()[1], verify=True)

            # consultando el usuario
            user = User.objects.get(pk=tokenDecoded['user_id'])
            edadUsuario = calculoEdad(user.fecha_nacimiento)

        if barrioUbicacion is not None and barrioSeleccion is not None and year is not None:

            semana = [{
                'label': 'Domingo',
                'value': 1
            }, {
                'label': 'Lunes',
                'value': 2
            }, {
                'label': 'Martes',
                'value': 3
            }, {
                'label': 'Miercoles',
                'value': 4
            }, {
                'label': 'Jueves',
                'value': 5
            }, {
                'label': 'Viernes',
                'value': 6
            }, {
                'label': 'Sabado',
                'value': 7
            }]

            conflictividadesCiudad = []
            conflictividadesUbicacion = []
            conflictividadesSeleccion = []
            conflictividadesPerfil = []

            for sem in semana:

                queryIndicadorCiudad = "SELECT SUM(t.cantidad) as count FROM (" \
                                       "SELECT * FROM opx.contextualizaciones " \
                                       "WHERE dia = {1} and " \
                                       "(fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                       .format(year, sem['value'])

                queryIndicadorUbicacion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE dia = {1} and " \
                                          "barrioid = {2} and " \
                                          "(fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                          .format(year, sem['value'], barrioUbicacion)

                queryIndicadorSeleccion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE dia = {1} and " \
                                          "barrioid = {2} and " \
                                          "(fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                          .format(year, sem['value'], barrioSeleccion)

                if 'user' in locals():
                    queryIndicadorPerfil = "SELECT SUM(t.cantidad) as count FROM (" \
                                           "SELECT * FROM opx.contextualizaciones " \
                                           "WHERE dia = {1} " \
                                           "and barrioid = {2}" \
                                           "and generoid = '{3}' " \
                                           "and nivelid = '{4}' " \
                                           "and edad = {5} " \
                                           "and (fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                           .format(year, sem['value'], user.barrioid, user.generoid, user.nivel_educativo_id, edadUsuario)

                with connection.cursor() as cursor:

                    #Indicador Ciudad
                    cursor.execute(queryIndicadorCiudad)
                    indicadorCiudad = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesCiudad.append(indicadorCiudad)

                    #Indicador Ubicación
                    cursor.execute(queryIndicadorUbicacion)
                    indicadorUbicacion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesUbicacion.append(indicadorUbicacion)

                    #Indicador Selección
                    cursor.execute(queryIndicadorSeleccion)
                    indicadorSeleccion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesSeleccion.append(indicadorSeleccion)

                    if 'user' in locals():
                        #Indicador Perfil
                        cursor.execute(queryIndicadorPerfil)
                        indicadorPerfil = verificacionExistenciaConflictividades(
                            dictfetchall(cursor)[0]['count'])

                        conflictividadesPerfil.append(indicadorPerfil)

            data = {
                'labels': [d['label'] for d in semana],
                'datasets': [{
                    'label': 'ciudad',
                    'fill': False,
                    'borderColor': 'orange',
                    'data': conflictividadesCiudad
                }, {
                    'label': 'Ubicación',
                    'fill': False,
                    'borderColor': 'blue',
                    'data': conflictividadesUbicacion
                }, {
                    'indicador': 'Selección',
                    'fill': False,
                    'borderColor': 'green',
                    'data': conflictividadesSeleccion
                }]
            }

            if 'user' in locals():
                data['datasets'].append({
                    'label': 'Perfil',
                    'fill': False,
                    'borderColor': 'red',
                    'data': conflictividadesPerfil
                })

            response = {'code': 200, 'data': data, 'status': 'success'}

        else:
            response = {'code': 400, 'status': 'error'}

    except (IndexError):

        response = {'code': 400, 'status': 'error'}

    except TokenBackendError as e:

        response = {'code': 400, 'message': str(e), 'status': 'error'}

    return JsonResponse(response, safe=False, status=response['code'])
コード例 #7
0
ファイル: contextualizacion.py プロジェクト: OpenOPx/opx-web
def todo(request):

    try:

        barrioUbicacion = request.GET.get('barrioUbicacion')
        barrioSeleccion = request.GET.get('barrioSeleccion')

        if request.META['HTTP_AUTHORIZATION'] != 'null':

            # Decodificando el access token
            tokenBackend = TokenBackend(settings.SIMPLE_JWT['ALGORITHM'],
                                        settings.SIMPLE_JWT['SIGNING_KEY'],
                                        settings.SIMPLE_JWT['VERIFYING_KEY'])
            tokenDecoded = tokenBackend.decode(
                request.META['HTTP_AUTHORIZATION'].split()[1], verify=True)

            # consultando el usuario
            user = User.objects.get(pk=tokenDecoded['user_id'])
            edadUsuario = calculoEdad(user.fecha_nacimiento)

        if barrioUbicacion is not None and barrioSeleccion is not None:

            anioInicial = 2010
            anioFinal = 2019
            anios = []

            count = anioInicial

            conflictividadesCiudad = []
            conflictividadesUbicacion = []
            conflictividadesSeleccion = []
            conflictividadesPerfil = []

            while count <= anioFinal:

                anios.append(count)

                queryIndicadorCiudad = "SELECT SUM(t.cantidad) as count FROM (" \
                        "SELECT * FROM opx.contextualizaciones " \
                        "WHERE fecha_hecho between '{0}-01-01' and '{0}-12-31') t" \
                        .format(count)

                queryIndicadorUbicacion = "SELECT SUM(t.cantidad) as count FROM (" \
                        "SELECT * FROM opx.contextualizaciones " \
                        "WHERE barrioid = {1} and " \
                        "(fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                        .format(count, barrioUbicacion)

                queryIndicadorSeleccion = "SELECT SUM(t.cantidad) as count FROM (" \
                                          "SELECT * FROM opx.contextualizaciones " \
                                          "WHERE barrioid = {1} and " \
                                          "(fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                          .format(count, barrioSeleccion)

                if 'user' in locals():
                    queryIndicadorPerfil = "SELECT SUM(t.cantidad) as count FROM (" \
                                           "SELECT * FROM opx.contextualizaciones " \
                                           "WHERE barrioid = {1} " \
                                           "and generoid = '{2}' " \
                                           "and nivelid = '{3}' " \
                                           "and edad = {4} " \
                                           "and (fecha_hecho between '{0}-01-01' and '{0}-12-31')) t" \
                                           .format(count, user.barrioid, user.generoid, user.nivel_educativo_id, edadUsuario)

                with connection.cursor() as cursor:

                    # Indicador Ciudad
                    cursor.execute(queryIndicadorCiudad)
                    indicadorCiudad = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesCiudad.append(indicadorCiudad)

                    # Indicador Ubicación
                    cursor.execute(queryIndicadorUbicacion)
                    indicadorUbicacion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesUbicacion.append(indicadorUbicacion)

                    # Indicador Selección
                    cursor.execute(queryIndicadorSeleccion)
                    indicadorSeleccion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    conflictividadesSeleccion.append(indicadorSeleccion)

                    if 'user' in locals():
                        # Indicador Perfil
                        cursor.execute(queryIndicadorPerfil)
                        indicadorPerfil = verificacionExistenciaConflictividades(
                            dictfetchall(cursor)[0]['count'])

                        conflictividadesPerfil.append(indicadorPerfil)

                count += 1

            data = {
                'labels':
                anios,
                'datasets': [{
                    'label': 'ciudad',
                    'fill': False,
                    'borderColor': 'orange',
                    'data': conflictividadesCiudad
                }, {
                    'label': 'Ubicación',
                    'fill': False,
                    'borderColor': 'blue',
                    'data': conflictividadesUbicacion
                }, {
                    'label': 'Selección',
                    'fill': False,
                    'borderColor': 'green',
                    'data': conflictividadesSeleccion
                }]
            }

            if 'user' in locals():

                totalConflictividadesPerfil = 0

                for cp in conflictividadesPerfil:
                    totalConflictividadesPerfil = totalConflictividadesPerfil + cp

                data['datasets'].append({
                    'label':
                    'Perfil',
                    'fill':
                    False,
                    'borderColor':
                    'red',
                    'data':
                    conflictividadesPerfil,
                    'promedio':
                    totalConflictividadesPerfil / len(conflictividadesPerfil)
                })

            response = {'code': 200, 'data': data, 'status': 'success'}

        else:
            response = {'code': 400, 'status': 'error'}

    except (IndexError):

        response = {'code': 400, 'status': 'error'}

    except TokenBackendError as e:

        response = {'code': 400, 'message': str(e), 'status': 'error'}

    return JsonResponse(response, safe=False, status=response['code'])
コード例 #8
0
ファイル: contextualizacion.py プロジェクト: OpenOPx/opx-web
def categorizacion(request):

    try:
        barrioUbicacion = request.GET.get('barrioUbicacion')
        barrioSeleccion = request.GET.get('barrioSeleccion')
        year = request.GET.get('year')

        if request.META['HTTP_AUTHORIZATION'] != 'null':

            # Decodificando el access token
            tokenBackend = TokenBackend(settings.SIMPLE_JWT['ALGORITHM'],
                                        settings.SIMPLE_JWT['SIGNING_KEY'],
                                        settings.SIMPLE_JWT['VERIFYING_KEY'])
            tokenDecoded = tokenBackend.decode(
                request.META['HTTP_AUTHORIZATION'].split()[1], verify=True)

            # consultando el usuario
            user = User.objects.get(pk=tokenDecoded['user_id'])
            edadUsuario = calculoEdad(user.fecha_nacimiento)

        if barrioUbicacion is not None and barrioSeleccion is not None and year is not None:

            conflictividades = Conflictividad.objects.all()
            data = []

            for conf in conflictividades:

                queryIndicadorCiudad = "select SUM(t.cantidad) as count from \
                                        (select * from opx.contextualizaciones \
                                        where (confid = '{0}') \
                                        and (fecha_hecho between '{1}-01-01' and '{1}-12-31')) t;" \
                                        .format(conf.confid, year)

                queryIndicadorUbicacion = "select SUM(t.cantidad) as count from \
                                        (select * from opx.contextualizaciones \
                                        where confid = '{0}' \
                                        and barrioid = '{1}' \
                                        and (fecha_hecho between '{2}-01-01' and '{2}-12-31')) t;" \
                                        .format(conf.confid, barrioUbicacion, year)

                queryIndicadorSeleccion = "select SUM(t.cantidad) as count from \
                                           (select * from opx.contextualizaciones \
                                           where confid = '{0}' \
                                           and barrioid = '{1}' \
                                           and (fecha_hecho between '{2}-01-01' and '{2}-12-31')) t;" \
                                          .format(conf.confid, barrioSeleccion, year)

                if 'user' in locals():
                    queryIndicadorPerfil = "select SUM(t.cantidad) as count from \
                                               (select * from opx.contextualizaciones \
                                               where confid = '{0}' \
                                               and barrioid = {1} \
                                               and generoid = '{2}' \
                                               and nivelid = '{3}' \
                                               and edad = {4} \
                                               and (fecha_hecho between '{5}-01-01' and '{5}-12-31')) t;" \
                                               .format(conf.confid, user.barrioid, user.generoid, user.nivel_educativo_id, edadUsuario, year)

                with connection.cursor() as cursor:
                    cursor.execute(queryIndicadorCiudad)
                    indicadorCiudad = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    cursor.execute(queryIndicadorUbicacion)
                    indicadorUbicacion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    cursor.execute(queryIndicadorSeleccion)
                    indicadorSeleccion = verificacionExistenciaConflictividades(
                        dictfetchall(cursor)[0]['count'])

                    if 'user' in locals():
                        cursor.execute(queryIndicadorPerfil)
                        indicadorPerfil = verificacionExistenciaConflictividades(
                            dictfetchall(cursor)[0]['count'])

                    if (indicadorCiudad != 0):
                        indicadorPromedio = indicadorCiudad / bisiesto(
                            int(year))
                    else:
                        indicadorPromedio = 0

                indicadores = {
                    'conflictividad': model_to_dict(conf),
                    'indicadores': {
                        'ciudad': indicadorCiudad,
                        'ubicacion': indicadorUbicacion,
                        'seleccion': indicadorSeleccion,
                        'promedio': indicadorPromedio
                    },
                }

                if 'user' in locals():
                    indicadores['indicadores']['perfil'] = indicadorPerfil

                data.append(indicadores)

                response = {'code': 200, 'data': data, 'status': 'success'}

        else:

            response = {'code': 400, 'status': 'error'}

    except (IndexError):
        response = {'code': 400, 'status': 'error'}

    except TokenBackendError as e:
        response = {'code': 400, 'message': str(e), 'status': 'error'}

    return JsonResponse(response, safe=False, status=response['code'])
コード例 #9
0
from django.contrib.auth import get_user_model

from rest_framework_simplejwt.backends import TokenBackend
from .settings import api_settings

User = get_user_model()
token_backend = TokenBackend(api_settings.ALGORITHM, api_settings.SIGNING_KEY,
                             api_settings.VERIFYING_KEY, api_settings.AUDIENCE,
                             api_settings.ISSUER)
コード例 #10
0
    def test_init(self):
        # Should reject unknown algorithms
        with self.assertRaises(TokenBackendError):
            TokenBackend("oienarst oieanrsto i", "not_secret")

        TokenBackend("HS256", "not_secret")
コード例 #11
0
 def setUp(self):
     self.hmac_token_backend = TokenBackend("HS256", SECRET)
     self.hmac_leeway_token_backend = TokenBackend("HS256", SECRET, leeway=LEEWAY)
     self.rsa_token_backend = TokenBackend("RS256", PRIVATE_KEY, PUBLIC_KEY)
     self.aud_iss_token_backend = TokenBackend(
         "RS256", PRIVATE_KEY, PUBLIC_KEY, AUDIENCE, ISSUER
     )
     self.payload = {"foo": "bar"}
     self.backends = (
         self.hmac_token_backend,
         self.rsa_token_backend,
         TokenBackend("ES256", ES256_PRIVATE_KEY, ES256_PUBLIC_KEY),
         TokenBackend("ES384", ES256_PRIVATE_KEY, ES256_PUBLIC_KEY),
         TokenBackend("ES512", ES256_PRIVATE_KEY, ES256_PUBLIC_KEY),
     )
コード例 #12
0
    def decode_token(access_token):
        """
			Method for decoding jwt token
        """
        return TokenBackend(algorithm='HS256').decode(access_token,
                                                      verify=False)
コード例 #13
0
class TestTokenBackend(TestCase):
    def setUp(self):
        self.hmac_token_backend = TokenBackend('HS256', SECRET)
        self.rsa_token_backend = TokenBackend('RS256', PRIVATE_KEY, PUBLIC_KEY)
        self.payload = {'foo': 'bar'}

    def test_init(self):
        # Should reject unknown algorithms
        with self.assertRaises(TokenBackendError):
            TokenBackend('oienarst oieanrsto i', 'not_secret')

        TokenBackend('HS256', 'not_secret')

    def test_encode_hmac(self):
        # Should return a JSON web token for the given payload
        payload = {'exp': make_utc(datetime(year=2000, month=1, day=1))}

        hmac_token = self.hmac_token_backend.encode(payload)

        # Token could be one of two depending on header dict ordering
        self.assertIn(
            hmac_token,
            (
                'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjk0NjY4NDgwMH0.NHpdD2X8ub4SE_MZLBedWa57FCpntGaN_r6f8kNKdUs',
                'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk0NjY4NDgwMH0.jvxQgXCSDToR8uKoRJcMT-LmMJJn2-NM76nfSR2FOgs',
            ),
        )

    def test_encode_rsa(self):
        # Should return a JSON web token for the given payload
        payload = {'exp': make_utc(datetime(year=2000, month=1, day=1))}

        rsa_token = self.rsa_token_backend.encode(payload)

        # Token could be one of two depending on header dict ordering
        self.assertIn(
            rsa_token,
            (
                'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjk0NjY4NDgwMH0.cuE6ocmdxVHXVrGufzn-_ZbXDV475TPPb5jtSacvJsnR3s3tLIm9yR7MF4vGcCAUGJn2hU_JgSOhs9sxntPPVjdwvC3-sxAwfUQ5AgUCEAF5XC7wTvGhmvufzhAgEG_DNsactCh79P8xRnc0iugtlGNyFp_YK582-ZrlfQEp-7C0L9BNG_JCS2J9DsGR7ojO2xGFkFfzezKRkrVTJMPLwpl0JAiZ0iqbQE-Tex7redCrGI388_mgh52GLsNlSIvW2gQMqCVMYndMuYx32Pd5tuToZmLUQ2PJ9RyAZ4fOMApTzoshItg4lGqtnt9CDYzypHULJZohJIPcxFVZZfHxhw',
                'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk0NjY4NDgwMH0.pzHTOaVvKJMMkSqksGh-NdeEvQy8Thre3hBM3smUW5Sohtg77KnHpaUYjq30DyRmYQRmPSjEVprh1Yvic_-OeAXPW8WVsF-r4YdJuxWUpuZbIPwJ9E-cMfTZkDkOl18z1zOdlsLtsP2kXyAlptyy9QQsM7AxoqM6cyXoQ5TI0geWccgoahTy3cBtA6pmjm7H0nfeDGqpqYQBhtaFmRuIWn-_XtdN9C6NVmRCcZwyjH-rP3oEm6wtuKJEN25sVWlZm8YRQ-rj7A7SNqBB5tFK2anM_iv4rmBlIEkmr_f2s_WqMxn2EWUSNeqbqiwabR6CZUyJtKx1cPG0B2PqOTcZsg',
            ),
        )

    def test_decode_hmac_with_no_expiry(self):
        no_exp_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        self.hmac_token_backend.decode(no_exp_token)

    def test_decode_hmac_with_no_expiry_no_verify(self):
        no_exp_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        self.assertEqual(
            self.hmac_token_backend.decode(no_exp_token, verify=False),
            self.payload,
        )

    def test_decode_hmac_with_expiry(self):
        self.payload['exp'] = aware_utcnow() - timedelta(seconds=1)

        expired_token = jwt.encode(self.payload, SECRET, algorithm='HS256')

        with self.assertRaises(TokenBackendError):
            self.hmac_token_backend.decode(expired_token)

    def test_decode_hmac_with_invalid_sig(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, SECRET,
                             algorithm='HS256').decode('utf-8')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, SECRET,
                             algorithm='HS256').decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        with self.assertRaises(TokenBackendError):
            self.hmac_token_backend.decode(invalid_token)

    def test_decode_hmac_with_invalid_sig_no_verify(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, SECRET,
                             algorithm='HS256').decode('utf-8')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, SECRET,
                             algorithm='HS256').decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        self.assertEqual(
            self.hmac_token_backend.decode(invalid_token, verify=False),
            self.payload,
        )

    def test_decode_hmac_success(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        self.payload['foo'] = 'baz'

        token = jwt.encode(self.payload, SECRET,
                           algorithm='HS256').decode('utf-8')

        self.assertEqual(self.hmac_token_backend.decode(token), self.payload)

    def test_decode_rsa_with_no_expiry(self):
        no_exp_token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        self.rsa_token_backend.decode(no_exp_token)

    def test_decode_rsa_with_no_expiry_no_verify(self):
        no_exp_token = jwt.encode(self.payload, PRIVATE_KEY, algorithm='RS256')

        self.assertEqual(
            self.hmac_token_backend.decode(no_exp_token, verify=False),
            self.payload,
        )

    def test_decode_rsa_with_expiry(self):
        self.payload['exp'] = aware_utcnow() - timedelta(seconds=1)

        expired_token = jwt.encode(self.payload,
                                   PRIVATE_KEY,
                                   algorithm='RS256')

        with self.assertRaises(TokenBackendError):
            self.rsa_token_backend.decode(expired_token)

    def test_decode_rsa_with_invalid_sig(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, PRIVATE_KEY,
                             algorithm='RS256').decode('utf-8')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, PRIVATE_KEY,
                             algorithm='RS256').decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        with self.assertRaises(TokenBackendError):
            self.rsa_token_backend.decode(invalid_token)

    def test_decode_rsa_with_invalid_sig_no_verify(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        token_1 = jwt.encode(self.payload, PRIVATE_KEY,
                             algorithm='RS256').decode('utf-8')
        self.payload['foo'] = 'baz'
        token_2 = jwt.encode(self.payload, PRIVATE_KEY,
                             algorithm='RS256').decode('utf-8')

        token_2_payload = token_2.rsplit('.', 1)[0]
        token_1_sig = token_1.rsplit('.', 1)[-1]
        invalid_token = token_2_payload + '.' + token_1_sig

        self.assertEqual(
            self.hmac_token_backend.decode(invalid_token, verify=False),
            self.payload,
        )

    def test_decode_rsa_success(self):
        self.payload['exp'] = aware_utcnow() + timedelta(days=1)
        self.payload['foo'] = 'baz'

        token = jwt.encode(self.payload, PRIVATE_KEY,
                           algorithm='RS256').decode('utf-8')

        self.assertEqual(self.rsa_token_backend.decode(token), self.payload)