Esempio n. 1
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())
Esempio n. 2
0
    def test_verify_failure_invalid_audience_no_audience_expected(self):
        """
            Test verifying a token with an audience claim, but without expecting one when verifying the token.

            Expected Result: An `InvalidAudienceError` is raised.
        """

        easyjwt_creation = EasyJWT(self.key)
        easyjwt_creation.audience = self.audience
        token = easyjwt_creation.create()

        with self.assertRaises(InvalidAudienceError):
            easyjwt_verification = EasyJWT.verify(token, self.key)
            self.assertIsNone(easyjwt_verification)
Esempio n. 3
0
    def test_verify_failure_invalid_audience_wrong_type(self):
        """
            Test verifying a token with an audience claim, but giving an audience of a wrong type.

            Expected Result: An `InvalidAudienceError` is raised.
        """

        easyjwt_creation = EasyJWT(self.key)
        easyjwt_creation.audience = self.audience
        token = easyjwt_creation.create()

        with self.assertRaises(InvalidAudienceError):
            # noinspection PyTypeChecker
            easyjwt_verification = EasyJWT.verify(token,
                                                  self.key,
                                                  audience=42.1337)
            self.assertIsNone(easyjwt_verification)
Esempio n. 4
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))
Esempio n. 5
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))
Esempio n. 6
0
    def test_verify_success_with_validated_registered_claims(self):
        """
            Test verifying a valid token with valid 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.audience = self.audience
        easyjwt_creation.expiration_date = self.expiration_date
        easyjwt_creation.issuer = self.issuer
        easyjwt_creation.JWT_ID = self.JWT_ID
        easyjwt_creation.not_before_date = self.not_before_date
        easyjwt_creation.subject = self.subject
        token = easyjwt_creation.create()

        easyjwt_verification = EasyJWT.verify(token,
                                              self.key,
                                              issuer=self.issuer,
                                              audience=self.audience)
        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)