本文整理匯總了Python中itsdangerous.URLSafeTimedSerializer方法的典型用法代碼示例。如果您正苦於以下問題:Python itsdangerous.URLSafeTimedSerializer方法的具體用法?Python itsdangerous.URLSafeTimedSerializer怎麽用?Python itsdangerous.URLSafeTimedSerializer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類itsdangerous
的用法示例。
在下文中一共展示了itsdangerous.URLSafeTimedSerializer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_session
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_session(response):
""" Return session cookie contents.
This a base64 encoded json.
Returns a dict
"""
# Alas seems like if there are multiple set-cookie headers - we are on our own
for index, h in enumerate(response.headers):
if h[0] == "Set-Cookie":
cookie = parse_cookie(response.headers[index][1])
encoded_cookie = cookie.get("session", None)
if encoded_cookie:
serializer = URLSafeTimedSerializer(
"secret", serializer=TaggedJSONSerializer()
)
val = serializer.loads_unsafe(encoded_cookie)
return val[1]
示例2: get_serializer
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_serializer(secret: str, legacy: bool, salt: str) -> URLSafeTimedSerializer:
"""
Get a (cached) serializer instance
:param secret: Secret key
:param salt: Salt
:param legacy: Should the legacy timestamp generator be used?
:return: Flask session serializer
"""
if legacy:
signer = LegacyTimestampSigner
else:
signer = TimestampSigner
return URLSafeTimedSerializer(
secret_key=secret,
salt=salt,
serializer=TaggedJSONSerializer(),
signer=signer,
signer_kwargs={
'key_derivation': 'hmac',
'digest_method': hashlib.sha1})
示例3: send_password_reset_email
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def send_password_reset_email(user_email):
password_reset_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
password_reset_url = url_for(
'users.reset_with_token',
token=password_reset_serializer.dumps(user_email, salt='password-reset-salt'),
_external=True)
html = render_template(
'email_password_reset.html',
password_reset_url=password_reset_url)
send_email('Password Reset Requested', [user_email], html)
# ROUTES
示例4: get_signing_serializer
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_signing_serializer(self, app: "Quart") -> Optional[URLSafeTimedSerializer]:
"""Return a serializer for the session that also signs data.
This will return None if the app is not configured for secrets.
"""
if not app.secret_key:
return None
options = {"key_derivation": self.key_derivation, "digest_method": self.digest_method}
return URLSafeTimedSerializer(
app.secret_key, salt=self.salt, serializer=self.serializer, signer_kwargs=options
)
示例5: get_signing_serializer
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_signing_serializer(self, app):
if not app.secret_key:
return None
signer_kwargs = dict(
key_derivation=self.key_derivation,
digest_method=self.digest_method
)
return URLSafeTimedSerializer(app.secret_key, salt=self.salt,
serializer=self.serializer,
signer_kwargs=signer_kwargs)
示例6: get_serializer
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_serializer():
global serializer
if serializer is None:
serializer = URLSafeTimedSerializer(settings.load()['database']['crypto']['key'])
return serializer
示例7: generate_confirmation_token
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def generate_confirmation_token(config, email):
"""generate confirmation token using user's email via itsdangerous"""
serializer = URLSafeTimedSerializer(config['SECRET_KEY'])
return serializer.dumps(email, salt=config['EMAIL']['salt'])
示例8: validate_token
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def validate_token(config, token, expire_time=3600):
"""from token and expire_time to confirm user's email"""
serializer = URLSafeTimedSerializer(config['SECRET_KEY'])
try:
confirmed_email = serializer.loads(token, max_age=expire_time, salt=config['EMAIL']['salt'])
except Exception:
return False
return confirmed_email
示例9: _get_serializer
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def _get_serializer(app, name):
secret_key = app.config.get("SECRET_KEY")
salt = app.config.get("SECURITY_%s_SALT" % name.upper())
return URLSafeTimedSerializer(secret_key=secret_key, salt=salt)
示例10: generate_csrf
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def generate_csrf(secret_key=None, token_key=None):
"""Generate a CSRF token. The token is cached for a request, so multiple
calls to this function will generate the same token.
During testing, it might be useful to access the signed token in
``g.csrf_token`` and the raw token in ``session['csrf_token']``.
:param secret_key: Used to securely sign the token. Default is
``WTF_CSRF_SECRET_KEY`` or ``SECRET_KEY``.
:param token_key: Key where token is stored in session for comparision.
Default is ``WTF_CSRF_FIELD_NAME`` or ``'csrf_token'``.
"""
secret_key = _get_config(
secret_key, 'WTF_CSRF_SECRET_KEY', current_app.secret_key,
message='A secret key is required to use CSRF.'
)
field_name = _get_config(
token_key, 'WTF_CSRF_FIELD_NAME', 'csrf_token',
message='A field name is required to use CSRF.'
)
if field_name not in g:
if field_name not in session:
session[field_name] = hashlib.sha1(os.urandom(64)).hexdigest()
s = URLSafeTimedSerializer(secret_key, salt='wtf-csrf-token')
setattr(g, field_name, s.dumps(session[field_name]))
return g.get(field_name)
示例11: create_unique_url
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def create_unique_url(payload, salt=None):
config = SiteConfiguration.get_solo()
salt = config.salt if salt is None else salt
s = URLSafeTimedSerializer(settings.SECRET_KEY, salt=salt)
token = s.dumps(payload)
return reverse('unique_link', kwargs={'token': token}), token
示例12: generate_confirmation_token
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def generate_confirmation_token(email):
serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
return serializer.dumps(email, salt=current_app.config['SECURITY_PASSWORD_SALT'])
示例13: confirm_token
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def confirm_token(token, expiration=3600):
serializer = URLSafeTimedSerializer(current_app.config['SECRET_KEY'])
try:
email = serializer.loads(
token,
salt=current_app.config['SECURITY_PASSWORD_SALT'],
max_age=expiration
)
except:
return False
return email
示例14: get_state
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def get_state(self):
serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
return serializer.dumps(self.get_local_netloc())
示例15: verify_weixin_state
# 需要導入模塊: import itsdangerous [as 別名]
# 或者: from itsdangerous import URLSafeTimedSerializer [as 別名]
def verify_weixin_state(self):
state = self.get_weixin_state()
try:
serializer = URLSafeTimedSerializer(settings.SECRET_KEY)
return serializer.loads(state) == self.get_local_netloc()
except BadData:
return False