本文整理汇总了Python中google.auth.jwt.encode方法的典型用法代码示例。如果您正苦于以下问题:Python jwt.encode方法的具体用法?Python jwt.encode怎么用?Python jwt.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.auth.jwt
的用法示例。
在下文中一共展示了jwt.encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_id_token_jwt_grant
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_id_token_jwt_grant():
now = _helpers.utcnow()
id_token_expiry = _helpers.datetime_to_secs(now)
id_token = jwt.encode(SIGNER, {"exp": id_token_expiry}).decode("utf-8")
request = make_request({"id_token": id_token, "extra": "data"})
token, expiry, extra_data = _client.id_token_jwt_grant(
request, "http://example.com", "assertion_value"
)
# Check request call
verify_request_params(
request, {"grant_type": _client._JWT_GRANT_TYPE, "assertion": "assertion_value"}
)
# Check result
assert token == id_token
# JWT does not store microseconds
now = now.replace(microsecond=0)
assert expiry == now
assert extra_data["extra"] == "data"
示例2: token_factory
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def token_factory(signer, es256_signer):
def factory(claims=None, key_id=None, use_es256_signer=False):
now = _helpers.datetime_to_secs(_helpers.utcnow())
payload = {
"aud": "audience@example.com",
"iat": now,
"exp": now + 300,
"user": "billy bob",
"metadata": {"meta": "data"},
}
payload.update(claims or {})
# False is specified to remove the signer's key id for testing
# headers without key ids.
if key_id is False:
signer._key_id = None
key_id = None
if use_es256_signer:
return jwt.encode(es256_signer, payload, key_id=key_id)
else:
return jwt.encode(signer, payload, key_id=key_id)
return factory
示例3: test_before_request
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_before_request(self):
headers = {}
self.credentials.refresh(None)
self.credentials.before_request(
None, "GET", "http://example.com?a=1#3", headers
)
header_value = headers["authorization"]
_, token = header_value.split(" ")
# Since the audience is set, it should use the existing token.
assert token.encode("utf-8") == self.credentials.token
payload = self._verify_token(token)
assert payload["aud"] == self.AUDIENCE
示例4: test__token_endpoint_request
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test__token_endpoint_request():
request = make_request({"test": "response"})
result = _client._token_endpoint_request(
request, "http://example.com", {"test": "params"}
)
# Check request call
request.assert_called_with(
method="POST",
url="http://example.com",
headers={"content-type": "application/x-www-form-urlencoded"},
body="test=params".encode("utf-8"),
)
# Check result
assert result == {"test": "response"}
示例5: _make_jwt
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def _make_jwt(self):
"""Make a signed JWT.
Returns:
Tuple[bytes, datetime]: The encoded JWT and the expiration.
"""
now = _helpers.utcnow()
lifetime = datetime.timedelta(seconds=self._token_lifetime)
expiry = now + lifetime
payload = {
"iss": self._issuer,
"sub": self._subject,
"iat": _helpers.datetime_to_secs(now),
"exp": _helpers.datetime_to_secs(expiry),
"aud": self._audience,
}
payload.update(self._additional_claims)
jwt = encode(self._signer, payload)
return jwt, expiry
示例6: fake_token
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def fake_token(signer):
now = calendar.timegm(datetime.datetime.utcnow().utctimetuple())
payload = {
'aud': 'example.com',
'azp': '1234567890',
'email': 'pubsub@example.iam.gserviceaccount.com',
'email_verified': True,
'iat': now,
'exp': now + 3600,
'iss': 'https://accounts.google.com',
'sub': '1234567890'
}
header = {
'alg': 'RS256',
'kid': signer.key_id,
'typ': 'JWT'
}
yield jwt.encode(signer, payload, header=header)
示例7: _make_jwt
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def _make_jwt(self):
"""Make a signed JWT.
Returns:
Tuple[bytes, datetime]: The encoded JWT and the expiration.
"""
now = _helpers.utcnow()
lifetime = datetime.timedelta(seconds=self._token_lifetime)
expiry = now + lifetime
payload = {
'iss': self._issuer,
'sub': self._subject,
'iat': _helpers.datetime_to_secs(now),
'exp': _helpers.datetime_to_secs(expiry),
'aud': self._audience,
}
payload.update(self._additional_claims)
jwt = encode(self._signer, payload)
return jwt, expiry
示例8: _get_id_token
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def _get_id_token(payload_overrides=None, header_overrides=None):
signer = crypt.RSASigner.from_string(MOCK_PRIVATE_KEY)
headers = {
'kid': 'mock-key-id-1'
}
payload = {
'aud': MOCK_CREDENTIAL.project_id,
'iss': 'https://securetoken.google.com/' + MOCK_CREDENTIAL.project_id,
'iat': int(time.time()) - 100,
'exp': int(time.time()) + 3600,
'sub': '1234567890',
'admin': True,
'firebase': {
'sign_in_provider': 'provider',
},
}
if header_overrides:
headers = _merge_jwt_claims(headers, header_overrides)
if payload_overrides:
payload = _merge_jwt_claims(payload, payload_overrides)
return jwt.encode(signer, payload, header=headers)
示例9: test_encode_basic
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_encode_basic(signer):
test_payload = {"test": "value"}
encoded = jwt.encode(signer, test_payload)
header, payload, _, _ = jwt._unverified_decode(encoded)
assert payload == test_payload
assert header == {"typ": "JWT", "alg": "RS256", "kid": signer.key_id}
示例10: test_encode_extra_headers
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_encode_extra_headers(signer):
encoded = jwt.encode(signer, {}, header={"extra": "value"})
header = jwt.decode_header(encoded)
assert header == {
"typ": "JWT",
"alg": "RS256",
"kid": signer.key_id,
"extra": "value",
}
示例11: test_encode_basic_es256
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_encode_basic_es256(es256_signer):
test_payload = {"test": "value"}
encoded = jwt.encode(es256_signer, test_payload)
header, payload, _, _ = jwt._unverified_decode(encoded)
assert payload == test_payload
assert header == {"typ": "JWT", "alg": "ES256", "kid": es256_signer.key_id}
示例12: test_decode_bad_token_no_iat_or_exp
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_decode_bad_token_no_iat_or_exp(signer):
token = jwt.encode(signer, {"test": "value"})
with pytest.raises(ValueError) as excinfo:
jwt.decode(token, PUBLIC_CERT_BYTES)
assert excinfo.match(r"Token does not contain required claim")
示例13: test_decode_missing_crytography_alg
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def test_decode_missing_crytography_alg(monkeypatch):
monkeypatch.delitem(jwt._ALGORITHM_TO_VERIFIER_CLASS, "ES256")
headers = json.dumps({u"kid": u"1", u"alg": u"ES256"})
token = b".".join(
map(lambda seg: base64.b64encode(seg.encode("utf-8")), [headers, u"{}", u"sig"])
)
with pytest.raises(ValueError) as excinfo:
jwt.decode(token)
assert excinfo.match(r"cryptography")
示例14: make_request
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def make_request(response_data, status=http_client.OK):
response = mock.create_autospec(transport.Response, instance=True)
response.status = status
response.data = json.dumps(response_data).encode("utf-8")
request = mock.create_autospec(transport.Request)
request.return_value = response
return request
示例15: _make_jwt_for_audience
# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import encode [as 别名]
def _make_jwt_for_audience(self, audience):
"""Make a new JWT for the given audience.
Args:
audience (str): The intended audience.
Returns:
Tuple[bytes, datetime]: The encoded JWT and the expiration.
"""
now = _helpers.utcnow()
lifetime = datetime.timedelta(seconds=self._token_lifetime)
expiry = now + lifetime
payload = {
"iss": self._issuer,
"sub": self._subject,
"iat": _helpers.datetime_to_secs(now),
"exp": _helpers.datetime_to_secs(expiry),
"aud": audience,
}
payload.update(self._additional_claims)
jwt = encode(self._signer, payload)
return jwt, expiry