5454_PKCS12_KEY = '_private_key_pkcs12'
5555
5656
57+ def _get_signer (pkcs8_pem , pkcs12 , password ):
58+ """Get a signer from the ``crypt`` module.
59+
60+ Will be used by :class:`ServiceAccountCredentials` to sign the
61+ header and payload in a JWT.
62+
63+ Args:
64+ pkcs8_pem: bytes or ``NoneType``, The content of a PKCS#8 key
65+ in PEM format.
66+ pkcs12: bytes or ``NoneType``, The content of a PKCS#12 key.
67+ password: string or ``NoneType``, Password for PKCS#12 private
68+ key. Defaults to ``notasecret``.
69+
70+ Returns:
71+ tuple, A pair of :class:`crypt.Signer` and the potentially updated
72+ value of ``password``.
73+
74+ Raises:
75+ ValueError: If both ``pkcs8_pem`` and ``pkcs12`` are set.
76+ ValueError: If ``pkcs8_pem`` is set and ``password`` is passed in.
77+ ValueError: If ``pkcs12`` is set and ``password`` is not passed in.
78+ , """
79+ if pkcs8_pem is not None :
80+ if pkcs12 is not None :
81+ raise ValueError (_AT_LEAST_ONE_KEY , 'Both were passed.' )
82+ if password is not None :
83+ raise ValueError ('Private key password can only be used with '
84+ 'a PKCS#12 key.' )
85+ return crypt .Signer .from_string (pkcs8_pem ), None
86+
87+ if pkcs12 is None :
88+ raise ValueError (_AT_LEAST_ONE_KEY , 'Neither were passed.' )
89+ # From here we assume a PKCS#12 key, which is only
90+ # supported by pyOpenSSL.
91+ if crypt .Signer is not crypt .OpenSSLSigner :
92+ raise EnvironmentError (_PKCS12_ERROR )
93+ if password is None :
94+ password = _PASSWORD_DEFAULT
95+ return crypt .Signer .from_string (pkcs12 , password ), password
96+
97+
5798class ServiceAccountCredentials (AssertionCredentials ):
5899 """Service Account credential for OAuth 2.0 signed JWT grants.
59100
@@ -76,8 +117,8 @@ class ServiceAccountCredentials(AssertionCredentials):
76117 Args:
77118 service_account_email: string, The email associated with the
78119 service account.
79- private_key_pkcs8_pem: bytes, The content of a PKCS#8 key in PEM
80- format.
120+ private_key_pkcs8_pem: bytes, (Optional) The content of a PKCS#8 key
121+ in PEM format.
81122 private_key_pkcs12: bytes, (Optional) The content of a PKCS#12 key.
82123 private_key_password: string, (Optional) Password for PKCS#12 private
83124 key. Defaults to ``notasecret``.
@@ -97,15 +138,15 @@ class ServiceAccountCredentials(AssertionCredentials):
97138 access token.
98139 _revoke_uri: string, (Optional) The URI to use when revoking an
99140 access token.
100- kwargs: dict, Extra key-value pairs to send in the payload body
101- when making an assertion.
141+ kwargs: dict, Extra key-value pairs (both strings) to send in the
142+ payload body when making an assertion.
102143
103144 Raises:
104145 ValueError: If both ``private_key_pkcs8_pem`` and
105146 ``private_key_pkcs12`` are set.
106147 ValueError: If ``private_key_pkcs8_pem`` is set and
107148 ``private_key_password`` is passed in.
108- ValueError: If ``private_key_pkcs11 `` is set and
149+ ValueError: If ``private_key_pkcs12 `` is set and
109150 ``private_key_password`` is not passed in.
110151 """
111152
@@ -138,33 +179,15 @@ def __init__(self,
138179 self ._private_key_pkcs8_pem = private_key_pkcs8_pem
139180 self ._private_key_pkcs12 = private_key_pkcs12
140181 self ._private_key_password = private_key_password
141- self ._signer = self ._get_signer ()
182+ self ._signer , self ._private_key_password = _get_signer (
183+ self ._private_key_pkcs8_pem , self ._private_key_pkcs12 ,
184+ self ._private_key_password )
142185 self ._scopes = util .scopes_to_string (scopes )
143186 self ._private_key_id = private_key_id
144187 self ._service_account_id = service_account_id
145188 self ._user_agent = user_agent
146189 self ._kwargs = kwargs
147190
148- def _get_signer (self ):
149- if self ._private_key_pkcs8_pem is not None :
150- if self ._private_key_pkcs12 is not None :
151- raise ValueError (_AT_LEAST_ONE_KEY , 'Both were passed.' )
152- if self ._private_key_password is not None :
153- raise ValueError ('Private key password can only be used with '
154- 'a PKCS#12 key.' )
155- return crypt .Signer .from_string (self ._private_key_pkcs8_pem )
156-
157- if self ._private_key_pkcs12 is None :
158- raise ValueError (_AT_LEAST_ONE_KEY , 'Neither were passed.' )
159- # From here we assume a PKCS#12 key, which is only
160- # supported by pyOpenSSL.
161- if crypt .Signer is not crypt .OpenSSLSigner :
162- raise EnvironmentError (_PKCS12_ERROR )
163- if self ._private_key_password is None :
164- self ._private_key_password = _PASSWORD_DEFAULT
165- return crypt .Signer .from_string (self ._private_key_pkcs12 ,
166- self ._private_key_password )
167-
168191 def _to_json (self , strip , to_serialize = None ):
169192 """Utility function that creates JSON repr. of a Credentials object.
170193
0 commit comments