def _generate_assertion(self): """Generate the assertion that will be used in the request.""" header = { 'alg': 'RS256', 'typ': 'JWT', 'kid': self._private_key_id } now = int(time.time()) payload = { 'aud': self._token_uri, 'scope': self._scopes, 'iat': now, 'exp': now + _ServiceAccountCredentials.MAX_TOKEN_LIFETIME_SECS, 'iss': self._service_account_email } payload.update(self._kwargs) first_segment = _urlsafe_b64encode(_json_encode(header)) second_segment = _urlsafe_b64encode(_json_encode(payload)) assertion_input = first_segment + b'.' + second_segment # Sign the assertion. rsa_bytes = rsa.pkcs1.sign(assertion_input, self._private_key, 'SHA-256') signature = base64.urlsafe_b64encode(rsa_bytes).rstrip(b'=') return assertion_input + b'.' + signature
def make_signed_jwt(signer, payload, key_id=None): """Make a signed JWT. See http://self-issued.info/docs/draft-jones-json-web-token.html. Args: signer: crypt.Signer, Cryptographic signer. payload: dict, Dictionary of data to convert to JSON and then sign. key_id: string, (Optional) Key ID header. Returns: string, The JWT for the payload. """ header = {'typ': 'JWT', 'alg': 'RS256'} if key_id is not None: header['kid'] = key_id segments = [ _helpers._urlsafe_b64encode(_helpers._json_encode(header)), _helpers._urlsafe_b64encode(_helpers._json_encode(payload)), ] signing_input = b'.'.join(segments) signature = signer.sign(signing_input) segments.append(_helpers._urlsafe_b64encode(signature)) logger.debug(str(segments)) return b'.'.join(segments)
def _generate_assertion(self): """Generate the assertion that will be used in the request.""" header = { 'alg': 'RS256', 'typ': 'JWT', 'kid': self._private_key_id } now = int(time.time()) payload = { 'aud': self._token_uri, 'scope': self._scopes, 'iat': now, 'exp': now + _ServiceAccountCredentials.MAX_TOKEN_LIFETIME_SECS, 'iss': self._service_account_email } payload.update(self._kwargs) first_segment = _urlsafe_b64encode(_json_encode(header)) second_segment = _urlsafe_b64encode(_json_encode(payload)) assertion_input = first_segment + b'.' + second_segment # Sign the assertion. rsa_bytes = rsa.pkcs1.sign(assertion_input, self._private_key, 'SHA-256') signature = base64.urlsafe_b64encode(rsa_bytes).rstrip(b'=') return assertion_input + b'.' + signature
def test_list_input(self): data = [42, 1337] result = _json_encode(data) self.assertEqual(result, """[42,1337]""")
def test_dictionary_input(self): # Use only a single key since dictionary hash order # is non-deterministic. data = {u'foo': 10} result = _json_encode(data) self.assertEqual(result, """{"foo":10}""")
def test_list_input(self): data = [42, 1337] result = _helpers._json_encode(data) self.assertEqual(result, '[42,1337]')
def test_dictionary_input(self): # Use only a single key since dictionary hash order # is non-deterministic. data = {u'foo': 10} result = _helpers._json_encode(data) self.assertEqual(result, '{"foo":10}')
def test_list_input(self): data = [42, 1337] result = _json_encode(data) self.assertEqual(result, """[42,1337]""")
def get_google_events(calendar_events): g = conf['google'] with open(g['json_file'], 'r') as file_obj: client_credentials = json.load(file_obj) private_key_pkcs8_pem = client_credentials['private_key'] signer = _pure_python_crypt.RsaSigner.from_string(private_key_pkcs8_pem) header = {'typ': 'JWT', 'alg': 'RS256'} now = int(time.time()) payload = { 'aud': 'https://www.googleapis.com/oauth2/v4/token', 'scope': g['scopes'], 'iat': now, 'exp': now + 3600, # 1 hour in seconds 'iss': g['email_address'] } segments = [ _helpers._urlsafe_b64encode(_helpers._json_encode(header)), _helpers._urlsafe_b64encode(_helpers._json_encode(payload)), ] signing_input = b'.'.join(segments) signature = signer.sign(signing_input) segments.append(_helpers._urlsafe_b64encode(signature)) claim = b'.'.join(segments) post_data = { 'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion':claim } req = requests.post('https://www.googleapis.com/oauth2/v4/token', post_data) resp = req.json() access_token = resp['access_token'] auth_header = { 'Authorization':'Bearer ' + access_token } calendar_url = 'https://www.googleapis.com/calendar/v3/calendars/[email protected]/events' hel_time = datetime.datetime.now(tz) start_time = hel_time.strftime('%Y-%m-%dT00:00:00+02') get_data = { 'maxResults':100, 'orderBy':'startTime', 'singleEvents':'true', 'timeMin':start_time } req = requests.get(calendar_url, params = get_data, headers = auth_header) #req = requests.get(calendar_list_url, headers = auth_header) resp = req.json() for item in resp['items']: start_time_item = item['start'] start_time = parse_google_event_datetime(item['start']) end_time = parse_google_event_datetime(item['end']) event = { 'summary': item.get('summary', ''), 'location': item.get('location', ''), 'start_time': start_time, 'end_time': end_time } add_event(calendar_events, event)