当前位置: 首页>>代码示例>>Python>>正文


Python jwt.decode方法代码示例

本文整理汇总了Python中google.auth.jwt.decode方法的典型用法代码示例。如果您正苦于以下问题:Python jwt.decode方法的具体用法?Python jwt.decode怎么用?Python jwt.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google.auth.jwt的用法示例。


在下文中一共展示了jwt.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_with_service_account

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_with_service_account(self, sign, get, utcnow):
        sign.side_effect = [b"signature"]

        request = mock.create_autospec(transport.Request, instance=True)
        self.credentials = credentials.IDTokenCredentials(
            request=request,
            target_audience="https://audience.com",
            service_account_email="service-account@other.com",
        )

        # Generate authorization grant:
        token = self.credentials._make_authorization_grant_assertion()
        payload = jwt.decode(token, verify=False)

        # The JWT token signature is 'signature' encoded in base 64:
        assert token.endswith(b".c2lnbmF0dXJl")

        # Check that the credentials have the token and proper expiration
        assert payload == {
            "aud": "https://www.googleapis.com/oauth2/v4/token",
            "exp": 3600,
            "iat": 0,
            "iss": "service-account@other.com",
            "target_audience": "https://audience.com",
        } 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:27,代码来源:test_credentials.py

示例2: sign_bytes

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def sign_bytes(self, message):

        iam_sign_endpoint = _IAM_SIGN_ENDPOINT.format(self._target_principal)

        body = {
            "payload": base64.b64encode(message).decode("utf-8"),
            "delegates": self._delegates,
        }

        headers = {"Content-Type": "application/json"}

        authed_session = AuthorizedSession(self._source_credentials)

        response = authed_session.post(
            url=iam_sign_endpoint, headers=headers, json=body
        )

        return base64.b64decode(response.json()["signedBlob"]) 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:20,代码来源:impersonated_credentials.py

示例3: _fetch_certs

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def _fetch_certs(request, certs_url):
    """Fetches certificates.

    Google-style cerificate endpoints return JSON in the format of
    ``{'key id': 'x509 certificate'}``.

    Args:
        request (google.auth.transport.Request): The object used to make
            HTTP requests.
        certs_url (str): The certificate endpoint URL.

    Returns:
        Mapping[str, str]: A mapping of public key ID to x.509 certificate
            data.
    """
    response = request(certs_url, method="GET")

    if response.status != http_client.OK:
        raise exceptions.TransportError(
            "Could not fetch certificates at {}".format(certs_url)
        )

    return json.loads(response.data.decode("utf-8")) 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:25,代码来源:id_token.py

示例4: verify_token

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def verify_token(id_token, request, audience=None, certs_url=_GOOGLE_OAUTH2_CERTS_URL):
    """Verifies an ID token and returns the decoded token.

    Args:
        id_token (Union[str, bytes]): The encoded token.
        request (google.auth.transport.Request): The object used to make
            HTTP requests.
        audience (str): The audience that this token is intended for. If None
            then the audience is not verified.
        certs_url (str): The URL that specifies the certificates to use to
            verify the token. This URL should return JSON in the format of
            ``{'key id': 'x509 certificate'}``.

    Returns:
        Mapping[str, Any]: The decoded token.
    """
    certs = _fetch_certs(request, certs_url)

    return jwt.decode(id_token, certs=certs, audience=audience) 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:21,代码来源:id_token.py

示例5: _validate_iap_jwt

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def _validate_iap_jwt(iap_jwt, expected_audience):
    try:
        # Retrieve public key for token signature verification.
        key_id = jwt.decode_header(iap_jwt).get('kid')
        if not key_id:
            return (None, None, '**ERROR: no key ID**')
        key = get_iap_key(key_id)

        # Verify token signature, expiry and audience.
        decoded_jwt = jwt.decode(iap_jwt, certs=key, audience=expected_audience)

        # Verify token issuer.
        if decoded_jwt.get('iss') != 'https://cloud.google.com/iap':
            return (None, None, '**ERROR: invalid issuer**')

        return (decoded_jwt['sub'], decoded_jwt['email'], '')
    except (ValueError, requests.exceptions.RequestException) as e:
        return (None, None, '**ERROR: JWT validation error {}**'.format(e)) 
开发者ID:GoogleCloudPlatform,项目名称:python-docs-samples,代码行数:20,代码来源:validate_jwt.py

示例6: _fetch_certs

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def _fetch_certs(request, certs_url):
    """Fetches certificates.

    Google-style cerificate endpoints return JSON in the format of
    ``{'key id': 'x509 certificate'}``.

    Args:
        request (google.auth.transport.Request): The object used to make
            HTTP requests.
        certs_url (str): The certificate endpoint URL.

    Returns:
        Mapping[str, str]: A mapping of public key ID to x.509 certificate
            data.
    """
    response = request(certs_url, method='GET')

    if response.status != http_client.OK:
        raise exceptions.TransportError(
            'Could not fetch certificates at {}'.format(certs_url))

    return json.loads(response.data.decode('utf-8')) 
开发者ID:fniephaus,项目名称:alfred-gmail,代码行数:24,代码来源:id_token.py

示例7: verify_token

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def verify_token(id_token, request, audience=None,
                 certs_url=_GOOGLE_OAUTH2_CERTS_URL):
    """Verifies an ID token and returns the decoded token.

    Args:
        id_token (Union[str, bytes]): The encoded token.
        request (google.auth.transport.Request): The object used to make
            HTTP requests.
        audience (str): The audience that this token is intended for. If None
            then the audience is not verified.
        certs_url (str): The URL that specifies the certificates to use to
            verify the token. This URL should return JSON in the format of
            ``{'key id': 'x509 certificate'}``.

    Returns:
        Mapping[str, Any]: The decoded token.
    """
    certs = _fetch_certs(request, certs_url)

    return jwt.decode(id_token, certs=certs, audience=audience) 
开发者ID:luci,项目名称:luci-py,代码行数:22,代码来源:id_token.py

示例8: decode_token

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def decode_token(token, client_id):
    decoded = jwt.decode(token, certs=GOOGLE_PUBLIC_KEY, verify=True, audience=client_id)
    return decoded 
开发者ID:treethought,项目名称:flask-assistant,代码行数:5,代码来源:utils.py

示例9: test_decode_valid

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_valid(token_factory):
    payload = jwt.decode(token_factory(), certs=PUBLIC_CERT_BYTES)
    assert payload["aud"] == "audience@example.com"
    assert payload["user"] == "billy bob"
    assert payload["metadata"]["meta"] == "data" 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:7,代码来源:test_jwt.py

示例10: test_decode_valid_es256

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_valid_es256(token_factory):
    payload = jwt.decode(
        token_factory(use_es256_signer=True), certs=EC_PUBLIC_CERT_BYTES
    )
    assert payload["aud"] == "audience@example.com"
    assert payload["user"] == "billy bob"
    assert payload["metadata"]["meta"] == "data" 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:9,代码来源:test_jwt.py

示例11: test_decode_valid_with_audience

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_valid_with_audience(token_factory):
    payload = jwt.decode(
        token_factory(), certs=PUBLIC_CERT_BYTES, audience="audience@example.com"
    )
    assert payload["aud"] == "audience@example.com"
    assert payload["user"] == "billy bob"
    assert payload["metadata"]["meta"] == "data" 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:9,代码来源:test_jwt.py

示例12: test_decode_valid_unverified

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_valid_unverified(token_factory):
    payload = jwt.decode(token_factory(), certs=OTHER_CERT_BYTES, verify=False)
    assert payload["aud"] == "audience@example.com"
    assert payload["user"] == "billy bob"
    assert payload["metadata"]["meta"] == "data" 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:7,代码来源:test_jwt.py

示例13: test_decode_bad_token_wrong_number_of_segments

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_bad_token_wrong_number_of_segments():
    with pytest.raises(ValueError) as excinfo:
        jwt.decode("1.2", PUBLIC_CERT_BYTES)
    assert excinfo.match(r"Wrong number of segments") 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:6,代码来源:test_jwt.py

示例14: test_decode_bad_token_not_json

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [as 别名]
def test_decode_bad_token_not_json():
    token = b".".join([base64.urlsafe_b64encode(b"123!")] * 3)
    with pytest.raises(ValueError) as excinfo:
        jwt.decode(token, PUBLIC_CERT_BYTES)
    assert excinfo.match(r"Can\'t parse segment") 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:7,代码来源:test_jwt.py

示例15: test_decode_bad_token_no_iat_or_exp

# 需要导入模块: from google.auth import jwt [as 别名]
# 或者: from google.auth.jwt import decode [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") 
开发者ID:googleapis,项目名称:google-auth-library-python,代码行数:7,代码来源:test_jwt.py


注:本文中的google.auth.jwt.decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。