Esempio n. 1
0
    def decorated(*args, **kwargs):
        parser = reqparse.RequestParser()
        parser.add_argument('Authorization',
                            type=str,
                            location='headers',
                            required=True,
                            help='Authorization header is required.')
        args = parser.parse_args()

        # Parse header
        search = re.search(r"^Bearer\s(.*)", args.Authorization)
        if search is None:
            raise bad_request('Invalid token')

        encoded = search.groups()[0]

        # Attempt to decode JWT
        try:
            decoded = jwt.decode(encoded,
                                 config.get('token', 'secret'),
                                 algorithms=['HS256'])
        except jwt.InvalidTokenError:
            raise bad_request('Invalid token')

        # Pass token to decorated function
        kwargs['token'] = decoded

        return f(*args, **kwargs)
Esempio n. 2
0
def generate_token(user):
 
    #Generate a new JWT containing user info which can be used to authenticate future api calls
    return jwt.encode(
      { 'username': user.username, 'email': user.email, 'id': user.id },
      config.get('token', 'secret'),
      algorithm='HS256'
    )
Esempio n. 3
0
def generate_token(user):

    #Generate a new JWT containing user info which can be used to authenticate future api calls
    return jwt.encode(
        {
            'username': user.username,
            'email': user.email,
            'id': user.id
        },
        config.get('token', 'secret'),
        algorithm='HS256')
Esempio n. 4
0
    def decorated(*args, **kwargs):
        parser = reqparse.RequestParser()
        parser.add_argument('Authorization', type=str, location='headers', required=True, help='Authorization header is required.')
        args = parser.parse_args()
        
        # Parse header
        search = re.search(r"^Bearer\s(.*)", args.Authorization)
        if search is None:
            raise bad_request('Invalid token')

        encoded = search.groups()[0]

        # Attempt to decode JWT
        try:
            decoded = jwt.decode(encoded, config.get('token', 'secret'), algorithms=['HS256'])
        except jwt.InvalidTokenError:
            raise bad_request('Invalid token')

        # Pass token to decorated function
        kwargs['token'] = decoded
        
        return f(*args, **kwargs)