def __init__(self, org_uid): """ Prepares access token for API calls (gets it from datastore and refreshes as needed). Args: org_uid(str): org identifier """ parent = ndb.Key('Org', org_uid) self.org_uid = org_uid org = parent.get_async() self.creds = OrgCredentials.get_by_id(org_uid, parent=parent) expires_at = datetime.utcfromtimestamp(self.creds.token['expires_at']) provider_config_key = org.get_result().provider_config if provider_config_key is None: logging.warn("org `{}` does not have a provider config.".format( parent.id())) raise MissingProviderConfigException() else: self.provider_config = provider_config_key.get() if (expires_at - datetime.utcnow()).total_seconds() < 60: logging.info( "access token for {} about to expire, refreshing".format( self.org_uid)) self.refresh_token() self.access_token = self.creds.token['access_token'] super(ZuoraApiSession, self).__init__()
def refresh_token(self): """ Refreshes the session cookie for the org """ parent = ndb.Key('Org', self.org_uid) user_creds = UserCredentials.get_by_id(self.org_uid, parent=parent) _get_session_cookie(user_creds) self.creds = OrgCredentials.get_by_id(self.org_uid, parent=parent)
def __init__(self, org_uid): """ Prepares access token for API calls (gets it from datastore and refreshes as needed). Args: org_uid(str): org identifier """ parent = ndb.Key('Org', org_uid) self.org_uid = org_uid org = parent.get_async() self.creds = OrgCredentials.get_by_id(org_uid, parent=parent) self.current_token = self.creds.token expires_at = datetime.utcfromtimestamp((self.creds.token['expires_at'])) self.org = org.get_result() provider_config_key = self.org.provider_config if provider_config_key is None: logging.warn("org `{}` does not have a provider config.".format(parent.id())) raise MissingProviderConfigException else: self.provider_config = provider_config_key.get() auth_attrs = json_module.loads(self.provider_config.additional_auth_attributes) if (expires_at - datetime.utcnow()).total_seconds() < 60: logging.info("access token for {} about to expire, refreshing".format(self.org_uid)) if auth_attrs['application_type'] == PARTNER: self.refresh_token(auth_attrs['rsa_key']) else: logging.info("application type is `public`. Skipping refresh.") rsa_key, sig_method = _get_partner_session_attrs(self.provider_config) super(XeroApiSession, self).__init__( self.provider_config.client_id, client_secret=self.provider_config.client_secret, resource_owner_key=self.current_token['oauth_token'], resource_owner_secret=self.current_token.get('oauth_token_secret'), rsa_key=rsa_key, signature_method=sig_method )
def refresh_token(self): """ Refreshes the access token for the org. """ try: token = OAuth2Session().refresh_token( token_url=TOKEN_URL, refresh_token=self.creds.token['refresh_token'], headers={ 'Authorization': "Basic " + base64.b64encode( self.provider_config.client_id + ":" + self.provider_config.client_secret ), 'Accept': 'application/json', 'content-type': 'application/x-www-form-urlencoded' } ) except InvalidGrantError: logging.warn("got InvalidGrantError exception on token refresh") raise InvalidGrantException() parent = ndb.Key('Org', self.org_uid) OrgCredentials(parent=parent, id=self.org_uid, token=token).put() self.creds = OrgCredentials.get_by_id(self.org_uid, parent=parent)