Skip to content

Commit ee4d84f

Browse files
ServiceAccount should expect errors
1 parent 842ab01 commit ee4d84f

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/google/auth.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,33 @@
1616
from rsa import pkcs1
1717
import ntp
1818

19+
1920
def encode_dict_to_base64(d):
2021
return encode_bytes_to_safe_base64(json.dumps(d).encode('utf8'))
2122

23+
2224
def encode_bytes_to_safe_base64(bytes):
2325
encoded = binascii.b2a_base64(bytes).replace(b'+', b'-')
2426
return encoded.replace(b'/', b'_').strip().decode('utf8')
2527

28+
2629
# this class builds a JWT to request an access token
2730
# from the Google OAuth 2.0 Authorization Server using a service account
2831
# see https://developers.google.com/identity/protocols/OAuth2ServiceAccount
2932
class JWTBuilder:
3033

3134
def __init__(self):
32-
self._header = {}
33-
self._header['alg'] = 'RS256'
34-
self._header['typ'] = 'JWT'
35-
self._claim = {}
36-
self._claim['iss'] = ''
37-
self._claim['scope'] = ''
38-
self._claim['aud'] = 'https://www.googleapis.com/oauth2/v4/token'
39-
self._claim['exp'] = 0
40-
self._claim['iat'] = 0
35+
self._header = {
36+
'alg': 'RS256',
37+
'typ': 'JWT'}
38+
self._claim = {
39+
'iss': '',
40+
'scope': '',
41+
'aud': 'https://www.googleapis.com/oauth2/v4/token',
42+
'exp': 0,
43+
'iat': 0}
4144
self._key = None
42-
self._expiration = 30 * 60 # 30 minutes, in seconds
45+
self._expiration = 30 * 60 # 30 minutes, in seconds
4346

4447
def service_account(self, email):
4548
self._claim['iss'] = email
@@ -65,10 +68,12 @@ def build(self):
6568
print('jwt: signing ...')
6669
signature = pkcs1.sign(to_be_signed.encode('utf8'), self._key, 'SHA-256')
6770
print('jwt: done')
68-
print('jwt: encodeing')
71+
print('jwt: encoding')
6972
encoded_signature = encode_bytes_to_safe_base64(signature)
7073
return '%s.%s' % (to_be_signed, encoded_signature)
7174

75+
76+
# the class obtains a token for accessing the Google API
7277
class ServiceAccount:
7378

7479
def __init__(self):
@@ -97,13 +102,18 @@ def token(self):
97102
jwt = builder.build()
98103
print('token: jwt is done')
99104

100-
type = 'urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer'
101-
body = 'grant_type=%s&assertion=%s' % (type, jwt)
105+
grant_type = 'urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer'
106+
body = 'grant_type=%s&assertion=%s' % (grant_type, jwt)
102107
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
103108
response = requests.post('https://www.googleapis.com/oauth2/v4/token',
104109
data=body,
105110
headers=headers)
106111
if not response:
107-
print('token: no response received')
112+
raise Exception('token: no response received')
113+
114+
data = response.json()
115+
if 'access_token' not in data:
116+
print('response data: {}'.format(data))
117+
raise Exception('token: no access token in response')
108118

109-
return response.json()['access_token']
119+
return data['access_token']

0 commit comments

Comments
 (0)