text: from flask import g, jsonify
  from flask_httpauth import HTTPBasicAuth
  from ..models import User
  from . import errors
  from . import api
  
  auth = HTTPBasicAuth()
  
  
  @auth.error_handler
  def auth_error():
      return errors.generate_error(errors.UNAUTHORIZED,
                                   'Invalid credentials')
  
  
  @api.before_request
  @auth.login_required
  def before_request():
      if (not g.current_user.confirmed):
              return errors.generate_error(errors.FORBIDDEN,
                                           'Unconfirmed account')
  
  
  @auth.verify_password
  def verify_password(email_or_token, password):
      if email_or_token == '':
          return False
      if password == '':
          g.current_user = User.verify_auth_token(email_or_token)
          g.token_used = True
          return g.current_user is not None
      user = User.query.filter_by(email=email_or_token).first()
      if not user:
          return False
      g.current_user = user
      g.token_used = False
      return user.verify_password(password)
  
  
  @api.route('/token')
  def get_token():
      if g.token_used:
          return errors.generate_error(errors.UNAUTHORIZED,
                                       'Invalid credentials')
      token = g.current_user.generate_auth_token().decode('UTF-8')
      return jsonify({'token': token,
                      'expiration': 3600})
  