Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def confirm_account(self, token):
"""Verify that the provided token is for this user's id."""
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token)
except (BadSignature, SignatureExpired):
return False
if data.get('confirm') != self.id:
return False
self.confirmed = True
db.session.add(self)
db.session.commit()
return True
def verify_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except (BadSignature, SignatureExpired):
return None
user = models.User.query.get(data['id'])
return user
def generate_auth_token(self, key, expiration = 10080):
s = Serializer(key, expires_in = expiration)
return s.dumps({ 'id': self.id })
def change_email(self, token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token.encode('utf-8'))
except:
return False
if data.get('change_email') != self.id:
return False
new_email = data.get('new_email')
if new_email is None:
return False
if self.query.filter_by(email=new_email).first() is not None:
return False
self.email = new_email
self.avatar_hash = self.gravatar_hash()
db.session.add(self)
return True
def generate_token(self, sub_domain):
entry = self.env['wxapp.config'].get_entry(sub_domain)
secret_key = entry.get_config('secret')
app_id = entry.get_config('app_id')
if not secret_key or not app_id:
raise exceptions.ValidationError('未设置 secret_key 或 appId')
s = Serializer(secret_key=secret_key, salt=app_id, expires_in=AccessToken._transient_max_hours * 3600)
timestamp = time.time()
return s.dumps({'session_key': self.session_key, 'open_id': self.open_id, 'iat': timestamp})
def verify_auth_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None # valid token, but expired
except BadSignature:
return None # invalid token
user = User.query.get(data['id'])
return user
def authenticate_by_token(token):
if token is None:
return False
s = Serializer(SECRET_KEY)
try:
username = s.loads(token.encode())
if username == 'admin':
return True
except:
return False
return False
def confirm(self, token):
s = Serializer(get_config_var('SECRET_KEY'))
try:
data = s.loads(token.encode('utf-8'))
except:
return False
if data.get('confirm') != self.id.__str__():
return False
self.confirmed = True
return True
def verify_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except (BadSignature, SignatureExpired):
return None
return data