本文整理汇总了Python中itsdangerous.Signer类的典型用法代码示例。如果您正苦于以下问题:Python Signer类的具体用法?Python Signer怎么用?Python Signer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Signer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cookie_serialize
def cookie_serialize(secure_key, session_id, expire):
"""Serialize the cookie. Set the expire_time timestamp into the cookie"""
signer = Signer(secure_key)
session_data = session_id.encode('utf-8') + '&' + str(expire)
session_data = signer.sign(session_data).decode('utf-8')
return session_data
示例2: gen_signed_code
def gen_signed_code(self, identifier=None):
"""Generates a signed code in the format discount_code_base.randint.signature"""
if not identifier:
identifier = buid()
signer = Signer(self.secret)
key = "{base}.{identifier}".format(base=self.discount_code_base, identifier=identifier)
return signer.sign(key)
示例3: build_url
def build_url(self, local_path, **kwargs):
local_path = local_path.strip('/')
for key in 'background mode width height quality format padding'.split():
if key in kwargs:
kwargs[key[0]] = kwargs.pop(key)
# Remote URLs are encoded into the query.
parsed = urlparse(local_path)
if parsed.netloc:
kwargs['u'] = local_path
local_path = 'remote'
# Local ones are not.
else:
abs_path = self.find_img(local_path)
if abs_path:
kwargs['v'] = encode_int(int(os.path.getmtime(abs_path)))
# Sign the query.
public_kwargs = ((k, v) for k, v in kwargs.iteritems() if not k.startswith('_'))
query = urlencode(sorted(public_kwargs), True)
signer = Signer(current_app.secret_key)
sig = signer.get_signature('%s?%s' % (local_path, query))
return '%s/%s?%s&s=%s' % (
current_app.config['IMAGES_URL'],
local_path,
query,
sig,
)
示例4: save_session
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
if not session:
if session.modified:
self.redis.delete(self.session_prefix + session.session_id)
response.delete_cookie(app.session_cookie_name,
domain=domain, path=path)
return
httponly = self.get_cookie_httponly(app)
secure = self.get_cookie_secure(app)
expire = self.get_expiration_time(app, session)
serialize_session = self.serialization_method.dumps(dict(session))
pipe = self.redis.pipeline()
pipe.set(self.session_prefix + session.session_id, serialize_session)
pipe.expire(self.session_prefix + session.session_id, total_seconds(self.expire_time))
pipe.execute()
if self.use_sign:
session_id = Signer(app.secret_key, salt='flask-redis-session',
key_derivation='hmac').sign(session.session_id.encode('utf-8'))
session_id = session_id.decode('utf-8')
else:
session_id = session.session_id
print('session_id:', session_id)
response.set_cookie(key=app.session_cookie_name, value=session_id,
max_age=self.expire_time, expires=expire,
path=path, domain=domain,
secure=secure, httponly=httponly)
示例5: validate_machine_token
def validate_machine_token(self, token):
s = Signer(self.secret)
try:
s.unsign(token)
return True
except BadSignature:
return False
示例6: reset_password
def reset_password():
form = ResetPasswordForm(request.form)
if request.method == "POST" and form.validate():
token = form.token.data
s = Signer(app.config['SECRET_KEY'])
try:
email = s.unsign(token)
except BadSignature:
return render_template("reset_invalid_token.html")
user = User.query.filter_by(email=email).first()
if user:
user.set_password(form.password.data)
print user.password
login_user(user)
return redirect("/")
else:
return render_template("reset_invalid_token.html")
token = request.args.get('token', None)
if not token:
return render_template("reset_invalid_token.html")
return render_template("reset_password.html", form=form, token=token)
示例7: sign
def sign():
s = Signer(
bytes(input("Enter a secret(!!) key: "),
encoding="UTF-8"))
print(s.sign(
bytes(input("Enter data\n"),
encoding="UTF-8")).decode('UTF-8'))
示例8: validate_fingerprints
def validate_fingerprints(fp_secret_key, fp_salt, client_ip_fingerprint, browser_fingerprint, client_ip, user_agent,
accept_language):
is_valid = True
signer = Signer(fp_secret_key, fp_salt)
logging.debug('client_ip_fingerprint: %s', client_ip_fingerprint)
calculated_client_ip_fingerprint = signer.get_signature(client_ip)
logging.debug('calculated_client_ip_fingerprint: %s', calculated_client_ip_fingerprint)
if calculated_client_ip_fingerprint != client_ip_fingerprint:
logging.warn('Client IP does not match fingerprint in signature')
is_valid = False
# TODO:
# Uncomment return line below until atmobeta sends the right fingerprint signature.
# Just ignore fingerprint for now.
return is_valid
browser_fingerprint_input = ''.join([
user_agent,
accept_language])
logging.debug('browser_fingerprint_input: %s', browser_fingerprint_input)
logging.debug('browser_fingerprint: %s', browser_fingerprint)
calculated_browser_fingerprint = signer.get_signature(browser_fingerprint_input)
logging.debug('calculated_browser_fingerprint: %s', calculated_browser_fingerprint)
if calculated_browser_fingerprint != browser_fingerprint:
logging.warn('Browser fingerprint does not match calculated fingerprint')
is_valid = False
return is_valid
示例9: test_malformed_timestamp
def test_malformed_timestamp(self, signer):
other = Signer("secret-key")
signed = other.sign(b"value.____________")
with pytest.raises(BadTimeSignature) as exc_info:
signer.unsign(signed)
assert "Malformed" in str(exc_info.value)
示例10: test_timestamp_missing
def test_timestamp_missing(self, signer):
other = Signer("secret-key")
signed = other.sign("value")
with pytest.raises(BadTimeSignature) as exc_info:
signer.unsign(signed)
assert "missing" in str(exc_info.value)
示例11: verify_email
def verify_email(addr):
s = Signer(app.config['SECRET_KEY'])
token = s.sign(addr)
text = """Please click the following link to confirm your email address for {}: {}""".format(
get_config('ctf_name'),
url_for('auth.confirm_user', _external=True) + '/' + token.encode('base64')
)
sendmail(addr, text)
示例12: check_token
def check_token(token):
signer = Signer(flask.current_app.config["SECRET_KEY"])
try:
id = int(signer.unsign(token).decode("ascii"))
except Exception as e:
return None
user = Subscriber.query.filter_by(id=id).first()
return user
示例13: generate_csrf_token
def generate_csrf_token(user):
"""产生token"""
secret_key = current_app.config.get("WTF_CSRF_SECRET_KEY", current_app.secret_key)
nonce = os.urandom(24)
user_id = user.id
signer = Signer(secret_key)
csrf_token = signer.sign(str(user_id))
print csrf_token
return csrf_token
示例14: attachment
def attachment(self, att_id):
s = Signer(current_app.config.get('SECRET_KEY'), sep='$')
try:
att_id = int(s.unsign(att_id))
attach_obj = self.model.client.model('ir.attachment')
content = attach_obj.read(att_id, ['datas'])['datas']
image_fp = StringIO(base64.b64decode(content))
return send_file(image_fp)
except BadSignature:
abort(404)
示例15: split_cookie
def split_cookie(self, rv):
signer = Signer(self.app.secret_key)
cookie_data = rv.headers['Set-Cookie'].split(';', 1)[0]
for cookie in cookie_data.split('&'):
name, value = cookie_data.split('=')
if name == self.app.session_cookie_name:
unsigned_value = signer.unsign(value)
return unsigned_value.split('_')