Python itsdangerous.TimedJSONWebSignatureSerializer() Examples

The following are 30 code examples of itsdangerous.TimedJSONWebSignatureSerializer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module itsdangerous , or try the search function .
Example #1
Source File: models.py    From JmilkFan-s-Blog with Apache License 2.0 6 votes vote down vote up
def verify_auth_token(token):
        """Validate the token whether is night."""

        serializer = Serializer(
            current_app.config['SECRET_KEY'])
        try:
            # serializer object already has tokens in itself and wait for
            # compare with token from HTTP Request /api/posts Method `POST`.
            data = serializer.loads(token)
        except SignatureExpired:
            return None
        except BadSignature:
            return None

        user = User.query.filter_by(id=data['id']).first()
        return user 
Example #2
Source File: user.py    From BhagavadGita with GNU General Public License v3.0 6 votes vote down vote up
def change_email(self, token):
        """Verify the new email for this user."""
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except (BadSignature, SignatureExpired):
            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
        db.session.add(self)
        db.session.commit()
        return True 
Example #3
Source File: models.py    From flask-pycon2014 with MIT License 6 votes vote down vote up
def unsubscribe_user(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return None, None
        id = data.get('talk')
        email = data.get('email')
        if not id or not email:
            return None, None
        talk = Talk.query.get(id)
        if not talk:
            return None, None
        Comment.query\
            .filter_by(talk=talk).filter_by(author_email=email)\
            .update({'notify': False})
        db.session.commit()
        return talk, email 
Example #4
Source File: user.py    From penn-club-ratings with MIT License 6 votes vote down vote up
def change_email(self, token):
        """Verify the new email for this user."""
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except (BadSignature, SignatureExpired):
            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
        db.session.add(self)
        db.session.commit()
        return True 
Example #5
Source File: route_handler.py    From education-sawtooth-simple-supply with Apache License 2.0 5 votes vote down vote up
def generate_auth_token(secret_key, public_key):
    serializer = Serializer(secret_key)
    token = serializer.dumps({'public_key': public_key})
    return token.decode('ascii') 
Example #6
Source File: user.py    From penn-club-ratings with MIT License 5 votes vote down vote up
def generate_email_change_token(self, new_email, expiration=3600):
        """Generate an email change token to email an existing user."""
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'change_email': self.id, 'new_email': new_email}) 
Example #7
Source File: route_handler.py    From education-sawtooth-simple-supply with Apache License 2.0 5 votes vote down vote up
def deserialize_auth_token(secret_key, token):
    serializer = Serializer(secret_key)
    return serializer.loads(token) 
Example #8
Source File: models.py    From gitmark with GNU General Public License v2.0 5 votes vote down vote up
def generate_confirmation_token(self, expiration=3600):
        serializer = TimedJSONWebSignatureSerializer(current_app.config['SECRET_KEY'], expiration)
        return serializer.dumps({'confirm':self.username}) 
Example #9
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def generate_reset_token(self, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'reset': self.id}) 
Example #10
Source File: user.py    From penn-club-ratings with MIT License 5 votes vote down vote up
def generate_password_reset_token(self, expiration=3600):
        """
        Generate a password reset change token to email to an existing user.
        """
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'reset': self.id}) 
Example #11
Source File: user.py    From penn-club-ratings with MIT License 5 votes vote down vote up
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 
Example #12
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def generate_confirmation_token(self, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id}) 
Example #13
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def confirm(self, token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return False
        if data.get('confirm') != self.id:
            return False
        self.confirmed = True
        db.session.add(self)
        return True 
Example #14
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def generate_email_change_token(self, new_email, expiration=3600):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'change_email': self.id, 'new_email': new_email}) 
Example #15
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def verify_auth_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return None
        return User.query.get(data['id']) 
Example #16
Source File: models.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def generate_auth_token(self, expiration):
        s = Serializer(current_app.config['SECRET_KEY'],
                       expires_in=expiration)
        return s.dumps({'id': self.id}).decode('ascii') 
Example #17
Source File: models.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def get_api_token(self, expiration=300):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'user': self.id}).decode('utf-8') 
Example #18
Source File: models.py    From GeoHealthCheck with MIT License 5 votes vote down vote up
def verify_token(token):
        s = Serializer(APP.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except Exception:
            return None
        user_id = data.get('user')
        if user_id:
            return User.query.get(user_id)
        return None 
Example #19
Source File: models.py    From GeoHealthCheck with MIT License 5 votes vote down vote up
def get_token(self, expiration=7200):
        s = Serializer(APP.config['SECRET_KEY'], expiration)
        return s.dumps({'user': self.get_id()}).decode('utf-8') 
Example #20
Source File: user.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def reset_password(self, token, new_password):
        """Verify the new password for this user."""
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except (BadSignature, SignatureExpired):
            return False
        if data.get('reset') != self.id:
            return False
        self.password = new_password
        db.session.add(self)
        db.session.commit()
        return True 
Example #21
Source File: user.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def generate_password_reset_token(self, expiration=3600):
        """
        Generate a password reset change token to email to an existing user.
        """
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'reset': self.id}) 
Example #22
Source File: user.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def generate_email_change_token(self, new_email, expiration=3600):
        """Generate an email change token to email an existing user."""
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'change_email': self.id, 'new_email': new_email}) 
Example #23
Source File: user.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def generate_confirmation_token(self, expiration=604800):
        """Generate a confirmation token to email a new user."""

        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'confirm': self.id}) 
Example #24
Source File: views.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def confirm_shloka(id, token):
    """Confirm new subscription with provided token."""
    vrindavan_id = id
    current_app.logger.info(vrindavan_id)
    sql = """
        SELECT confirmed
        FROM vrindavan
        WHERE vrindavan_id = %s
    """ % (vrindavan_id)
    result = db.session.execute(sql)
    confirmed = [d['confirmed'] for d in result][0]
    current_app.logger.info(confirmed)

    if confirmed:
        return redirect(url_for('main.index'))
    if not confirmed:
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except (BadSignature, SignatureExpired):
            return False
        if data.get('confirm') != vrindavan_id:
            return False
        sql = """
            UPDATE vrindavan
            SET confirmed = 'true'
            WHERE vrindavan_id = %s
        """ % (vrindavan_id)
        db.session.execute(sql)
        db.session.commit()
        flash('Your subscription has been confirmed.', 'success')
    else:
        flash('The confirmation link is invalid or has expired.', 'error')
    return redirect(url_for('main.index')) 
Example #25
Source File: views.py    From BhagavadGita with GNU General Public License v3.0 5 votes vote down vote up
def generate_confirmation_token(id, expiration=604800):
    """Generate a confirmation token to email a new subscriber."""
    s = Serializer(current_app.config['SECRET_KEY'], expiration)
    return s.dumps({'confirm': id}) 
Example #26
Source File: auth.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def verify_token(token):
    try:
        serializer = Serializer(current_app.config["SECRET_KEY"], expires_in=36000)
        data = serializer.loads(token)
    except:
        return False
    return data["user_id"] 
Example #27
Source File: auth.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def generate_token(user_id):
    serializer = Serializer(current_app.config["SECRET_KEY"], expires_in=36000)
    return serializer.dumps({"user_id": user_id}) 
Example #28
Source File: models.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def get_unsubscribe_token(self, email, expiration=604800):
        s = Serializer(current_app.config['SECRET_KEY'], expiration)
        return s.dumps({'talk': self.id, 'email': email}).decode('utf-8') 
Example #29
Source File: models.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def validate_api_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except:
            return None
        id = data.get('user')
        if id:
            return User.query.get(id)
        return None 
Example #30
Source File: common.py    From sawtooth-marketplace with Apache License 2.0 5 votes vote down vote up
def deserialize_auth_token(secret_key, token):
    serializer = Serializer(secret_key)
    return serializer.loads(token)