def wrapper(*args, **kwargs):
    ip = request.remote_addr
    token_string = session.get('flux_login_token')
    token = models.LoginToken.select(lambda t: t.token == token_string).first()
    if not token or token.ip != ip or token.expired():
      if token and token.expired():
        flash("Your login session has expired.")
        token.delete()
      return redirect(url_for('login'))

    request.login_token = token
    request.user = token.user
    return func(*args, **kwargs)