How to use the jwt.algorithms.HMACAlgorithm function in jwt

To help you get started, we’ve selected a few jwt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jpadilla / pyjwt / tests / test_algorithms.py View on Github external
def test_hmac_from_jwk_should_raise_exception_if_not_hmac_key(self):
        algo = HMACAlgorithm(HMACAlgorithm.SHA256)

        with open(key_path("jwk_rsa_pub.json"), "r") as keyfile:
            with pytest.raises(InvalidKeyError):
                algo.from_jwk(keyfile.read())
github jpadilla / pyjwt / tests / test_algorithms.py View on Github external
def test_hmac_should_accept_unicode_key(self):
        algo = HMACAlgorithm(HMACAlgorithm.SHA256)

        algo.prepare_key(force_unicode("awesome"))
github jpadilla / pyjwt / tests / test_algorithms.py View on Github external
def test_hmac_should_throw_exception_if_key_is_ssh_public_key(self):
        algo = HMACAlgorithm(HMACAlgorithm.SHA256)

        with pytest.raises(InvalidKeyError):
            with open(key_path("testkey_rsa.pub"), "r") as keyfile:
                algo.prepare_key(keyfile.read())
github jpadilla / pyjwt / tests / test_algorithms.py View on Github external
def test_hmac_should_reject_nonstring_key(self):
        algo = HMACAlgorithm(HMACAlgorithm.SHA256)

        with pytest.raises(TypeError) as context:
            algo.prepare_key(object())

        exception = context.value
        assert str(exception) == "Expected a string value"
github NYPL-Simplified / circulation / api / adobe_vendor_id.py View on Github external
# for other libraries.
        for uri, v in other_libraries.items():
            short_name, secret = v
            short_name = short_name.upper()
            if short_name in self.library_uris_by_short_name:
                # This can happen if the same library is in the list
                # twice, capitalized differently.
                raise ValueError(
                    "Duplicate short name: %s" % short_name
                )
            self.library_uris_by_short_name[short_name] = uri
            self.secrets_by_library_uri[uri] = secret

        self.log = logging.getLogger("Adobe authdata utility")

        self.short_token_signer = HMACAlgorithm(HMACAlgorithm.SHA256)
        self.short_token_signing_key = self.short_token_signer.prepare_key(
            self.secret
        )
github jpadilla / pyjwt / jwt / algorithms.py View on Github external
def get_default_algorithms():
    """
    Returns the algorithms that are implemented by the library.
    """
    default_algorithms = {
        "none": NoneAlgorithm(),
        "HS256": HMACAlgorithm(HMACAlgorithm.SHA256),
        "HS384": HMACAlgorithm(HMACAlgorithm.SHA384),
        "HS512": HMACAlgorithm(HMACAlgorithm.SHA512),
    }

    if has_crypto:
        default_algorithms.update(
            {
                "RS256": RSAAlgorithm(RSAAlgorithm.SHA256),
                "RS384": RSAAlgorithm(RSAAlgorithm.SHA384),
                "RS512": RSAAlgorithm(RSAAlgorithm.SHA512),
                "ES256": ECAlgorithm(ECAlgorithm.SHA256),
                "ES384": ECAlgorithm(ECAlgorithm.SHA384),
                "ES521": ECAlgorithm(ECAlgorithm.SHA512),
                "ES512": ECAlgorithm(
                    ECAlgorithm.SHA512
                ),  # Backward compat for #219 fix
github jpadilla / pyjwt / jwt / algorithms.py View on Github external
def get_default_algorithms():
    """
    Returns the algorithms that are implemented by the library.
    """
    default_algorithms = {
        "none": NoneAlgorithm(),
        "HS256": HMACAlgorithm(HMACAlgorithm.SHA256),
        "HS384": HMACAlgorithm(HMACAlgorithm.SHA384),
        "HS512": HMACAlgorithm(HMACAlgorithm.SHA512),
    }

    if has_crypto:
        default_algorithms.update(
            {
                "RS256": RSAAlgorithm(RSAAlgorithm.SHA256),
                "RS384": RSAAlgorithm(RSAAlgorithm.SHA384),
                "RS512": RSAAlgorithm(RSAAlgorithm.SHA512),
                "ES256": ECAlgorithm(ECAlgorithm.SHA256),
                "ES384": ECAlgorithm(ECAlgorithm.SHA384),
                "ES521": ECAlgorithm(ECAlgorithm.SHA512),
                "ES512": ECAlgorithm(
                    ECAlgorithm.SHA512
                ),  # Backward compat for #219 fix
                "PS256": RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256),
github NYPL-Simplified / circulation / api / adobe_vendor_id.py View on Github external
# for other libraries.
        for uri, v in other_libraries.items():
            short_name, secret = v
            short_name = short_name.upper()
            if short_name in self.library_uris_by_short_name:
                # This can happen if the same library is in the list
                # twice, capitalized differently.
                raise ValueError(
                    "Duplicate short name: %s" % short_name
                )
            self.library_uris_by_short_name[short_name] = uri
            self.secrets_by_library_uri[uri] = secret
        
        self.log = logging.getLogger("Adobe authdata utility")

        self.short_token_signer = HMACAlgorithm(HMACAlgorithm.SHA256)
        self.short_token_signing_key = self.short_token_signer.prepare_key(
            self.secret
        )