Пример #1
0
    def from_dict(cls, D):
        D = D.copy()

        if 'k' in D:
            D['k'] = b64_decode(D['k'])

        return super(OctKey, cls).from_dict(D)
Пример #2
0
    def from_dict(cls, D):
        D = D.copy()

        if "k" in D:
            D["k"] = b64_decode(D["k"])

        return super(OctKey, cls).from_dict(D)
Пример #3
0
 def test_b64_decode(self):
     ret = b'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQog' \
         + b'Imh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ'
     self.assertEqual(
         utils.b64_decode(ret),
         b'{"iss":"joe",\r\n "exp":1300819380,\r\n '
         b'"http://example.com/is_root":true}'
     )
Пример #4
0
    def decode(self, headerobj, rest):
        assert isinstance(headerobj, dict)

        try:
            encoded_payload, _ = rest.split('.')
            return b64_decode(encoded_payload)
        except ValueError as why:
            raise MalformedJWT()
Пример #5
0
    def verify(self, jwt):
        encoded_header, rest = jwt.split('.', 1)
        headerobj = json.loads(b64_decode(encoded_header).decode('utf8'))
        impl = self._get_impl(headerobj['alg'])

        if headerobj.get('cty') == 'JWT':
            return self.verify(impl.decode(headerobj, rest).decode('utf8'))

        return impl.verify(headerobj, encoded_header, rest)
Пример #6
0
    def verify(self, jwt):
        assert isinstance(jwt, str)

        encoded_header, rest = jwt.split('.', 1)
        headerobj = json.loads(b64_decode(encoded_header).decode('utf8'))
        impl = self._get_impl(headerobj['alg'])

        if headerobj.get('cty') == 'JWT':
            jwt = impl.decode(headerobj, rest)
            return self.verify(str(jwt.decode('utf8')))

        return impl.verify(headerobj, encoded_header, rest)
Пример #7
0
    def decode(self, jwt):
        encoded_header, rest = jwt.split('.', 1)
        headerobj = json.loads(b64_decode(encoded_header).decode('utf8'))
        impl = self._get_impl(headerobj['alg'])

        if not impl.verify(headerobj, encoded_header, rest):
            raise MalformedJWT()

        payload = impl.decode(headerobj, rest)

        if headerobj.get('cty') == 'JWT':
            return self.decode(payload.decode('utf8'))

        return payload
Пример #8
0
    def verify(self, headerobj, encoded_header, rest):
        assert isinstance(headerobj, dict)

        try:
            encoded_payload, encoded_signature = rest.split('.')
            signature = b64_decode(encoded_signature)
        except ValueError as why:
            raise MalformedJWT()
        else:
            msg = self._signing_message(encoded_header, encoded_payload)

            signer = self.get_signer(headerobj['alg'])
            if headerobj['alg'] == 'none':
                return signer[1](None, msg, signature)

            key = self.get_key(headerobj['alg'], headerobj.get('kid'))
            return signer[1](key.keyobj, msg, signature)
Пример #9
0
    def verify(self, headerobj, encoded_header, rest):
        assert isinstance(headerobj, dict)
        assert isinstance(encoded_header, str)
        assert isinstance(rest, str)

        try:
            encoded_payload, encoded_signature = rest.split(".")
            signature = b64_decode(encoded_signature)
        except ValueError as why:
            raise MalformedJWT()
        else:
            msg = self._signing_message(encoded_header, encoded_payload)

            signer = self.get_signer(headerobj["alg"])
            if headerobj["alg"] == "none":
                return signer.verify(None, msg, signature)

            key = self.get_key(headerobj["alg"], headerobj.get("kid"))
            return signer.verify(key.keyobj, msg, signature)
Пример #10
0
 def test_from_dict(self):
     inst = self.target.from_dict(self.jwk)
     self.assertEqual(inst.kty, 'oct')
     self.assertEqual(inst.kid, 'HMAC key used in JWS A.1 example')
     self.assertEqual(inst.k, b64_decode(self.jwk['k']))