Repository: fossasia/open-event-server

Issue:
Sponsors step in event creation wizard gives 500 error.
**I'm submitting a ...**  (check one with "x")
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support requests here, instead ask your query in out Gitter channel at https://gitter.im/fossasia/open-event-server

**Current behavior:**
On proceeding to sponsors step in event creation wizard server returns 500 error.

**Steps to reproduce:**
Try creating a event with frontend using local instance of server.

**Logs are here:**
```
INFO:werkzeug:127.0.0.1 - - [20/May/2018 20:26:10] "GET /v1/event-topics?include=event-sub-topics&sort=name HTTP/1.1" 200 -
127.0.0.1 - - [20/May/2018 20:26:10] "GET /v1/events/17/sponsors HTTP/1.1" 500 -
INFO:werkzeug:127.0.0.1 - - [20/May/2018 20:26:10] "GET /v1/events/17/sponsors HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/rs/Pradeep/github/open-event-server/app/__init__.py", line 64, in __call__
    return self.app(environ, start_response)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 32, in wrapper
    return func(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/lib/python3.5/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 68, in dispatch_request
    response = method(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/decorators.py", line 56, in wrapper
    return func(*args, **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/resource.py", line 145, in get
    objects_count, objects = self._data_layer.get_collection(qs, kwargs)
  File "/home/rs/Pradeep/github/open-event-server/env/src/flask-rest-jsonapi/flask_rest_jsonapi/data_layers/alchemy.py", line 92, in get_collection
    query = self.query(view_kwargs)
  File "/home/rs/Pradeep/github/open-event-server/app/api/sponsors.py", line 47, in query
    query_ = event_query(self, query_, view_kwargs)
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/query.py", line 28, in event_query
    'Authorization' not in request.headers or not has_access(permission, event_id=event.id)):
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/permission_manager.py", line 445, in has_access
    auth = permissions[access_level](lambda *a, **b: True, (), {}, (), **kwargs)
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/permission_manager.py", line 89, in is_coorganizer_endpoint_related_to_event
    user = get_identity()
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/jwt.py", line 51, in get_identity
    payload = json.loads(base64.b64decode(token_second_segment))
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
127.0.0.1 - - [20/May/2018 20:26:10] "GET /v1/events/57acc8e2?include=event-topic%2Cevent-sub-topic%2Cevent-type%2Cevent-copyright%2Ctax HTTP/1.1" 200
```


Discussion:
This needs to be `json.loads(str(base64.b64decode(token_second_segment), 'utf-8'))` in `jwt.py`
@pradeepgangwar Please go ahead and fix it if you want as @iamareebjamal stated.
@srv-twry Ok I will do it. Thanks. :sweat_smile: 
Please check and report if it is working as intended after fixing @pradeepgangwar 
Also please upgrade to Python 3.6.+ :smile: This occurs on 3.5, but yes it needs to be fixed
@iamareebjamal it gives a new error now. I tried searching for its solution, but couldn't find one. Can you look into this? :sweat_smile: 

```
    user = get_identity()
  File "/home/rs/Pradeep/github/open-event-server/app/api/helpers/jwt.py", line 52, in get_identity
    payload = json.loads(str(base64.b64decode(token_second_segment)))
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
```
Did you write this? `json.loads(str(base64.b64decode(token_second_segment), 'utf-8'))`?
`str(base64.b64decode(token_second_segment))` converts JSON 

```json
{
  "name" : "Areeb Jamal"
}
```

to 

`'b\'{"name":"Areeb"}\''`
 which cannot be parsed as JSON as it is not valid JSON

But 
`str(base64.b64decode(token_second_segment), 'utf-8')` converts it correctly
@iamareebjamal Sorry, I forgot 'utf-8' . I think previous comment was edited. Sorry for inconvenience. :smile: 
It works now. :fireworks: 

Resolving patch:
diff --git a/app/api/helpers/jwt.py b/app/api/helpers/jwt.py
--- a/app/api/helpers/jwt.py
+++ b/app/api/helpers/jwt.py
@@ -48,6 +48,6 @@ def get_identity():
     if missing_padding != 0:
         token_second_segment += '=' * (4 - missing_padding)
 
-    payload = json.loads(base64.b64decode(token_second_segment))
+    payload = json.loads(str(base64.b64decode(token_second_segment), 'utf-8'))
     user = jwt_identity(payload)
     return user


Test patch: