help with pipe signature in version 2.6 #518
-
Hi, This is my pipe for URL signature: from emmett import Pipe, Injector, request, abort
from emmett.routing.urls import url
from emmett_core.routing.urls import HttpUrlBuilder
from itsdangerous import Signer
KEY = 'my-key'
class SignInjector(Injector):
namespace = "signature"
@staticmethod
def sign(path, args, params, anchor, scheme, host, language):
s = Signer(KEY)
builder = HttpUrlBuilder([url(path)])
_url = builder.url(None, None, language, args, params, None)
p = _url.find('_signature')
if p > 0:
_url = _url[:p-1] + _url[p+38:]
_url = str.encode(_url)
signature = s.get_signature(_url).decode("utf-8")
return signature
class SignPipe(Pipe):
async def pipe(self, next_pipe, **kwargs):
if await self.valid_url():
return await next_pipe(**kwargs)
abort(404)
async def valid_url(self):
signature = request.query_params._signature or ''
builder = HttpUrlBuilder([request.path])
_url = builder.url(None, None,
request.language, [],
{key: request.query_params[key] for key in request.query_params.keys() if key != '_signature'},
None)
s = Signer(KEY)
sig = str.encode(signature)
value = str.encode(_url)
return s.verify_signature(value=value, sig=sig) With version 2.6 change:
for from emmett.routing.urls import url
from emmett_core.routing.urls import HttpUrlBuilder With this Emmett starts, but I get this error:
File /venv/lib/python3.12/site-packages/emmett_core/routing/urls.py, in function http at line 163 host = self.current.request.host
# add signature
if sign:
if "_signature" in params:
del params["_signature"]
params["_signature"] = sign(path, args, params, anchor, scheme, host, language)
return builder.url(scheme, host, lang, args, params, anchor)
def ws(self, path, args=[], params={}, scheme=None, host=None, language=None):
if not isinstance(args, (list, tuple)): File /tramites-app/src/modules/signature.py, in function sign at line 18 @staticmethod
def sign(path, args, params, anchor, scheme, host, language):
s = Signer(KEY)
builder = HttpUrlBuilder([url(path)])
_url = builder.url(None, None, language, args, params, None)
p = _url.find('_signature')
if p > 0:
_url = _url[:p-1] + _url[p+38:] File /venv/lib/python3.12/site-packages/emmett_core/routing/urls.py, in function url at line 78 rv += "#{}".format(element)
return rv
def url(self, scheme, host, language, args, params, anchor):
args = self._args + args
self.add_static_versioning(args)
self.add_language(args, language)
self.add_prefix(args)
return f"{self.path_prefix(scheme, host)}{self.args(args)}" f"{self.params(params)}{self.anchor(anchor)}" File /venv/lib/python3.12/site-packages/emmett_core/routing/urls.py, in function add_static_versioning at line 62 class HttpUrlBuilder(UrlBuilder):
__slots__ = ("components", "_args")
def add_static_versioning(self, args):
versioning = self.current.app._router_http.static_versioning()
if self.path.startswith("/static") and versioning:
self.components.insert(1, "/_{}")
args.insert(1, str(versioning)) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you want to use from emmett_core.routing.urls import HttpUrlBuilder
from emmett import current
HttpUrlBuilder(current, ["whatever']) |
Beta Was this translation helpful? Give feedback.
If you want to use
HttpUrlBuilder
directly fromemmett_core
, you need to passcurrent
as first attribute, eg: