title: Basic timeout for HTTP Basic Auth
body: <p>I'm serving up a static file that to view requires a basic login and password. Currently I'm just using this snippet: <a href="http://flask.pocoo.org/snippets/8/" rel="nofollow">http://flask.pocoo.org/snippets/8/</a></p>
  
  <p>right now it just stays active until the browser quits. I'd like to figure out how to write just a simple timeout. Something like "if its been 5 minutes, kill this and redirect the user back to the index page.</p>
  
  <p>I'm pretty close, it times out, only thing is it remembers that redirect if the browser window is still open. Any suggestions on how to deal with that one last part? A cookie? Session clearing? Something else?</p>
  
  <p>Thanks</p>
  
  <pre><code>def check_auth(username, password):
    #This function is called to check if a username / password combination is valid.
    return username == 'oneshot' and password == 'private'
  
  def authenticate():
    # Sends a 401 response that enables basic auth
    return Response(
    'Could not verify your access level for that URL.\n'
    'You have to login with proper credentials', 401,
    {'WWW-Authenticate': 'Basic realm="Login Required"'})
  
  def requires_auth(f):
    @wraps(f)
    def decorated(*args, **kwargs):
  
      start_time = session.get('session_time', None)
  
      if start_time is None:
        start_time = datetime.datetime.now()
        session['session_time'] = start_time
  
      elapsed = datetime.datetime.now() - start_time
  
      if datetime.timedelta(0, 60, 0) &lt; elapsed:
        return redirect(url_for('index'))
  
  
      auth = request.authorization
      if not auth or not check_auth(auth.username, auth.password):
        return authenticate()
  
      return f(*args, **kwargs)
  
    return decorated
  </code></pre>