def get_auth_token(self, username, password, tenant_id, auth_api, url, **kwargs): """ Init the variables related to authorization, needed to execute tests :return: The auth token retrieved """ cred_kwargs = { 'auth_url': url, 'username': username, 'password': password } # Currently, both v2 and v3 Identity API versions are supported if auth_api == AUTH_API_V2: cred_kwargs['tenant_name'] = kwargs.get('tenant_name') elif auth_api == AUTH_API_V3: cred_kwargs['user_domain_name'] = kwargs.get('user_domain_name') # Instantiate a Password object try: identity_package = 'keystoneclient.auth.identity.%s' % auth_api.replace('.0', '') identity_module = __import__(identity_package, fromlist=['Password']) password_class = getattr(identity_module, 'Password') logger.debug("Authentication with %s", password_class) credentials = password_class(**cred_kwargs) except (ImportError, AttributeError) as e: raise e # Get auth token logger.debug("Getting auth token for tenant %s...", tenant_id) try: auth_sess = self.session.Session(auth=credentials, timeout=DEFAULT_REQUEST_TIMEOUT) auth_token = auth_sess.get_token() logger.debug("Admin token generated:" + auth_token) except (KeystoneClientException, KeystoneConnectionRefused) as e: logger.error("No auth token (%s)", e.message) raise(e) return auth_token
def checkToken(self, admin_token, token, tenant_id, url, auth_api): """checks if a token is valid against a url using an admin token.""" logger.info("Starting Authentication of token %s ", token) try: if not token: raise Unauthorized("Token is empty") auth_result = self.get_info_token(url, admin_token, token, auth_api) if auth_result: if tenant_id == auth_result.tenant["id"]: logger.info('The token is valid') else: logger.error("TenantId %s ", tenant_id) logger.error("Token TenantId %s ", auth_result.tenant["id"]) raise Unauthorized("Token is not valid for specified tenant %s" % tenant_id) except Unauthorized as unauth: logger.error(unauth) raise unauth except InternalServerError as internalError: raise AuthorizationFailure("Token could not have enough permissions to access tenant: %s" % tenant_id) except Exception as ex: raise ex