import os
from flask import Flask, Blueprint, request, jsonify

app = Flask(__name__)
# Read configuration to apply from environment
config_name = os.environ.get('FLASK_CONFIG', 'development')
# apply configuration
cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
app.config.from_pyfile(cfg)

# Create a blueprint
api = Blueprint('api', __name__)
# Import the endpoints belonging to this blueprint
from . import endpoints
from . import errors


@api.before_request
def before_request():
    """All routes in this blueprint require authentication."""
    if app.config['AUTH_REQUIRED']:
        if request.args.get('secret_token'):
            token = request.headers.get(app.config['TOKEN_HEADER'])
            if token == app.config['SECRET_TOKEN']:
                app.logger.info('Validated request token')
            app.logger.warn('Unauthorized: Invalid request token')
        app.logger.warn('Unauthorized: No request token included')
        return jsonify({'error': 'unauthorized'}), 401
    app.logger.info('No authentication required')


# register blueprints
app.register_blueprint(api, url_prefix=app.config['URL_PREFIX'])

import os
from flask import Flask, Blueprint, request, jsonify

app = Flask(__name__)
# Read configuration to apply from environment
config_name = os.environ.get('FLASK_CONFIG', 'development')
# apply configuration
cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
app.config.from_pyfile(cfg)

# Create a blueprint
api = Blueprint('api', __name__)
# Import the endpoints belonging to this blueprint
from . import endpoints
from . import errors


@api.before_request
def before_request():
    """All routes in this blueprint require authentication."""
    if app.config['AUTH_REQUIRED']:
        token_header = app.config['TOKEN_HEADER']
        if request.headers.get(token_header):
            token = request.headers[token_header]
            if token == app.config['SECRET_TOKEN']:
                app.logger.info('Validated request token')
            app.logger.warn('Unauthorized: Invalid request token')
        app.logger.warn('Unauthorized: No request token included')
        return jsonify({'error': 'unauthorized'}), 401
    app.logger.info('No authentication required')


# register blueprints
app.register_blueprint(api, url_prefix=app.config['URL_PREFIX'])

Fix bug in token authentication
Fix bug in token authentication

  import os
  from flask import Flask, Blueprint, request, jsonify
  
  app = Flask(__name__)
  # Read configuration to apply from environment
  config_name = os.environ.get('FLASK_CONFIG', 'development')
  # apply configuration
  cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
  app.config.from_pyfile(cfg)
  
  # Create a blueprint
  api = Blueprint('api', __name__)
  # Import the endpoints belonging to this blueprint
  from . import endpoints
  from . import errors
  
  
  @api.before_request
  def before_request():
      """All routes in this blueprint require authentication."""
      if app.config['AUTH_REQUIRED']:
-         if request.args.get('secret_token'):
-             token = request.headers.get(app.config['TOKEN_HEADER'])
+         token_header = app.config['TOKEN_HEADER']
+         if request.headers.get(token_header):
+             token = request.headers[token_header]
              if token == app.config['SECRET_TOKEN']:
                  app.logger.info('Validated request token')
              app.logger.warn('Unauthorized: Invalid request token')
          app.logger.warn('Unauthorized: No request token included')
          return jsonify({'error': 'unauthorized'}), 401
      app.logger.info('No authentication required')
  
  
  # register blueprints
  app.register_blueprint(api, url_prefix=app.config['URL_PREFIX'])
  
Fix bug in token authentication
## Code Before:
import os
from flask import Flask, Blueprint, request, jsonify

app = Flask(__name__)
# Read configuration to apply from environment
config_name = os.environ.get('FLASK_CONFIG', 'development')
# apply configuration
cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
app.config.from_pyfile(cfg)

# Create a blueprint
api = Blueprint('api', __name__)
# Import the endpoints belonging to this blueprint
from . import endpoints
from . import errors


@api.before_request
def before_request():
    """All routes in this blueprint require authentication."""
    if app.config['AUTH_REQUIRED']:
        if request.args.get('secret_token'):
            token = request.headers.get(app.config['TOKEN_HEADER'])
            if token == app.config['SECRET_TOKEN']:
                app.logger.info('Validated request token')
            app.logger.warn('Unauthorized: Invalid request token')
        app.logger.warn('Unauthorized: No request token included')
        return jsonify({'error': 'unauthorized'}), 401
    app.logger.info('No authentication required')


# register blueprints
app.register_blueprint(api, url_prefix=app.config['URL_PREFIX'])

## Instruction:
Fix bug in token authentication
## Code After:
import os
from flask import Flask, Blueprint, request, jsonify

app = Flask(__name__)
# Read configuration to apply from environment
config_name = os.environ.get('FLASK_CONFIG', 'development')
# apply configuration
cfg = os.path.join(os.getcwd(), 'config', config_name + '.py')
app.config.from_pyfile(cfg)

# Create a blueprint
api = Blueprint('api', __name__)
# Import the endpoints belonging to this blueprint
from . import endpoints
from . import errors


@api.before_request
def before_request():
    """All routes in this blueprint require authentication."""
    if app.config['AUTH_REQUIRED']:
        token_header = app.config['TOKEN_HEADER']
        if request.headers.get(token_header):
            token = request.headers[token_header]
            if token == app.config['SECRET_TOKEN']:
                app.logger.info('Validated request token')
            app.logger.warn('Unauthorized: Invalid request token')
        app.logger.warn('Unauthorized: No request token included')
        return jsonify({'error': 'unauthorized'}), 401
    app.logger.info('No authentication required')


# register blueprints
app.register_blueprint(api, url_prefix=app.config['URL_PREFIX'])
