def new_search_query(self) -> str: # Generate a new element key each time a new search is performed self.session['fernet_keys']['element_key'] = generate_user_keys( cookies_disabled=self.cookies_disabled)['element_key'] q = self.request_params.get('q') if q is None or len(q) == 0: return '' else: # Attempt to decrypt if this is an internal link try: q = Fernet(self.session['fernet_keys']['text_key']).decrypt( q.encode()).decode() except InvalidToken: pass # Reset text key self.session['fernet_keys']['text_key'] = generate_user_keys( cookies_disabled=self.cookies_disabled)['text_key'] # Format depending on whether or not the query is a "feeling lucky" query self.feeling_lucky = q.startswith('! ') self.query = q[2:] if self.feeling_lucky else q return self.query
def new_search_query(self) -> str: """Parses a plaintext query into a valid string for submission Also decrypts the query string, if encrypted (in the case of paginated results). Returns: str: A valid query string """ # Generate a new element key each time a new search is performed self.session['fernet_keys']['element_key'] = generate_user_keys( cookies_disabled=self.cookies_disabled)['element_key'] q = self.request_params.get('q') if q is None or len(q) == 0: return '' else: # Attempt to decrypt if this is an internal link try: q = Fernet(self.session['fernet_keys']['text_key']).decrypt( q.encode()).decode() except InvalidToken: pass # Reset text key self.session['fernet_keys']['text_key'] = generate_user_keys( cookies_disabled=self.cookies_disabled)['text_key'] # Strip leading '! ' for "feeling lucky" queries self.feeling_lucky = q.startswith('! ') self.query = q[2:] if self.feeling_lucky else q return self.query
def check_token(pathname): path_info = pathname.split("?token=") logger.info(f"path name: {pathname} | path_info: {path_info}") if len(path_info) != 2: logger.error("** token doesn't exist") return dcc.Location(href=Security.login_page_url, id="any"), None signed_token = path_info[1] try: jwt_token = Fernet(Security.fernet_secret).decrypt( signed_token.encode("utf-8")).decode("utf-8") except (InvalidToken, TypeError): logger.error("Invalid Token Error") return dcc.Location(href=Security.login_page_url, id="any"), None post_man_bear_token = "Bearer " if jwt_token.startswith(post_man_bear_token): jwt_token = jwt_token[len(post_man_bear_token):] try: payload = jwt.decode(jwt_token, Security.jwt_secret, algorithms=[Security.jwt_algo]) logger.info(f"** payload\n{payload}") if payload["role"] not in {"support", "ip"}: logger.error("** Role not supported") return dcc.Location(href=Security.login_page_url, id="any"), None else: logger.info("**success") return "", payload['user_id'] except (jwt.DecodeError, jwt.ExpiredSignatureError): logger.error("** Decode/ExpiredSignatrue Errors") return dcc.Location(href=Security.login_page_url, id="any"), None return dcc.Location(href=Security.login_page_url, id="any"), None
def new_search_query(self) -> str: """Parses a plaintext query into a valid string for submission Also decrypts the query string, if encrypted (in the case of paginated results). Returns: str: A valid query string """ q = self.request_params.get('q') if q is None or len(q) == 0: return '' else: # Attempt to decrypt if this is an internal link try: q = Fernet(self.session_key).decrypt(q.encode()).decode() except InvalidToken: pass # Strip leading '! ' for "feeling lucky" queries self.feeling_lucky = q.startswith('! ') self.query = q[2:] if self.feeling_lucky else q return self.query
def search(): request_params = request.args if request.method == 'GET' else request.form q = request_params.get('q') if q is None or len(q) == 0: return redirect('/') else: # Attempt to decrypt if this is an internal link try: q = Fernet(app.secret_key).decrypt(q.encode()).decode() except InvalidToken: pass feeling_lucky = q.startswith('! ') if feeling_lucky: # Well do you, punk? q = q[2:] user_agent = request.headers.get('User-Agent') mobile = 'Android' in user_agent or 'iPhone' in user_agent content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key) full_query = gen_query(q, request_params, g.user_config, content_filter.near) get_body = g.user_request.send(query=full_query) dirty_soup = BeautifulSoup(content_filter.reskin(get_body), 'html.parser') if feeling_lucky: return redirect( get_first_link(dirty_soup), 303) # Using 303 so the browser performs a GET request for the URL else: formatted_results = content_filter.clean(dirty_soup) # Set search type to be used in the header template to allow for repeated searches # in the same category search_type = request_params.get('tbm') if 'tbm' in request_params else '' return render_template( 'display.html', query=urlparse.unquote(q), search_type=search_type, dark_mode=g.user_config.dark, response=formatted_results, search_header=render_template('header.html', dark_mode=g.user_config.dark, q=urlparse.unquote(q), search_type=search_type, mobile=g.user_request.mobile) if 'isch' not in search_type else '')
def search(): request_params = request.args if request.method == 'GET' else request.form q = request_params.get('q') if q is None or len(q) == 0: return redirect('/') else: # Attempt to decrypt if this is an internal link try: q = Fernet(app.secret_key).decrypt(q.encode()).decode() except InvalidToken: pass feeling_lucky = q.startswith('! ') if feeling_lucky: # Well do you, punk? q = q[2:] user_agent = request.headers.get('User-Agent') mobile = 'Android' in user_agent or 'iPhone' in user_agent content_filter = Filter(mobile, g.user_config, secret_key=app.secret_key) full_query = gen_query(q, request_params, content_filter.near, language=g.user_config.lang) get_body = g.user_request.send(query=full_query) dirty_soup = BeautifulSoup(content_filter.reskin(get_body), 'html.parser') if feeling_lucky: return redirect( get_first_link(dirty_soup), 303) # Using 303 so the browser performs a GET request for the URL else: formatted_results = content_filter.clean(dirty_soup) return render_template('display.html', query=urlparse.unquote(q), response=formatted_results)