def _http_scheme(self, pool): global token http_auth_url = "http://example.org/identity/v2.0" try: client = AstakosClient(token['id'], http_auth_url, use_pool=pool) client.authenticate() except AstakosClientException as err: if err.status != 302: self.fail("Should have returned 302 (Found)") else: self.fail("Should have returned 302 (Found)")
def _http_scheme(self, pool): global token http_auth_url = "http://example.org/identity/v2.0" try: client = AstakosClient(token['id'], http_auth_url, use_pool=pool) client.authenticate() except AstakosClientException as err: if err.status != 302: self.fail("Should have returned 302 (Found)") else: self.fail("Should have returned 302 (Found)")
def _unsupported_scheme(self, pool): global token, auth_url try: client = AstakosClient( token['id'], "ftp://example.com", use_pool=pool) client.authenticate() except BadValue: pass except Exception: self.fail("Should have raise BadValue Exception") else: self.fail("Should have raise BadValue Exception")
def _invalid_token(self, pool): global auth_url token = "skaksaFlBl+fasFdaf24sx" try: client = AstakosClient(token, auth_url, use_pool=pool) client.authenticate() except Unauthorized: pass except Exception: self.fail("Should have returned 401 (Invalid X-Auth-Token)") else: self.fail("Should have returned 401 (Invalid X-Auth-Token)")
def _invalid_token(self, pool): global auth_url token = "skaksaFlBl+fasFdaf24sx" try: client = AstakosClient(token, auth_url, use_pool=pool) client.authenticate() except Unauthorized: pass except Exception: self.fail("Should have returned 401 (Invalid X-Auth-Token)") else: self.fail("Should have returned 401 (Invalid X-Auth-Token)")
def _unsupported_scheme(self, pool): global token, auth_url try: client = AstakosClient( token['id'], "ftp://example.com", use_pool=pool) client.authenticate() except BadValue: pass except Exception: self.fail("Should have raise BadValue Exception") else: self.fail("Should have raise BadValue Exception")
def retrieve_user(token, astakos_auth_url, logger=None, client_ip=None): """Return user_info retrieved from astakos for the given token""" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: if logger: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise if not token: return None headers = None if client_ip: headers = {'X-Client-IP': client_ip} astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger, headers=headers) user_info = astakos.authenticate() return user_info
def _auth_user(self, pool): global token, endpoints_with_info, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) auth_info = client.authenticate() except Exception as err: self.fail("Shouldn't raise an Exception: %s" % err) self.assertEqual(endpoints_with_info, auth_info)
def _auth_user(self, pool): global token, endpoints_with_info, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) auth_info = client.authenticate() except Exception as err: self.fail("Shouldn't raise an Exception: %s" % err) self.assertEqual(endpoints_with_info, auth_info)
def user_for_token(token, astakos_auth_url, logger=None): if token is None: return None client = AstakosClient(token, astakos_auth_url, retry=2, use_pool=True, logger=logger) try: return client.authenticate() except Unauthorized: return None
def user_for_token(token, astakos_auth_url, logger=None): if token is None: return None client = AstakosClient(token, astakos_auth_url, retry=2, use_pool=True, logger=logger) try: return client.authenticate() except Unauthorized: return None
def authenticate(self): access_token = None #A KeyError will be raised if there is no token. access_token = LocalDataManager().get_token() try: s = AstakosClient(access_token, strings.Pithos_AUTHURL) auth_data = s.authenticate() username = auth_data['access']['user']['name'] pithos_url = self._get_pithos_public_url(auth_data) uuid = auth_data['access']['user']['id'] pithosClient = PithosClient(pithos_url, access_token, uuid) resp = pithosClient.list_containers() except (AstakosErrors.Unauthorized, faults.InvalidAuth) as e: raise faults.InvalidAuth('Pithos-Auth') except (AstakosErrors.AstakosClientException, faults.NetworkError) as e: raise faults.NetworkError('No internet-Auth') return (pithosClient, username)
def _pithos_auth(self): access_token = None dataManager = LocalDataManager() #A KeyError will be raised if there is no token. access_token = dataManager.get_credentials('Pithos') try: dm = LocalDataManager() s = AstakosClient(access_token, local.Pithos_AUTHURL) auth_data = s.authenticate() pithos_url = self._get_pithos_public_url(auth_data) uuid = auth_data['access']['user']['id'] pithosClient = CloudyPithosClient(pithos_url, access_token, uuid) pithosClient.container = dm.get_service_root('Pithos') #Check if the container saved in the settings exists. self._call_exceptional(pithosClient) except (AstakosErrors.Unauthorized, faults.InvalidAuth) as e: raise faults.InvalidAuth('Pithos-Auth') except (AstakosErrors.AstakosClientException, faults.NetworkError) as e: raise faults.NetworkError('No internet-Auth') return pithosClient
def _pithos_auth(self): access_token = None dataManager = LocalDataManager() #A KeyError will be raised if there is no token. access_token = dataManager.get_credentials('Pithos') try: dm = LocalDataManager() s = AstakosClient(access_token, local.Pithos_AUTHURL) auth_data = s.authenticate() pithos_url = self._get_pithos_public_url(auth_data) uuid = auth_data['access']['user']['id'] pithosClient = CloudyPithosClient(pithos_url, access_token, uuid) pithosClient.container = dm.get_service_root('Pithos') #Check if the container saved in the settings exists. self._call_exceptional(pithosClient) except (AstakosErrors.Unauthorized, faults.InvalidAuth) as e: raise faults.InvalidAuth('Pithos-Auth') except (AstakosErrors.AstakosClientException, faults.NetworkError) as e: raise faults.NetworkError('No internet-Auth') return pithosClient
def retrieve_user(token, astakos_auth_url, logger=None, client_ip=None): """Return user_info retrieved from astakos for the given token""" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: if logger: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise if not token: return None headers = None if client_ip: headers = {'X-Client-IP': client_ip} astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger, headers=headers) user_info = astakos.authenticate() return user_info
def finish(self, token): s = AstakosClient(token, local.Pithos_AUTHURL, logger=logger.logger_factory('astakosclient')) s.authenticate() return token
def wrapper(request, *args, **kwargs): try: # Explicitly set request encoding to UTF-8 instead of relying # to the DEFAULT_CHARSET setting. See: # https://docs.djangoproject.com/en/1.4/ref/unicode/#form-submission # flake8: noqa request.encoding = 'utf-8' # Get the requested serialization format serialization = get_serialization(request, format_allowed, serializations[0]) # If guessed serialization is not supported, fallback to # the default serialization or return an API error in case # strict serialization flag is set. if not serialization in serializations: if strict_serlization: raise faults.BadRequest(("%s serialization not " "supported") % serialization) serialization = serializations[0] request.serialization = serialization # Check HTTP method if http_method and request.method != http_method: raise faults.NotAllowed("Method not allowed", allowed_methods=[http_method]) # Get authentication token request.x_auth_token = None if token_required or user_required: token = get_token(request) if not token: msg = "Access denied. No authentication token" raise faults.Unauthorized(msg) request.x_auth_token = token # Authenticate if user_required: assert (token_required), "Can not get user without token" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger) user_info = astakos.authenticate() request.user_uniq = user_info["access"]["user"]["id"] request.user = user_info # Get the response object response = func(request, *args, **kwargs) # Fill in response variables update_response_headers(request, response) return response except faults.Fault as fault: if fault.code >= 500: django_logger.error("Unexpected API Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": fault.code, "request": request }) return render_fault(request, fault) except AstakosClientException as err: fault = faults.Fault(message=err.message, details=err.details, code=err.status) if fault.code >= 500: django_logger.error("Unexpected AstakosClient Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": fault.code, "request": request }) return render_fault(request, fault) except: django_logger.error("Internal Server Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": '500', "request": request }) fault = faults.InternalServerError("Unexpected error") return render_fault(request, fault)
def finish(self, token): s = AstakosClient(token, strings.Pithos_AUTHURL, logger=logger.logger_factory('astakosclient')) s.authenticate() return token
def wrapper(request, *args, **kwargs): try: # Get the requested serialization format serialization = get_serialization( request, format_allowed, serializations[0]) # If guessed serialization is not supported, fallback to # the default serialization or return an API error in case # strict serialization flag is set. if not serialization in serializations: if strict_serlization: raise faults.BadRequest(("%s serialization not " "supported") % serialization) serialization = serializations[0] request.serialization = serialization # Check HTTP method if http_method and request.method != http_method: raise faults.NotAllowed("Method not allowed", allowed_methods=[http_method]) # Get authentication token request.x_auth_token = None if token_required or user_required: token = get_token(request) if not token: msg = "Access denied. No authentication token" raise faults.Unauthorized(msg) request.x_auth_token = token # Authenticate if user_required: assert(token_required), "Can not get user without token" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger) user_info = astakos.authenticate() request.user_uniq = user_info["access"]["user"]["id"] request.user = user_info # Get the response object response = func(request, *args, **kwargs) # Fill in response variables update_response_headers(request, response) return response except faults.Fault as fault: if fault.code >= 500: django_logger.error("Unexpected API Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": fault.code, "request": request}) return render_fault(request, fault) except AstakosClientException as err: fault = faults.Fault(message=err.message, details=err.details, code=err.status) if fault.code >= 500: django_logger.error("Unexpected AstakosClient Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": fault.code, "request": request}) return render_fault(request, fault) except: django_logger.error("Internal Server Error: %s", request.path, exc_info=sys.exc_info(), extra={ "status_code": '500', "request": request}) fault = faults.InternalServerError("Unexpected error") return render_fault(request, fault)