本文整理汇总了Python中itsdangerous.URLSafeSerializer方法的典型用法代码示例。如果您正苦于以下问题:Python itsdangerous.URLSafeSerializer方法的具体用法?Python itsdangerous.URLSafeSerializer怎么用?Python itsdangerous.URLSafeSerializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类itsdangerous
的用法示例。
在下文中一共展示了itsdangerous.URLSafeSerializer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sign_data
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def sign_data(data, expires_in=None, **kw):
"""
To sign url safe data.
If expires_in is provided it will Time the signature
:param data: (mixed) the data to sign
:param expires_in: (int) in minutes. Time to expire
:param kw: kwargs for itsdangerous.URLSafeSerializer
:return:
"""
if expires_in:
expires_in *= 60
s = URLSafeTimedSerializer2(secret_key=__CRYPT.get("secret_key"),
expires_in=expires_in,
salt=__CRYPT.get("salt"),
**kw)
else:
s = itsdangerous.URLSafeSerializer(secret_key=__CRYPT.get("secret_key"),
salt=__CRYPT.get("salt"),
**kw)
return s.dumps(data)
示例2: unsign_data
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def unsign_data(token, **kw):
"""
To unsign url safe data.
If expires_in is provided it will Time the signature
:param token:
:param secret_key:
:param salt: (string) a namespace key
:param kw:
:return:
"""
if len(token.split(".")) == 3:
s = URLSafeTimedSerializer2(secret_key=__CRYPT.get("secret_key"), salt=__CRYPT.get("salt"), **kw)
value, timestamp = s.loads(token, max_age=None, return_timestamp=True)
now = datetime.datetime.utcnow()
if timestamp > now:
return value
else:
raise itsdangerous.SignatureExpired(
'Signature age %s < %s ' % (timestamp, now),
payload=value,
date_signed=timestamp)
else:
s = itsdangerous.URLSafeSerializer(secret_key=__CRYPT.get("secret_key"), salt=__CRYPT.get("salt"), **kw)
return s.loads(token)
示例3: load_token
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def load_token(token):
# Load unsafe because payload is needed for sig
sig_okay, payload = URLSafeSerializer(current_app.config['SECRET_KEY']).loads_unsafe(token)
if not payload:
return None
# User key *could* be stored in payload to avoid user lookup in db
user = User.get_by_id(payload.get('id'))
if not user:
return None
try:
if BaseUser.signer(sha256(user.password).hexdigest()).loads(token):
return user
else:
return None
except BadSignature:
return None
示例4: on_delete_signup
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def on_delete_signup(token):
"""Endpoint to delete signups via email"""
try:
s = URLSafeSerializer(get_token_secret())
signup_id = ObjectId(s.loads(token))
except BadSignature:
return "Unknown token"
deleteitem_internal('eventsignups', concurrency_check=False,
**{current_app.config['ID_FIELD']: signup_id})
redirect_url = current_app.config.get('SIGNUP_DELETED_REDIRECT')
if redirect_url:
return redirect(redirect_url)
else:
return current_app.config['SIGNUP_DELETED_TEXT']
示例5: sign
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def sign(self, value, namespace="default"):
return URLSafeSerializer(self._secret, namespace).dumps(value)
示例6: unsign
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def unsign(self, signed, namespace="default"):
return URLSafeSerializer(self._secret, namespace).loads(signed)
示例7: connect
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def connect():
global serializer, fernet
crypto_info = settings.load()['database']['crypto']
mongo_host = settings.load()['database']['mongo'].get('host', '127.0.0.1')
mongo_port = settings.load()['database']['mongo'].get('port', '27017')
serializer = URLSafeSerializer(crypto_info['key'])
fernet = Fernet(crypto_info['fernet'])
mongoengine.connect(name, host=mongo_host, port=mongo_port, tz_aware=True)
示例8: encode
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def encode(something):
"""Encode something with SECRET_KEY."""
secret_key = current_app.config.get('SECRET_KEY')
s = URLSafeSerializer(secret_key)
return s.dumps(something)
示例9: decode
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def decode(something):
"""Decode something with SECRET_KEY."""
secret_key = current_app.config.get('SECRET_KEY')
s = URLSafeSerializer(secret_key)
try:
return s.loads(something)
except BadSignature:
return None
示例10: get_serializer
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def get_serializer(secret_key=None):
if secret_key is None:
secret_key = current_app.config['SECRET_KEY']
return URLSafeSerializer(secret_key)
示例11: session
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def session(callback):
cookie_name = 'session'
serializer = URLSafeSerializer(conf['SECRET'])
def inner(*args, **kwargs):
data_raw = data = request.get_cookie(cookie_name)
if data_raw:
try:
data = serializer.loads(data_raw)
except (BadSignature, BadData):
data = None
if data:
conf['USER'] = data['username']
request.session = data or {}
try:
return callback(*args, **kwargs)
finally:
if request.session:
save(request.session)
elif not data_raw:
pass
else:
response.delete_cookie(cookie_name)
def save(session):
cookie_opts = {
# keep session for 3 days
'max_age': 3600 * 24 * 3,
# for security
'httponly': True,
'secure': request.headers.get('X-Forwarded-Proto') == 'https',
}
data = serializer.dumps(session)
response.set_cookie(cookie_name, data, **cookie_opts)
return inner
示例12: get_gopher_signing_serializer
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def get_gopher_signing_serializer(self, app):
"""
This is almost the same serializer that the cookie session uses,
except that it doesn't set an `expiration` time for the session.
"""
if not app.secret_key:
return None
signer_kwargs = dict(
key_derivation=self.key_derivation,
digest_method=self.digest_method)
return URLSafeSerializer(
app.secret_key,
salt=self.salt,
serializer=self.serializer,
signer_kwargs=signer_kwargs)
示例13: signer
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def signer(salt):
return URLSafeSerializer(current_app.config['SECRET_KEY'] + salt)
示例14: generate_token
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def generate_token(self):
s = URLSafeSerializer('secret-key')
self.token = s.dumps(self.address)
return self.token
示例15: sign_data
# 需要导入模块: import itsdangerous [as 别名]
# 或者: from itsdangerous import URLSafeSerializer [as 别名]
def sign_data(data):
"""
Serialize and sign data (likely to be put in a session cookie).
Parameters
----------
data : object
Data to be serialized and signed.
Returns
-------
str
Encrypted data
"""
s = URLSafeSerializer(app.secret_key)
return s.dumps(data)