def wrapper(self, *args, **kwargs): """ Verifies the existence and validity of an access token before calling the decorated handler Parameters: :param args: the arguments for the decorated function :param kwargs: the keyword arguments for the decorated function Returns: :return: the decorated function result if the access token was valid; otherwise it send an error response and returns None """ if self.request.method in ["GET", "DELETE"]: access_token = self.request.get("accessToken") else: try: access_token = loads(self.request.body).get("accessToken") except ValueError: access_token = None if access_token is None or len(access_token) is 0: self.write_error(401, "No access token provided") return None try: application = get_application_key(access_token) except (TypeError, ValueError): self.write_error(401, "Invalid access token") return None if application is not None: return handler_method(self, *args, **kwargs) else: self.write_error(401, "Invalid access token") return None
def __get_application(self): """ Gets the application that made the current request Returns: :return: the application if the access token is valid; None otherwise """ if self.request.method in ['GET', 'DELETE']: access_token = self.request.get('accessToken') else: try: access_token = loads(self.request.body).get('accessToken') except ValueError: access_token = None if access_token is None: return None application_key = get_application_key(access_token) if not application_key: return None return Application.get_by_id(application_key)