Repository: scoutapp/scout_apm_python

Issue:
Problem with flask_jwt
I'm trying to use the ScoutApp in an flask application using the flask_jwt module, but i recive the below error message when my loggin route are called:

```jsx
[2018-05-30 13:06:20,984] ERROR in __init__: Traceback (most recent call last):

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 69, in dispatch_request
    result = view_func(**req.view_args)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/core/monkey.py", line 213, in __call__
    return self._eop_wrapper_(self.__subject__, *args, **kwargs)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 119, in tracing_function
    raise e

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 116, in tracing_function
    return original(*args, **kwargs)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/flask_jwt/__init__.py", line 115, in _default_auth_request_handler
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)

AttributeError: 'NoneType' object has no attribute 'get'
 No user info
    Request:   OPTIONS /api/auth
    QueryString: b''
    IP:        127.0.0.1
    Agent:     linux | chrome 66.0.3359.181
    Raw Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
    Payload: b''
    
ERROR:condominio:Traceback (most recent call last):

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 69, in dispatch_request
    result = view_func(**req.view_args)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/core/monkey.py", line 213, in __call__
    return self._eop_wrapper_(self.__subject__, *args, **kwargs)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 119, in tracing_function
    raise e

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/scout_apm/flask/__init__.py", line 116, in tracing_function
    return original(*args, **kwargs)

  File "/home/andreramos/.virtualenvs/back/lib/python3.6/site-packages/flask_jwt/__init__.py", line 115, in _default_auth_request_handler
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)

AttributeError: 'NoneType' object has no attribute 'get'
 No user info
    Request:   OPTIONS /api/auth
    QueryString: b''
    IP:        127.0.0.1
    Agent:     linux | chrome 66.0.3359.181
    Raw Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
    Payload: b''
    
INFO:werkzeug:127.0.0.1 - - [30/May/2018 13:06:23] "OPTIONS /api/auth HTTP/1.1" 500 
```

Debugging the problem, apparently the JWT can't get the request data to do the authentication. For some reason the request lose it the data when ScoutApp are enabled.

The JWT code that breaks:
```python
def _default_auth_request_handler():
    data = request.get_json()
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)
    password = data.get(current_app.config.get('JWT_AUTH_PASSWORD_KEY'), None)
    criterion = [username, password, len(data) == 2]

    if not all(criterion):
        raise JWTError('Bad Request', 'Invalid credentials')

    identity = _jwt.authentication_callback(username, password)

    if identity:
        access_token = _jwt.jwt_encode_callback(identity)
        return _jwt.auth_response_callback(access_token, identity)
    else:
        raise JWTError('Bad Request', 'Invalid credentials')
```



Discussion:

Resolving patch:
diff --git a/src/scout_apm/flask/__init__.py b/src/scout_apm/flask/__init__.py
--- a/src/scout_apm/flask/__init__.py
+++ b/src/scout_apm/flask/__init__.py
@@ -50,6 +50,10 @@ def dispatch_request(self):
         req = _request_ctx_stack.top.request
         app = current_app
 
+        # Return flask's default options response. See issue #40
+        if req.method == 'OPTIONS':
+            return app.make_default_options_response()
+
         if req.routing_exception is not None:
             app.raise_routing_exception(req)
 


Test patch: