Beispiel #1
0
    def test_init_with_timedelta(self):
        now = make_utc(datetime(year=2000, month=1, day=1))
        token = AccessToken(lifetime=timedelta(minutes=10))
        token.current_time = now
        token.set_exp()

        self.assertEqual(token['exp'],
                         datetime_to_epoch(now + timedelta(minutes=10)))
Beispiel #2
0
    def access_token(self) -> AccessToken:
        access = AccessToken()
        access.set_exp(from_time=self.current_time)
        no_copy = self.no_copy_claims

        for claim, value in self.payload.items():
            if claim in no_copy:
                continue
            access[claim] = value

        return access
Beispiel #3
0
    async def test_valid_token(self):
        """With a valid token the connection is accepted."""
        token = AccessToken()
        token.set_exp(lifetime=timedelta(minutes=20))

        application = JWTMiddleware(AsyncWebsocketConsumer())
        comminucator = WebsocketCommunicator(application, f"/?jwt={token}")

        connected, _ = await comminucator.connect()
        self.assertTrue(connected)
        await comminucator.disconnect()
Beispiel #4
0
    async def test_invalid_token(self):
        """With an invalid token the connection is refused."""
        token = AccessToken()
        token.set_exp(
            from_time=timezone.now() - timedelta(minutes=30),
            lifetime=timedelta(minutes=1),
        )

        application = JWTMiddleware(AsyncWebsocketConsumer())
        comminucator = WebsocketCommunicator(application, f"/?jwt={token}")

        connected, _ = await comminucator.connect()
        self.assertFalse(connected)
        await comminucator.disconnect()
Beispiel #5
0
    def post(self, request):
        refresh_token_string = request.data.get('refresh')
        access_token_string = request.data.get('access')

        if not refresh_token_string:
            return Response(status=status.HTTP_401_UNAUTHORIZED)

        try:
            access_token = AccessToken(access_token_string)
            access_token.set_exp(now())

            refresh_token = RefreshToken(refresh_token_string)
            refresh_token.blacklist()
        except TokenError:
            pass

        return Response(status=status.HTTP_200_OK)
Beispiel #6
0
    async def test_invalid_token(self):
        """With an invalid token the connection is refused."""
        token = AccessToken()
        token.set_exp(
            from_time=timezone.now() - timedelta(minutes=30),
            lifetime=timedelta(minutes=1),
        )

        application = JWTMiddleware(AsyncWebsocketConsumer())
        communicator = WebsocketCommunicator(application, f"/?jwt={token}")

        connected, _ = await communicator.connect()
        self.assertTrue(connected)

        response = await communicator.receive_output()

        self.assertEqual(response["type"], "websocket.close")
        self.assertEqual(response["code"], 4003)
        await communicator.disconnect()