Пример #1
0
    def test_verify_success_without_validated_registered_claims(self):
        """
            Test verifying a valid token without registered claims that are validated (exp, iss, nbf), using the correct
            key.

            Expected Result: An object representing the token is returned.
        """

        easyjwt_creation = EasyJWT(self.key)
        easyjwt_creation.JWT_ID = self.JWT_ID
        easyjwt_creation.subject = self.subject
        token = easyjwt_creation.create()

        easyjwt_verification = EasyJWT.verify(token, self.key)
        self.assertIsNotNone(easyjwt_verification)
        self.assertEqual(easyjwt_creation._key, easyjwt_verification._key)
        self.assertEqual(easyjwt_creation.audience,
                         easyjwt_verification.audience)
        self.assertEqual(easyjwt_creation.expiration_date,
                         easyjwt_verification.expiration_date)
        self.assertEqual(easyjwt_creation.issued_at_date,
                         easyjwt_verification.issued_at_date)
        self.assertEqual(easyjwt_creation.issuer, easyjwt_verification.issuer)
        self.assertEqual(easyjwt_creation.JWT_ID, easyjwt_verification.JWT_ID)
        self.assertEqual(easyjwt_creation.not_before_date,
                         easyjwt_verification.not_before_date)
        self.assertEqual(easyjwt_creation.subject,
                         easyjwt_verification.subject)
        self.assertEqual(easyjwt_creation._easyjwt_class,
                         easyjwt_verification._easyjwt_class)
Пример #2
0
    def test_verify_success_lenient_verification(self):
        """
            Test verifying a token without the `_easyjwt_class` claim with strict verification disabled.

            Expected Result: The token is successfully verified and an object representing the token is returned.
        """

        EasyJWT.strict_verification = False

        easyjwt_creation = EasyJWT(self.key)
        easyjwt_creation.JWT_ID = self.JWT_ID
        easyjwt_creation.subject = self.subject
        token = easyjwt_creation.create()

        easyjwt_verification = EasyJWT.verify(token, self.key)
        self.assertIsNotNone(easyjwt_verification)
        self.assertEqual(easyjwt_creation._key, easyjwt_verification._key)
        self.assertEqual(easyjwt_creation.audience,
                         easyjwt_verification.audience)
        self.assertEqual(easyjwt_creation.expiration_date,
                         easyjwt_verification.expiration_date)
        self.assertEqual(easyjwt_creation.issued_at_date,
                         easyjwt_verification.issued_at_date)
        self.assertEqual(easyjwt_creation.issuer, easyjwt_verification.issuer)
        self.assertEqual(easyjwt_creation.JWT_ID, easyjwt_verification.JWT_ID)
        self.assertEqual(easyjwt_creation.not_before_date,
                         easyjwt_verification.not_before_date)
        self.assertEqual(easyjwt_creation.subject,
                         easyjwt_verification.subject)
        self.assertNotIn('_easyjwt_class', vars(easyjwt_verification))
Пример #3
0
    def test_get_claim_set_with_optional_claims(self):
        """
            Test getting the claim set if optional claims are set.

            Expected Result: A dictionary with the entries for the class and the optional claims is returned.
        """

        claim_set = dict(
            _easyjwt_class='EasyJWT',
            aud=self.audience,
            exp=self.expiration_date,
            iat=self.issued_at_date,
            iss=self.issuer,
            jti=self.JWT_ID,
            nbf=self.not_before_date,
            sub=self.subject,
        )

        easyjwt = EasyJWT(self.key)
        easyjwt.audience = self.audience
        easyjwt.expiration_date = self.expiration_date
        easyjwt.issued_at_date = self.issued_at_date
        easyjwt.issuer = self.issuer
        easyjwt.JWT_ID = self.JWT_ID
        easyjwt.not_before_date = self.not_before_date
        easyjwt.subject = self.subject

        self.assertDictEqual(claim_set, easyjwt._get_claim_set())
Пример #4
0
    def test_create_success_lenient_verification(self):
        """
            Test creating a token with strict verification disabled.

            Expected Result: A token is created successfully. The create token can be decoded.
        """

        EasyJWT.strict_verification = False

        easyjwt = EasyJWT(self.key)
        easyjwt.expiration_date = self.expiration_date
        easyjwt.issuer = self.issuer
        easyjwt.JWT_ID = self.JWT_ID
        easyjwt.not_before_date = self.not_before_date
        easyjwt.subject = self.subject

        token = easyjwt.create()
        self.assertIsNotNone(token)

        self.assertIsNotNone(easyjwt.issued_at_date)

        claim_set = decode(token,
                           self.key,
                           algorithms=easyjwt._get_decode_algorithms())
        self.assertIsNotNone(claim_set)
Пример #5
0
    def test_verify_claim_set_success_with_optional_claims(self):
        """
            Test verifying a valid claim set containing (valid) optional claims.

            Expected result: `True`
        """

        easyjwt = EasyJWT(self.key)
        easyjwt.audience = self.audience
        easyjwt.expiration_date = self.expiration_date
        easyjwt.issued_at_date = self.issued_at_date
        easyjwt.issuer = self.issuer
        easyjwt.JWT_ID = self.JWT_ID
        easyjwt.not_before_date = self.not_before_date
        easyjwt.subject = self.subject

        claim_set = easyjwt._get_claim_set()
        self.assertTrue(easyjwt._verify_claim_set(claim_set))
Пример #6
0
    def test_verify_claim_set_success_lenient_verification(self):
        """
            Test verifying a valid claim set without an `_easyjwt_class` claim with strict verification disabled.

            Expected result: `True`
        """

        EasyJWT.strict_verification = False

        easyjwt = EasyJWT(self.key)
        easyjwt.audience = self.audience
        easyjwt.expiration_date = self.expiration_date
        easyjwt.issued_at_date = self.issued_at_date
        easyjwt.issuer = self.issuer
        easyjwt.JWT_ID = self.JWT_ID
        easyjwt.not_before_date = self.not_before_date
        easyjwt.subject = self.subject

        claim_set = easyjwt._get_claim_set()
        self.assertTrue(easyjwt._verify_claim_set(claim_set))
Пример #7
0
    def test_create_success_without_issued_at_date(self):
        """
             Test creating a token without specifying an issued-at date.

             Expected Result: A token is created. The created token can be decoded.
        """

        easyjwt = EasyJWT(self.key)
        easyjwt.expiration_date = self.expiration_date
        easyjwt.issuer = self.issuer
        easyjwt.JWT_ID = self.JWT_ID
        easyjwt.not_before_date = self.not_before_date
        easyjwt.subject = self.subject

        token = easyjwt.create()
        self.assertIsNotNone(token)

        self.assertIsNotNone(easyjwt.issued_at_date)

        claim_set = decode(token,
                           self.key,
                           algorithms=easyjwt._get_decode_algorithms())
        self.assertIsNotNone(claim_set)