def create_session_cookie(self, id_token, expires_in): """Creates a session cookie from the provided ID token.""" id_token = id_token.decode('utf-8') if isinstance(id_token, six.binary_type) else id_token if not isinstance(id_token, six.text_type) or not id_token: raise ValueError( 'Illegal ID token provided: {0}. ID token must be a non-empty ' 'string.'.format(id_token)) if isinstance(expires_in, datetime.timedelta): expires_in = int(expires_in.total_seconds()) if isinstance(expires_in, bool) or not isinstance(expires_in, int): raise ValueError('Illegal expiry duration: {0}.'.format(expires_in)) if expires_in < MIN_SESSION_COOKIE_DURATION_SECONDS: raise ValueError('Illegal expiry duration: {0}. Duration must be at least {1} ' 'seconds.'.format(expires_in, MIN_SESSION_COOKIE_DURATION_SECONDS)) if expires_in > MAX_SESSION_COOKIE_DURATION_SECONDS: raise ValueError('Illegal expiry duration: {0}. Duration must be at most {1} ' 'seconds.'.format(expires_in, MAX_SESSION_COOKIE_DURATION_SECONDS)) payload = { 'idToken': id_token, 'validDuration': expires_in, } try: response = self.client.body('post', ':createSessionCookie', json=payload) except requests.exceptions.RequestException as error: self._handle_http_error(COOKIE_CREATE_ERROR, 'Failed to create session cookie', error) else: if not response or not response.get('sessionCookie'): raise ApiCallError(COOKIE_CREATE_ERROR, 'Failed to create session cookie.') return response.get('sessionCookie')
def create_session_cookie(self, id_token, expires_in): """Creates a session cookie from the provided ID token.""" id_token = id_token.decode('utf-8') if isinstance(id_token, bytes) else id_token if not isinstance(id_token, str) or not id_token: raise ValueError( 'Illegal ID token provided: {0}. ID token must be a non-empty ' 'string.'.format(id_token)) if isinstance(expires_in, datetime.timedelta): expires_in = int(expires_in.total_seconds()) if isinstance(expires_in, bool) or not isinstance(expires_in, int): raise ValueError('Illegal expiry duration: {0}.'.format(expires_in)) if expires_in < MIN_SESSION_COOKIE_DURATION_SECONDS: raise ValueError('Illegal expiry duration: {0}. Duration must be at least {1} ' 'seconds.'.format(expires_in, MIN_SESSION_COOKIE_DURATION_SECONDS)) if expires_in > MAX_SESSION_COOKIE_DURATION_SECONDS: raise ValueError('Illegal expiry duration: {0}. Duration must be at most {1} ' 'seconds.'.format(expires_in, MAX_SESSION_COOKIE_DURATION_SECONDS)) url = '{0}:createSessionCookie'.format(self.base_url) payload = { 'idToken': id_token, 'validDuration': expires_in, } try: body, http_resp = self.http_client.body_and_response('post', url, json=payload) except requests.exceptions.RequestException as error: raise _auth_utils.handle_auth_backend_error(error) else: if not body or not body.get('sessionCookie'): raise _auth_utils.UnexpectedResponseError( 'Failed to create session cookie.', http_response=http_resp) return body.get('sessionCookie')
def get_headers(): # Create credential object from private key svc_creds = service_account.Credentials.from_service_account_info( service_account_json) # Create jwt jwt_creds = jwt.Credentials.from_signing_credentials(svc_creds, audience=audience) # Issue request to get auth token request = google.auth.transport.requests.Request() jwt_creds.refresh(request) # Extract token id_token = jwt_creds.token headers = { "Content-Type": "application/json", "Authorization": "Bearer " + id_token.decode('utf-8') } return headers
# Create jwt jwt_creds = jwt.Credentials.from_signing_credentials(svc_creds, audience=audience) # Issue request to get auth token request = google.auth.transport.requests.Request() jwt_creds.refresh(request) # Extract token id_token = jwt_creds.token # Construct GET request headers = { "Content-Type": "application/json", "Authorization": "Bearer " + id_token.decode('utf-8') } # Issue the request to the server r = requests.get(get_url, headers=headers) r.raise_for_status() # Print out all vols # print ("Response to GET request: " + get_url) print("Volumes:") for vol in r.json(): # Get volume attributes volname = vol["name"] volsizeGiB = convertToGiB(vol["quotaInBytes"]) region = vol["region"] volumeId = vol["volumeId"]