當前位置: 首頁>>代碼示例>>Python>>正文


Python jwt.encode方法代碼示例

本文整理匯總了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" 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:23,代碼來源:test__client.py

示例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 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:26,代碼來源:test_jwt.py

示例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 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:18,代碼來源:test_jwt.py

示例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"} 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:19,代碼來源:test__client.py

示例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 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:25,代碼來源:jwt.py

示例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) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:20,代碼來源:main_test.py

示例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 
開發者ID:fniephaus,項目名稱:alfred-gmail,代碼行數:25,代碼來源:jwt.py

示例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) 
開發者ID:firebase,項目名稱:firebase-admin-python,代碼行數:23,代碼來源:test_token_gen.py

示例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} 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:8,代碼來源:test_jwt.py

示例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",
    } 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:11,代碼來源:test_jwt.py

示例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} 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:8,代碼來源:test_jwt.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:7,代碼來源:test_jwt.py

示例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") 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:12,代碼來源:test_jwt.py

示例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 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:9,代碼來源:test__client.py

示例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 
開發者ID:googleapis,項目名稱:google-auth-library-python,代碼行數:28,代碼來源:jwt.py


注:本文中的google.auth.jwt.encode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。